class_name TubeAirflow extends Node2D # Motes showing the air through every one-way tube (OneWayTube) in `layer`. # At an entrance, orange motes drift in like dust sucked into a vacuum: they # start spread in a fan in front of the mouth, speed up and funnel into it. At # an exit, white motes are blown out: fast at first, slowing and spreading as # they go, fading out at the end of the blow zone (where TubeAirCurrents stops pushing). # Call refresh() after changing the layer's tiles. const MOTES_PER_MOUTH = 18 const INTAKE_REACH = 40.0 # How far out from an entrance motes start, in px. const INTAKE_SPREAD = 22.0 # How far to the side of an entrance motes can start, in px. const BLOW_SPREAD = 14.0 # How far to the side blown motes drift by the end, in px. const TRIP_TIME = 1.1 # Seconds per mote trip. const BLOW_TRIP_TIME = 0.6 # Blown motes are quicker. const FADE = 0.2 # The share of a trip spent fading in (intake) or out (blown). const MOTE_COLOR = Color(1.0, 0.6, 0.2) # Drawn in. const BLOW_COLOR = Color(0.85, 0.85, 0.85) # Blown out (lightened up to white). 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 # Per mouth: {mouth, outward: Vector2, isIntake, reach: px, spread: px, tripTime}. var _mouths: Array[Dictionary] = [] var _time := 0.0 func _ready() -> void: top_level = true # Mouths are in global coordinates. z_index = 1 # In front of the tiles. refresh() func refresh() -> void: _mouths.clear() if layer == null: return for entrance in OneWayTube.find_entrances(layer): _mouths.append({mouth = entrance.mouth, outward = Vector2(entrance.outward), isIntake = true, reach = INTAKE_REACH, spread = INTAKE_SPREAD, tripTime = TRIP_TIME}) var tileLength := float(layer.tile_set.tile_size.x) for exit in OneWayTube.find_exits(layer): var zoneLength := OneWayTube.blow_zone(layer, exit.cell, exit.outward).size() _mouths.append({mouth = exit.mouth, outward = Vector2(exit.outward), isIntake = false, reach = zoneLength * tileLength, spread = BLOW_SPREAD, tripTime = BLOW_TRIP_TIME}) queue_redraw() func _process(delta: float) -> void: if _mouths.is_empty(): return _time += delta queue_redraw() func _draw() -> void: for mouthIndex in _mouths.size(): var mouth: Dictionary = _mouths[mouthIndex] var across: Vector2 = mouth.outward.orthogonal() for moteIndex in MOTES_PER_MOUTH: # Each mote loops on its own offset, and gets a fresh random path # each trip. var tripClock: float = _time / mouth.tripTime + 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: float = random.randf_range(-1.0, 1.0) * mouth.spread var lighten := random.randf() * MOTE_LIGHTEN var moteSize := Vector2.ONE * (2.0 if random.randf() < BIG_MOTE_SHARE else 1.0) # How far out the mote is, from 0 (at the mouth) to 1 (at its reach). # Drawn in: slow at first, then faster. Blown out: fast, then slower. var out := 1.0 - progress * progress if mouth.isIntake else 1.0 - (1.0 - progress) * (1.0 - progress) var at: Vector2 = mouth.mouth + mouth.outward * (mouth.reach * out) + across * (side * out) var color := (MOTE_COLOR if mouth.isIntake else BLOW_COLOR).lightened(lighten) var fade := progress if mouth.isIntake else 1.0 - progress color.a = clampf(fade / FADE, 0.0, 1.0) draw_rect(Rect2(at.floor(), moteSize), color)