SlimePlatformer/Effects/one_way_tube.gd

50 lines
2 KiB
GDScript3
Raw Normal View History

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. TubeVacuum shows where the entrances are.
const ONE_WAY_LAYER = "tube_one_way"
const ENTRY_LAYER = "tube_one_way_entry"
# 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:
# [{mouth: global point, outward: Vector2}].
static func find_entrances(layer: TileMapLayer) -> Array[Dictionary]:
var entrances: Array[Dictionary] = []
for cell in layer.get_used_cells():
var entrySide := get_entry_side(layer, cell)
if entrySide == 0 or not is_one_way(layer, cell):
continue
for direction in TubeSteer.DIRECTIONS:
if TubeSteer.side_bit(direction) == entrySide and layer.get_cell_source_id(cell + direction) == -1:
entrances.append({mouth = TubeSteer.cell_point(layer, cell, direction), outward = Vector2(direction)})
return entrances