SlimePlatformer/Levels/Maze/maze_tiles.gd
tachyonwolf e7ae22b8cd Add Maze mode with one-way tubes and a soft-lock check
- Main menu "Maze" button opens a setup panel (width, height in screens;
  complexity 1-5) and generates a maze of rock chambers joined by tubes,
  with Start and End pads and a win panel.
- New one-way tube kind: orange tiles that only let the slime in at their
  entrance, with motes drifting into it (OneWayTube, TubeVacuum). The
  auto-tube trace moved to PixelStream.trace_tube so the player and the
  maze validator share it.
- Generation: chamber graph (spanning tree rooted at End, one-way tubes
  only towards End on the route, extra tubes), carved with AStarGrid2D.
  MazeValidator re-derives the chamber graph from the tiles and checks
  that End is reachable from every chamber reachable from Start.
- tests/maze_check.gd: headless batch check (360 mazes) plus a known
  soft-lock the validator must reject. See docs/MAZE.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:45:46 -06:00

61 lines
2.3 KiB
GDScript

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, GOO, 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"
const GOO_LAYER = "tube_goo"
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
if data.get_custom_data(GOO_LAYER):
return MazeGraph.GOO
return MazeGraph.AUTO
static func _key(openings: int, kind: StringName, entrySide: int) -> String:
return "%d/%s/%d" % [openings, kind, entrySide]