SlimePlatformer/Effects/tube_blower.gd

52 lines
1.8 KiB
GDScript3
Raw Normal View History

class_name TubeBlower
extends Node2D
# Air blowing out of every one-way tube's exit (OneWayTube.blow_zone) in
# `layer`. While `target`'s middle is in a blow zone, it's knocked away from
# the mouth: off a wall or ceiling it's stuck to, or lifted off the floor
# (the target's blow_away(), see player.gd). Sideways blowers also lift a
# little, so they knock the slime off the floor too, not just slide it.
# Outside the zone there's no push, so the slime can always get away (it can
# steer in the air, and the zone is never as wide as the chamber).
# Call refresh() after changing the layer's tiles.
const BLOW_SPEED = 240.0 # px/s, away from the mouth.
const SIDEWAYS_LIFT = 120.0 # px/s up, for mouths blowing sideways.
@export var layer: TileMapLayer
@export var target: Node2D
var _zones: Array[Dictionary] = [] # {rect: global Rect2, push: Vector2 px/s}.
func _ready() -> void:
process_physics_priority = -1 # Before the player moves this frame.
refresh()
func refresh() -> void:
_zones.clear()
if layer == null:
return
var tileSize := Vector2(layer.tile_set.tile_size)
for exit in OneWayTube.find_exits(layer):
var zone := OneWayTube.blow_zone(layer, exit.cell, exit.outward)
if zone.is_empty():
continue
var push := Vector2(exit.outward) * BLOW_SPEED
if exit.outward.y == 0:
push.y = -SIDEWAYS_LIFT
var rect := Rect2(layer.to_global(layer.map_to_local(zone[0])) - tileSize / 2.0, tileSize)
for zoneCell in zone:
rect = rect.merge(Rect2(layer.to_global(layer.map_to_local(zoneCell)) - tileSize / 2.0, tileSize))
_zones.append({rect = rect, push = push})
func _physics_process(_delta: float) -> void:
if target == null or not target.has_method(&"blow_away"):
return
for zone in _zones:
if zone.rect.has_point(target.global_position):
target.blow_away(zone.push)
return