68 lines
2.4 KiB
GDScript3
68 lines
2.4 KiB
GDScript3
|
|
class_name TubeAirCurrents
|
||
|
|
extends Node2D
|
||
|
|
|
||
|
|
# The air through every one-way tube (OneWayTube) in `layer`, acting on
|
||
|
|
# `target` (the player):
|
||
|
|
# - Sucking: while its middle is on the tile in front of an entrance, it's
|
||
|
|
# pulled into the tube without pressing anything (the target's
|
||
|
|
# pull_into_tube(), see player.gd).
|
||
|
|
# - Blowing: while its middle is in an exit's blow zone (OneWayTube.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()). 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.
|
||
|
|
# TubeAirflow draws the motes. 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}.
|
||
|
|
var _intakes: Array[Dictionary] = [] # {rect: global Rect2, cell: the entrance tube tile, direction: into it}.
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
process_physics_priority = -1 # Before the player moves this frame.
|
||
|
|
refresh()
|
||
|
|
|
||
|
|
|
||
|
|
func refresh() -> void:
|
||
|
|
_zones.clear()
|
||
|
|
_intakes.clear()
|
||
|
|
if layer == null:
|
||
|
|
return
|
||
|
|
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 := _tile_rect(zone[0])
|
||
|
|
for zoneCell in zone:
|
||
|
|
rect = rect.merge(_tile_rect(zoneCell))
|
||
|
|
_zones.append({rect = rect, push = push})
|
||
|
|
for entrance in OneWayTube.find_entrances(layer):
|
||
|
|
_intakes.append({rect = _tile_rect(entrance.cell + entrance.outward), cell = entrance.cell,
|
||
|
|
direction = Vector2(-entrance.outward)})
|
||
|
|
|
||
|
|
|
||
|
|
func _physics_process(_delta: float) -> void:
|
||
|
|
if target == null or not target.has_method(&"blow_away"):
|
||
|
|
return
|
||
|
|
for intake in _intakes:
|
||
|
|
if intake.rect.has_point(target.global_position):
|
||
|
|
target.pull_into_tube(layer, intake.cell, intake.direction)
|
||
|
|
return
|
||
|
|
for zone in _zones:
|
||
|
|
if zone.rect.has_point(target.global_position):
|
||
|
|
target.blow_away(zone.push)
|
||
|
|
return
|
||
|
|
|
||
|
|
|
||
|
|
func _tile_rect(cell: Vector2i) -> Rect2:
|
||
|
|
var tileSize := Vector2(layer.tile_set.tile_size)
|
||
|
|
return Rect2(layer.to_global(layer.map_to_local(cell)) - tileSize / 2.0, tileSize)
|