class_name TileLayer extends RefCounted ## A single tile layer within a chunk. ## ## Holds the dense grid of tile IDs, the palette mapping tile IDs to tile ## definitions, and the layer metadata. This is the runtime representation of ## the `TileLayer` from the chunk data format (see docs/architecture/world.md ## ยง3.2). ## Layer name (e.g. "ground", "walls", "decor"). var name: String = "" ## Numeric layer id, unique within the chunk. var layer_id: int = 0 ## Grid dimensions in tiles. var width: int = 0 var height: int = 0 var depth: int = 1 ## Palette mapping tile IDs to tile definitions (shared per layer). var palette: Array[TileDef] = [] ## Dense, row-major tile IDs. Length = width * height * depth. var tiles: PackedInt32Array = PackedInt32Array() func _init( p_layer_name: String = "", p_layer_id: int = 0, p_width: int = 0, p_height: int = 0, p_depth: int = 1 ) -> void: name = p_layer_name layer_id = p_layer_id width = p_width height = p_height depth = p_depth ## Total number of tiles in this layer. func tile_count() -> int: return width * height * depth ## Get the tile ID at (x, y, z). Returns 0 (empty) for out-of-bounds. func get_tile(x: int, y: int, z: int = 0) -> int: if x < 0 or x >= width or y < 0 or y >= height or z < 0 or z >= depth: return 0 var idx := (z * height + y) * width + x if idx < tiles.size(): return tiles[idx] return 0 ## Set the tile ID at (x, y, z). No-op if out of bounds. func set_tile(x: int, y: int, z: int, tile_id: int) -> void: if x < 0 or x >= width or y < 0 or y >= height or z < 0 or z >= depth: return var idx := (z * height + y) * width + x if idx < tiles.size(): tiles[idx] = tile_id ## Get the tile definition for a tile ID, or null if not in the palette. func get_tile_def(tile_id: int) -> TileDef: for def in palette: if def.id == tile_id: return def return null ## Whether the tile at (x, y, z) is solid (collision). func is_solid(x: int, y: int, z: int = 0) -> bool: var def := get_tile_def(get_tile(x, y, z)) return def != null and def.solid ## Whether the tile at (x, y, z) is walkable. func is_walkable(x: int, y: int, z: int = 0) -> bool: var def := get_tile_def(get_tile(x, y, z)) return def != null and def.is_walkable()