SlimePlatformer/Effects/tube_vacuum.gd

64 lines
2.3 KiB
GDScript3
Raw Normal View History

class_name TubeVacuum
extends Node2D
# Orange motes drifting into the entrance of every one-way tube (OneWayTube) in
# `layer`, like dust sucked into a vacuum: they start out spread in a fan in
# front of the mouth, speed up as they get close, and funnel into it.
# Call refresh() after changing the layer's tiles.
const MOTES_PER_MOUTH = 18
const REACH = 40.0 # How far out from the mouth motes start, in px.
const SPREAD = 22.0 # How far to the side of the mouth motes can start, in px.
const TRIP_TIME = 1.1 # Seconds from appearing to vanishing into the mouth.
const FADE_IN = 0.2 # The share of the trip spent fading in.
const MOTE_COLOR = Color(1.0, 0.6, 0.2)
const MOTE_LIGHTEN = 0.5 # Motes are up to this much lighter than MOTE_COLOR.
const BIG_MOTE_SHARE = 0.3 # This share of motes are 2x2 pixels; the rest are single pixels.
@export var layer: TileMapLayer
var _entrances: Array[Dictionary] = []
var _time := 0.0
func _ready() -> void:
top_level = true # Entrances are in global coordinates.
z_index = 1 # In front of the tiles.
refresh()
func refresh() -> void:
_entrances = OneWayTube.find_entrances(layer) if layer != null else []
queue_redraw()
func _process(delta: float) -> void:
if _entrances.is_empty():
return
_time += delta
queue_redraw()
func _draw() -> void:
for mouthIndex in _entrances.size():
var entrance: Dictionary = _entrances[mouthIndex]
var outward: Vector2 = entrance.outward
var across := outward.orthogonal()
for moteIndex in MOTES_PER_MOUTH:
# Each mote loops on its own offset, and gets a fresh random start
# each trip.
var tripClock := _time / TRIP_TIME + float(moteIndex) / MOTES_PER_MOUTH
var trip := floori(tripClock)
var progress := tripClock - trip
var random := RandomNumberGenerator.new()
random.seed = hash(Vector3i(mouthIndex, moteIndex, trip))
var side := random.randf_range(-1.0, 1.0) * SPREAD
var lighten := random.randf() * MOTE_LIGHTEN
var moteSize := Vector2.ONE * (2.0 if random.randf() < BIG_MOTE_SHARE else 1.0)
# Slow at first, then faster and faster into the mouth.
var remaining := 1.0 - progress * progress
var at: Vector2 = entrance.mouth + outward * (REACH * remaining) + across * (side * remaining)
var color := MOTE_COLOR.lightened(lighten)
color.a = clampf(progress / FADE_IN, 0.0, 1.0)
draw_rect(Rect2(at.floor(), moteSize), color)