class_name OneWayTube extends RefCounted # One-way tubes are ordinary tubes (the slime streams through as a PixelStream) # that only let it in at one end. Their tiles are tagged "tube_one_way", and # the tile at the entrance also says which of its sides is the way in # ("tube_one_way_entry": a TUBE_OPEN_... bit, see player.gd). Every other mouth # is an exit only, and air blows out of it, knocking the slime away (TubeAirCurrents). Air also # rushes into the entrance and pulls the slime in. # TubeAirflow shows both: motes drawn into entrances and blown out of exits. const ONE_WAY_LAYER = "tube_one_way" const ENTRY_LAYER = "tube_one_way_entry" const BLOW_RANGE = 1 # How many tiles out of an exit mouth the air blows. # Whether the tile at `cell` is part of a one-way tube. static func is_one_way(layer: TileMapLayer, cell: Vector2i) -> bool: var data := layer.get_cell_tile_data(cell) if data == null or not data.has_custom_data(ONE_WAY_LAYER): return false return data.get_custom_data(ONE_WAY_LAYER) # The side bit of `cell`'s entrance mouth, or 0 if it has none. static func get_entry_side(layer: TileMapLayer, cell: Vector2i) -> int: var data := layer.get_cell_tile_data(cell) if data == null or not data.has_custom_data(ENTRY_LAYER): return 0 return data.get_custom_data(ENTRY_LAYER) # Whether the slime may go into the tube at `cell` heading `direction`. Always # true for other kinds of tube. static func can_enter(layer: TileMapLayer, cell: Vector2i, direction: Vector2i) -> bool: if not is_one_way(layer, cell): return true return get_entry_side(layer, cell) & TubeSteer.side_bit(-direction) != 0 # Every entrance mouth in the layer that opens onto an empty tile (see _find_mouths). static func find_entrances(layer: TileMapLayer) -> Array[Dictionary]: return _find_mouths(layer, true) # Every exit mouth in the layer that opens onto an empty tile (see _find_mouths). # Air blows out of these: see blow_zone() and TubeAirCurrents. static func find_exits(layer: TileMapLayer) -> Array[Dictionary]: return _find_mouths(layer, false) # The empty tiles the air out of an exit mouth reaches: up to BLOW_RANGE tiles # straight out from the tube tile `cell`'s side `outward`, stopping at anything # solid. MazeCarver uses this before the tube's tiles are down, so it only # looks at the tiles in front. static func blow_zone(layer: TileMapLayer, cell: Vector2i, outward: Vector2i) -> Array[Vector2i]: var zone: Array[Vector2i] = [] for step in range(1, BLOW_RANGE + 1): var zoneCell := cell + outward * step if layer.get_cell_source_id(zoneCell) != -1: break zone.append(zoneCell) return zone # One-way mouths opening onto an empty tile, entrances or exits: # [{cell, mouth: global point, outward: Vector2i}]. static func _find_mouths(layer: TileMapLayer, isEntrance: bool) -> Array[Dictionary]: var mouths: Array[Dictionary] = [] for cell in layer.get_used_cells(): if not is_one_way(layer, cell): continue var openings := TubeSteer.get_openings(layer, cell) var entrySide := get_entry_side(layer, cell) for direction in TubeSteer.DIRECTIONS: var side := TubeSteer.side_bit(direction) if openings & side == 0 or layer.get_cell_source_id(cell + direction) != -1: continue if (side == entrySide) == isEntrance: mouths.append({cell = cell, mouth = TubeSteer.cell_point(layer, cell, direction), outward = direction}) return mouths