extends Node ## Persistent run progression across scene changes — the Slay-the-Spire-style map and ## where the player sits on it. The arena scene is reloaded for every fight, so this ## state must outlive scene changes: genuinely global run state earns an autoload (`Run`). ## ## Flow: menu Play → start_new_run() → arena (intro fight) → win → MapScreen shows the ## first frontier → pick a node → its level scene → win → back to MapScreen advanced one ## row, and on until a boss node ends the map. Every node is the arena for now; adding a ## level later is a new LevelDef in the catalog and pointing the generator at it. signal run_started const _ARENA_SCENE := "res://scene.tscn" # Map shape. Small + placeholder for now; the final row is a single boss node. const _ROWS := 5 const _MIN_PER_ROW := 2 const _MAX_PER_ROW := 4 ## map[row] is an Array[MapNode]; empty until a run starts. var map: Array = [] ## Node the player currently occupies. (-1, -1) = the start gate below the first row — ## i.e. straight after the intro fight, before any map node has been chosen. var player_row: int = -1 var player_col: int = -1 ## Node picked on the map whose level is being played now; committed to on the next win. var pending_row: int = -1 var pending_col: int = -1 # Level catalog. Only the arena exists today (a normal and a boss flavour, both loading # the same scene). Future levels — cave/bear, china shop — slot in here. var _arena: LevelDef var _arena_boss: LevelDef func _ready() -> void: _arena = _make_level(&"arena", "The Arena", _ARENA_SCENE, Color(0.80, 0.62, 0.24), false) _arena_boss = _make_level( &"arena_boss", "Grand Arena", _ARENA_SCENE, Color(0.88, 0.30, 0.20), true ) func _make_level( id: StringName, level_name: String, scene: String, color: Color, boss: bool ) -> LevelDef: var l := LevelDef.new() l.id = id l.display_name = level_name l.scene_path = scene l.color = color l.is_boss = boss return l ## Begin a fresh run: build a new map and place the player at the start gate. Called from ## the main-menu Play button, not from the arena scene (which reloads for every fight). func start_new_run() -> void: _generate_map() player_row = -1 player_col = -1 pending_row = -1 pending_col = -1 run_started.emit() ## Build a run if none exists — a safety net for entering the map or arena directly (e.g. ## running scene.tscn straight from the editor) without coming through the menu. func ensure_run() -> void: if map.is_empty(): start_new_run() func has_run() -> bool: return not map.is_empty() func node_at(row: int, col: int) -> MapNode: if row < 0 or row >= map.size(): return null var r: Array = map[row] if col < 0 or col >= r.size(): return null return r[col] func current_node() -> MapNode: return node_at(player_row, player_col) ## Nodes the player may pick next: the whole first row from the start gate, otherwise the ## nodes the current node links forward to. func reachable() -> Array: if map.is_empty(): return [] if player_row < 0: return (map[0] as Array).duplicate() var here := current_node() if here == null or player_row + 1 >= map.size(): return [] var out: Array = [] for c: int in here.links: var n := node_at(player_row + 1, c) if n != null: out.append(n) return out ## True once the player has cleared a boss node — the map is finished. func run_complete() -> bool: var here := current_node() return here != null and here.level != null and here.level.is_boss ## Mark a node as the one being played (its level scene is about to load). func select_node(node: MapNode) -> void: if node == null: return pending_row = node.row pending_col = node.col ## Commit the level just won: move the player onto the pending node (or leave them at the ## start gate after the intro fight, which has no pending node). func complete_current_level() -> void: if pending_row >= 0: player_row = pending_row player_col = pending_col var n := current_node() if n != null: n.cleared = true pending_row = -1 pending_col = -1 # ── Map generation ──────────────────────────────────────────────────────────── # A small branching lattice: each row gets a handful of nodes, each node links to the # nearest node(s) in the next row, and every next-row node is guaranteed a parent so the # path is always connected (no orphan the player can never reach). The final row is a # single boss node the whole map funnels toward. func _generate_map() -> void: map.clear() for r: int in _ROWS: var count := 1 if r == _ROWS - 1 else randi_range(_MIN_PER_ROW, _MAX_PER_ROW) var y := 1.0 - float(r + 1) / float(_ROWS + 1) # row 0 near the bottom, boss on top var row_nodes: Array = [] for c: int in count: var node := MapNode.new() node.row = r node.col = c node.pos = Vector2(_column_x(c, count), y) node.level = _arena_boss if r == _ROWS - 1 else _arena row_nodes.append(node) map.append(row_nodes) _link_rows() func _column_x(col: int, count: int) -> float: if count <= 1: return 0.5 return lerpf(0.18, 0.82, float(col) / float(count - 1)) func _link_rows() -> void: for r: int in _ROWS - 1: var cur: Array = map[r] var nxt: Array = map[r + 1] var incoming := PackedInt32Array() incoming.resize(nxt.size()) # zero-filled for from_node: MapNode in cur: var nearest := _nearest_index(from_node, nxt) _add_link(from_node, nearest, incoming) # Occasionally fan out to an adjacent next-row node so paths branch. if nxt.size() > 1 and randf() < 0.45: var step := 1 if randf() < 0.5 else -1 _add_link(from_node, clampi(nearest + step, 0, nxt.size() - 1), incoming) # Guarantee every next-row node has at least one parent (no orphans). for c: int in nxt.size(): if incoming[c] == 0: _add_link(cur[_nearest_index(nxt[c], cur)], c, incoming) func _add_link(from_node: MapNode, to_col: int, incoming: PackedInt32Array) -> void: if to_col in from_node.links: return from_node.links.append(to_col) incoming[to_col] += 1 # Index of the node in `others` whose column position is closest to `node` on the x axis — # used both to link a node forward and to adopt an orphan back to its nearest parent. func _nearest_index(node: MapNode, others: Array) -> int: var best := 0 var best_d := INF for i: int in others.size(): var d: float = absf((others[i] as MapNode).pos.x - node.pos.x) if d < best_d: best_d = d best = i return best