class_name MazeTiles extends RefCounted # Finds the tile for a tube of a given shape and kind in a TileSet, by reading # the tiles' custom data (so nothing here hard-codes atlas positions). # Kinds are MazeGraph's: AUTO, MANUAL, ONE_WAY, plus ROCK for solid tiles. const ROCK = &"rock" # The TileSet custom data layers (see player.gd and the tube scripts). const OPENINGS_LAYER = "tube_openings" const MANUAL_LAYER = "tube_manual" var _sourceId := -1 var _tiles := {} # "openings/kind/entrySide" -> [atlas coords, alternative]. func _init(tileSet: TileSet) -> void: for sourceIndex in tileSet.get_source_count(): var sourceId := tileSet.get_source_id(sourceIndex) var source := tileSet.get_source(sourceId) as TileSetAtlasSource if source == null: continue _sourceId = sourceId for tileIndex in source.get_tiles_count(): var coords := source.get_tile_id(tileIndex) for alternativeIndex in source.get_alternative_tiles_count(coords): var alternative := source.get_alternative_tile_id(coords, alternativeIndex) var data := source.get_tile_data(coords, alternative) var key := _key(data.get_custom_data(OPENINGS_LAYER), _kind_of(data), data.get_custom_data(OneWayTube.ENTRY_LAYER)) if not _tiles.has(key): _tiles[key] = [coords, alternative] return # Puts the tile at `cell`. openings: TUBE_OPEN_... bits (0 for rock). # entrySide: for a one-way tube's entrance tile, the side bit of its way in. func set_tile(layer: TileMapLayer, cell: Vector2i, openings: int, kind: StringName, entrySide := 0) -> bool: var key := _key(openings, kind, entrySide) if not _tiles.has(key): push_error("MazeTiles: no tile for openings %d, kind %s, entry %d" % [openings, kind, entrySide]) return false layer.set_cell(cell, _sourceId, _tiles[key][0], _tiles[key][1]) return true static func _kind_of(data: TileData) -> StringName: if data.get_custom_data(OPENINGS_LAYER) == 0: return ROCK if data.get_custom_data(OneWayTube.ONE_WAY_LAYER): return MazeGraph.ONE_WAY if data.get_custom_data(MANUAL_LAYER): return MazeGraph.MANUAL return MazeGraph.AUTO static func _key(openings: int, kind: StringName, entrySide: int) -> String: return "%d/%s/%d" % [openings, kind, entrySide]