From fe135557786ac9f1d49b4b88c3f01c4c5952f6a3 Mon Sep 17 00:00:00 2001 From: goose Date: Fri, 2 Oct 2026 23:58:07 -0400 Subject: [PATCH] feat: add multi-floor dungeon transitions --- .superpowers/sdd/task-B5-report.md | 57 ++++++++++++++++ scripts/domain/game_state.gd | 7 +- scripts/domain/interaction_resolver.gd | 5 +- scripts/domain/run/floor_transition.gd | 53 +++++++++++++++ scripts/domain/run/floor_transition.gd.uid | 1 + scripts/domain/world/room_graph.gd | 4 +- scripts/domain/world/spawn_rules.gd | 4 +- scripts/domain/world/surface_generator.gd | 43 ++++++++++++ scripts/domain/world/surface_generator.gd.uid | 1 + scripts/game/game.gd | 29 ++++++-- .../infrastructure/content/content_loader.gd | 2 +- scripts/presentation/hud.gd | 3 +- tests/e2e/test_demo_map_chest_e2e.gd | 3 +- tests/e2e/test_door_to_stairs_exit_e2e.gd | 16 ++--- tests/e2e/test_hud_prompts_e2e.gd | 5 +- tests/e2e/test_menu_multi_floor_e2e.gd | 67 +++++++++++++++++++ tests/e2e/test_menu_multi_floor_e2e.gd.uid | 1 + tests/unit/test_stairs_interaction.gd | 9 +-- tests/unit/test_surface_floor_transition.gd | 66 ++++++++++++++++++ .../unit/test_surface_floor_transition.gd.uid | 1 + 20 files changed, 348 insertions(+), 29 deletions(-) create mode 100644 .superpowers/sdd/task-B5-report.md create mode 100644 scripts/domain/run/floor_transition.gd create mode 100644 scripts/domain/run/floor_transition.gd.uid create mode 100644 scripts/domain/world/surface_generator.gd create mode 100644 scripts/domain/world/surface_generator.gd.uid create mode 100644 tests/e2e/test_menu_multi_floor_e2e.gd create mode 100644 tests/e2e/test_menu_multi_floor_e2e.gd.uid create mode 100644 tests/unit/test_surface_floor_transition.gd create mode 100644 tests/unit/test_surface_floor_transition.gd.uid diff --git a/.superpowers/sdd/task-B5-report.md b/.superpowers/sdd/task-B5-report.md new file mode 100644 index 0000000..bd8a64d --- /dev/null +++ b/.superpowers/sdd/task-B5-report.md @@ -0,0 +1,57 @@ +# Task B5 — Floor 0, descent/ascent and multi-floor state + +- **Worktree:** `/home/hectelyon/Documents/Projects/ProjetsPersos/DungeonCrawlerWorld/.worktrees/b5` +- **Brief:** `docs/superpowers/plans/2026-10-02-full-dcwascii-godot-port.md`, Task B5 +- **Scope:** authored surface floor, deterministic procedural floor transitions, cached per-floor state, and transition UI/tests. B6 time pressure, deadlines, beds, collapse, and staircase gating are deliberately not included. + +## Delivery + +- Added `SurfaceGenerator`: a fixed authored 30×20 floor 0 with the existing tutorial door/chest/stairs layout, entrance `(4, 12)`, exit `(18, 4)`, and `hand_authored` metadata. +- Added `FloorTransition` in the planned run-domain location. It caches floors by number in `RunState`, uses the existing deterministic `seed-floorN` generator for procedural floors, and makes both the entry/up stair and exit/down stair explicit on generated floors. +- Descending from floor 0 reaches generated floor 1 at its entrance; using that entry stair returns to cached floor 0 at its exit. Each later procedural floor follows the same up/down convention. +- `Game` swaps the active `FloorState`, map renderer visibility, player/camera, enemy views, and HUD after a stair interaction. `GameState.set_floor_state` centralizes the active-floor aliases and FOV refresh. +- Persistent visited-floor state is retained through the cached `FloorState`; chest opening now marks its floor-state chest record open as well as changing the map cell. +- HUD distinguishes floor-1+ entry stairs with `Espace — Monter l’escalier`; exit/down stairs retain `Espace — Descendre l’escalier`. +- Added unit coverage for authored-vs-procedural/deterministic floors and cache persistence, plus a real menu E2E descent and return trip. Existing stair and route E2Es now assert the actual floor transition. + +## Failure correction during finalization + +The first complete E2E run found two stale expectations in pre-existing B5-adapted E2Es: following descent to floor 1, they still expected the down-stair prompt. The implementation correctly puts the player on the generated floor's **up** stair, so the tests were corrected to require `Monter l’escalier`. + +A standalone visual capture initially exposed strict GDScript warnings treated as errors in three pre-existing dependency scripts (Variant type inference). Explicit type annotations were restored because the new transition imports that dependency path and visual verification must compile it. No behavior was changed by those annotations. + +## Verification + +Executed with Godot 4.5.0 (`/snap/bin/godot4`): + +```text +./scripts/test-unit.sh PASS — 73 tests, 734 assertions +./scripts/test-integration.sh PASS — 4 tests, 9,239 assertions +./scripts/test-e2e.sh PASS — 19 tests, 217 assertions +./scripts/run-visual-check.sh PASS — Xvfb capture and PNG validation +./scripts/verify.sh PASS — unit + integration + E2E + visual + +git diff --check PASS +``` + +Visual capture artifacts (ignored by Git) were produced and validated: + +```text +artifacts/visual-check.png: + dungeon_non_black_pixels=489308 + visible_pixels=17524 + explored_pixels=10438 + unknown_pixels=366972 + +artifacts/visual-check-exit.png: + dungeon_non_black_pixels=489252 + visible_pixels=17404 + explored_pixels=17146 + unknown_pixels=441144 +``` + +The test/visual runners report pre-existing GUT resource UID warnings and `ObjectDB`/resource cleanup diagnostics on exit. They have no `SCRIPT ERROR` or `Parse Error`; all runner exit statuses were successful. + +## Git + +Local commit only; no push or PR. diff --git a/scripts/domain/game_state.gd b/scripts/domain/game_state.gd index 0465697..6f58543 100644 --- a/scripts/domain/game_state.gd +++ b/scripts/domain/game_state.gd @@ -25,10 +25,15 @@ func _init(new_map: GridMapState, new_player: ActorState, new_enemies: Dictionar func bind_run_state(run_state: RunState) -> void: run_state.set_floor_state(floor_state) - floor_state = run_state.current_floor_state() + set_floor_state(run_state.current_floor_state()) + +func set_floor_state(new_floor_state: FloorState) -> void: + floor_state = new_floor_state map = floor_state.map enemies = floor_state.enemies explored_cells = floor_state.explored_cells + exit_reached = false + update_exploration() func get_context_target() -> StringName: if exit_reached: diff --git a/scripts/domain/interaction_resolver.gd b/scripts/domain/interaction_resolver.gd index 742ef04..5431725 100644 --- a/scripts/domain/interaction_resolver.gd +++ b/scripts/domain/interaction_resolver.gd @@ -20,13 +20,16 @@ static func resolve(state: GameState) -> Array[TurnResult]: var results: Array[TurnResult] = [] if state.map.get_cell(state.player.cell) == GridMapState.CellKind.STAIRS and not state.exit_reached: state.exit_reached = true - results.append(TurnResultType.new(state.player.id, &"stairs_used", state.player.cell, state.player.cell, &"", 0, "You descend the stairs.")) + var stairs_message := "You ascend the stairs." if state.floor_state.floor_number > 0 and state.player.cell == state.floor_state.entrance else "You descend the stairs." + results.append(TurnResultType.new(state.player.id, &"stairs_used", state.player.cell, state.player.cell, &"", 0, stairs_message)) TurnResolverType.append_enemy_response(state, results) return results var chest_cell := _find_cell_of_kind(state, GridMapState.CellKind.CHEST) if chest_cell.x >= 0: state.map.set_cell(chest_cell, GridMapState.CellKind.FLOOR) + if state.floor_state.chests.has(chest_cell): + state.floor_state.chests[chest_cell]["opened"] = true results.append(TurnResultType.new(state.player.id, &"chest_opened", state.player.cell, chest_cell, &"", 0, "You open the chest.")) TurnResolverType.append_enemy_response(state, results) return results diff --git a/scripts/domain/run/floor_transition.gd b/scripts/domain/run/floor_transition.gd new file mode 100644 index 0000000..db2687b --- /dev/null +++ b/scripts/domain/run/floor_transition.gd @@ -0,0 +1,53 @@ +class_name FloorTransition +extends RefCounted + +const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd") +const SurfaceGeneratorType = preload("res://scripts/domain/world/surface_generator.gd") +const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd") + +var catalog: ContentCatalog + +func _init(new_catalog: ContentCatalog) -> void: + catalog = new_catalog + +func initial_floor(run: RunState) -> FloorState: + if not run.floors.has(0): + run.set_floor_state(SurfaceGeneratorType.generate(run.seed)) + run.current_floor = 0 + return run.current_floor_state() + +# Returns the cached/generated destination and the stairs cell on which the +# player arrives. Floor 0's sole stairs descends, while every procedural floor +# has an up stair at entrance and down stair at exit. +func transition(run: RunState, source: FloorState, stairs_cell: Vector2i) -> Dictionary: + var destination_number := _destination_floor_number(source, stairs_cell) + var destination := _floor_for(run, destination_number) + var arrival := destination.entrance if destination_number > source.floor_number else destination.exit + run.current_floor = destination_number + return {"floor": destination, "arrival": arrival} + +func _destination_floor_number(source: FloorState, stairs_cell: Vector2i) -> int: + if source.floor_number == 0: + return 1 + if stairs_cell == source.entrance: + return source.floor_number - 1 + return source.floor_number + 1 + +func _floor_for(run: RunState, floor_number: int) -> FloorState: + if run.floors.has(floor_number): + return run.floors[floor_number] + if floor_number == 0: + return initial_floor(run) + var floor := FloorGeneratorType.generate(run.seed, floor_number, catalog) + # B3 only marked the down stair. B5 makes the entry stair explicit so a + # return route is visible and interaction direction is unambiguous. + floor.map.set_cell(floor.entrance, GridMapStateType.CellKind.STAIRS) + _materialize_unopened_chests(floor) + floor.metadata["hand_authored"] = false + run.set_floor_state(floor) + return floor + +func _materialize_unopened_chests(floor: FloorState) -> void: + for cell in floor.chests: + if not bool(floor.chests[cell].get("opened", false)): + floor.map.set_cell(cell, GridMapStateType.CellKind.CHEST) diff --git a/scripts/domain/run/floor_transition.gd.uid b/scripts/domain/run/floor_transition.gd.uid new file mode 100644 index 0000000..7544c86 --- /dev/null +++ b/scripts/domain/run/floor_transition.gd.uid @@ -0,0 +1 @@ +uid://dld2hudsse86m diff --git a/scripts/domain/world/room_graph.gd b/scripts/domain/world/room_graph.gd index 7cb2949..9b107e9 100644 --- a/scripts/domain/world/room_graph.gd +++ b/scripts/domain/world/room_graph.gd @@ -49,7 +49,7 @@ func tree_distances(start_index: int) -> Dictionary[int, int]: var distances: Dictionary[int, int] = {start_index: 0} var frontier: Array[int] = [start_index] while not frontier.is_empty(): - var current := frontier.pop_front() + var current: int = frontier.pop_front() for neighbor in adjacency.get(current, []): if not distances.has(neighbor): distances[neighbor] = distances[current] + 1 @@ -61,7 +61,7 @@ func farthest_tree_room(start_index: int) -> int: var farthest_index := start_index var farthest_distance := -1 for room_index in range(distances.size()): - var distance := distances.get(room_index, -1) + var distance: int = distances.get(room_index, -1) if distance > farthest_distance: farthest_index = room_index farthest_distance = distance diff --git a/scripts/domain/world/spawn_rules.gd b/scripts/domain/world/spawn_rules.gd index e146f8d..51a49ae 100644 --- a/scripts/domain/world/spawn_rules.gd +++ b/scripts/domain/world/spawn_rules.gd @@ -25,7 +25,7 @@ static func events(catalog: ContentCatalog) -> Array[Dictionary]: return _definitions(catalog, &"event") static func npc_for_role(catalog: ContentCatalog, role: StringName) -> Dictionary: - var preferred_id := { + var preferred_id: StringName = { &"shop": &"npc_wandering_merchant", &"guild": &"npc_guild_keeper", &"sanctuary": &"npc_shelter_survivor", @@ -39,7 +39,7 @@ static func npc_for_role(catalog: ContentCatalog, role: StringName) -> Dictionar return available[0] if not available.is_empty() else {} static func event_for_role(catalog: ContentCatalog, role: StringName) -> Dictionary: - var preferred_id := { + var preferred_id: StringName = { &"narrative": &"event_narrative_note", &"mystery": &"event_vending_machine", &"puzzle": &"event_puzzle_pressure_plates", diff --git a/scripts/domain/world/surface_generator.gd b/scripts/domain/world/surface_generator.gd new file mode 100644 index 0000000..1f77d6c --- /dev/null +++ b/scripts/domain/world/surface_generator.gd @@ -0,0 +1,43 @@ +class_name SurfaceGenerator +extends RefCounted + +const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd") +const FloorStateType = preload("res://scripts/domain/world/floor_state.gd") +const ActorStateType = preload("res://scripts/domain/actor_state.gd") + +# Floor zero deliberately remains an authored tutorial surface. Its geometry is +# not derived from the run seed; RunState supplies persistence by caching it. +static func generate(_seed: String = "") -> FloorState: + var map := GridMapStateType.new(Vector2i(30, 20), GridMapStateType.CellKind.WALL) + _carve_room(map, Rect2i(2, 12, 6, 5)) + _carve_room(map, Rect2i(2, 4, 7, 7)) + _carve_room(map, Rect2i(10, 2, 10, 6)) + _carve_room(map, Rect2i(21, 5, 7, 8)) + _carve_vertical_corridor(map, 4, 10, 12) + _carve_horizontal_corridor(map, 8, 10, 6) + _carve_horizontal_corridor(map, 18, 21, 6) + _carve_vertical_corridor(map, 18, 4, 6) + map.set_cell(Vector2i(4, 11), GridMapStateType.CellKind.CLOSED_DOOR) + map.set_cell(Vector2i(4, 7), GridMapStateType.CellKind.CHEST) + map.set_cell(Vector2i(18, 4), GridMapStateType.CellKind.STAIRS) + + var floor := FloorStateType.new(map, 0, 0) + floor.entrance = Vector2i(4, 12) + floor.exit = Vector2i(18, 4) + floor.enemies[&"goblin"] = ActorStateType.new(&"goblin", Vector2i(24, 8), 6, 2) + floor.chests[Vector2i(4, 7)] = {"opened": false} + floor.metadata = {"hand_authored": true, "theme": "surface"} + return floor + +static func _carve_room(map: GridMapState, room: Rect2i) -> void: + for y in range(room.position.y, room.end.y): + for x in range(room.position.x, room.end.x): + map.set_cell(Vector2i(x, y), GridMapStateType.CellKind.FLOOR) + +static func _carve_horizontal_corridor(map: GridMapState, from_x: int, to_x: int, y: int) -> void: + for x in range(from_x, to_x + 1): + map.set_cell(Vector2i(x, y), GridMapStateType.CellKind.FLOOR) + +static func _carve_vertical_corridor(map: GridMapState, x: int, from_y: int, to_y: int) -> void: + for y in range(from_y, to_y + 1): + map.set_cell(Vector2i(x, y), GridMapStateType.CellKind.FLOOR) diff --git a/scripts/domain/world/surface_generator.gd.uid b/scripts/domain/world/surface_generator.gd.uid new file mode 100644 index 0000000..90b53c4 --- /dev/null +++ b/scripts/domain/world/surface_generator.gd.uid @@ -0,0 +1 @@ +uid://cieqsjogiiein diff --git a/scripts/game/game.gd b/scripts/game/game.gd index 84f5415..e3feee6 100644 --- a/scripts/game/game.gd +++ b/scripts/game/game.gd @@ -14,6 +14,8 @@ const GameEventType = preload("res://scripts/domain/run/game_event.gd") const PlayerActionType = preload("res://scripts/domain/run/player_action.gd") const ActionRouterType = preload("res://scripts/game/action_router.gd") const ActorViewType = preload("res://scripts/presentation/actor_view.gd") +const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd") +const FloorTransitionType = preload("res://scripts/domain/run/floor_transition.gd") @onready var event_presenter: EventPresenter = $EventPresenter @onready var map_renderer: MapRenderer = $World/MapRenderer @onready var player_view: ActorView = $World/Player @@ -29,6 +31,7 @@ var action_router: ActionRouter var pending_run_settings: RunSettings var pending_player_name := "Sans-Nom" var pending_base_archetype: StringName = &"vanguard" +var floor_transition: FloorTransition func configure_new_run(settings: RunSettings, player_name: String, base_archetype: StringName) -> void: pending_run_settings = settings @@ -64,7 +67,10 @@ func _initialize_run_state() -> void: run_state = RunStateType.new(settings.seed, settings) run_state.player_name = pending_player_name run_state.base_archetype = pending_base_archetype - game_state.bind_run_state(run_state) + floor_transition = FloorTransitionType.new(ContentCatalogType.load_from_directory("res://data/ascii")) + var initial_floor := floor_transition.initial_floor(run_state) + game_state.set_floor_state(initial_floor) + game_state.player.cell = initial_floor.entrance if pending_run_settings != null: var game_started := GameEventType.new( run_state.next_turn(), @@ -170,13 +176,24 @@ func get_actor_cell_rect(actor_id: StringName) -> Rect2: return Rect2() func _process_interact() -> Array[TurnResult]: - if game_state.get_context_target() == &"exit_reached": - return _refresh_hud_for_context() - return _resolve_player_action(game_state.try_interact(), false) + var results := game_state.try_interact() + _resolve_player_action(results, false) + if not results.is_empty() and results[0].kind == &"stairs_used": + _transition_floor(results[0].from_cell) + return results -func _refresh_hud_for_context() -> Array[TurnResult]: +func _transition_floor(stairs_cell: Vector2i) -> void: + var transition := floor_transition.transition(run_state, game_state.floor_state, stairs_cell) + var destination: FloorState = transition["floor"] + game_state.set_floor_state(destination) + game_state.player.cell = transition["arrival"] + game_state.update_exploration() + map_renderer.set_map(game_state.map) + _sync_map_renderer_visibility() + player_view.set_actor(game_state.player) + _sync_camera_to_player() + _sync_enemy_views() hud.refresh(game_state, run_state.recent_events(run_state.log.size())) - return [] func _process_action(direction: Vector2i) -> Array[TurnResult]: return _resolve_player_action(game_state.try_player_action(direction), true) diff --git a/scripts/infrastructure/content/content_loader.gd b/scripts/infrastructure/content/content_loader.gd index 89e6f41..660de31 100644 --- a/scripts/infrastructure/content/content_loader.gd +++ b/scripts/infrastructure/content/content_loader.gd @@ -53,7 +53,7 @@ static func _load_file(path: String, relative_path: String, result: Dictionary) if file == null: result.diagnostics.append("Unable to read content file: %s" % relative_path) return - var parsed := JSON.parse_string(file.get_as_text()) + var parsed: Variant = JSON.parse_string(file.get_as_text()) if parsed == null: result.diagnostics.append("Invalid JSON content file: %s" % relative_path) return diff --git a/scripts/presentation/hud.gd b/scripts/presentation/hud.gd index 07de4f2..db7d809 100644 --- a/scripts/presentation/hud.gd +++ b/scripts/presentation/hud.gd @@ -4,6 +4,7 @@ extends CanvasLayer const DOOR_PROMPT := "Espace — Ouvrir la porte" const CHEST_PROMPT := "Espace — Ouvrir le coffre" const STAIRS_PROMPT := "Espace — Descendre l’escalier" +const STAIRS_UP_PROMPT := "Espace — Monter l’escalier" const EXIT_REACHED_PROMPT := "Sortie atteinte — prochain étage bientôt disponible" @onready var player_health_label: Label = $PlayerHealthLabel @@ -40,7 +41,7 @@ func get_context_prompt() -> String: &"closed_door": return DOOR_PROMPT &"stairs": - return STAIRS_PROMPT + return STAIRS_UP_PROMPT if _state.floor_state.floor_number > 0 and _state.player.cell == _state.floor_state.entrance else STAIRS_PROMPT &"exit_reached": return EXIT_REACHED_PROMPT _: diff --git a/tests/e2e/test_demo_map_chest_e2e.gd b/tests/e2e/test_demo_map_chest_e2e.gd index db5da34..a5574fc 100644 --- a/tests/e2e/test_demo_map_chest_e2e.gd +++ b/tests/e2e/test_demo_map_chest_e2e.gd @@ -43,7 +43,8 @@ func test_real_input_opens_door_then_chest_and_preserves_stairs_route() -> void: assert_eq(state.player.cell, Vector2i(18, 4)) assert_eq(prompt_label.text, "Espace — Descendre l’escalier") game._unhandled_input(_key_event(KEY_SPACE)) - assert_true(state.exit_reached) + assert_eq(game.get_run_state().current_floor, 1) + assert_eq(game.get_game_state().player.cell, game.get_game_state().floor_state.entrance) assert_string_contains(event_log.get_parsed_text(), "You descend the stairs.") func _key_event(keycode: Key) -> InputEventKey: diff --git a/tests/e2e/test_door_to_stairs_exit_e2e.gd b/tests/e2e/test_door_to_stairs_exit_e2e.gd index b910d1e..8740126 100644 --- a/tests/e2e/test_door_to_stairs_exit_e2e.gd +++ b/tests/e2e/test_door_to_stairs_exit_e2e.gd @@ -8,7 +8,7 @@ func before_each() -> void: add_child_autofree(game) await get_tree().process_frame -func test_real_input_event_completes_door_to_stairs_exit_without_replacing_the_current_map() -> void: +func test_real_input_event_completes_door_to_stairs_descent_and_arrives_on_floor_one() -> void: var state := game.get_game_state() var hud := game.get_node("HUD") as GameHud var prompt_label := game.get_node("HUD/ContextPromptLabel") as Label @@ -30,15 +30,15 @@ func test_real_input_event_completes_door_to_stairs_exit_without_replacing_the_c assert_eq(hud.get_context_prompt(), "Espace — Descendre l’escalier") game._unhandled_input(_key_event(KEY_E)) - assert_true(state.exit_reached) - assert_eq(hud.get_context_prompt(), "Sortie atteinte — prochain étage bientôt disponible") - assert_eq(prompt_label.text, "Sortie atteinte — prochain étage bientôt disponible") + assert_eq(game.get_run_state().current_floor, 1) + assert_eq(hud.get_context_prompt(), "Espace — Monter l’escalier") + assert_eq(prompt_label.text, "Espace — Monter l’escalier") assert_string_contains(event_log.get_parsed_text(), "You open the door.") assert_string_contains(event_log.get_parsed_text(), "You descend the stairs.") - assert_same(state.map, initial_map) - assert_eq(state.map.size, initial_map_size) - assert_eq(state.map.get_cell(Vector2i(18, 4)), GridMapState.CellKind.STAIRS) - assert_eq(state.player.cell, Vector2i(18, 4)) + assert_ne(state.map, initial_map) + assert_ne(state.map.size, initial_map_size) + assert_eq(initial_map.get_cell(Vector2i(18, 4)), GridMapState.CellKind.STAIRS) + assert_eq(state.player.cell, state.floor_state.entrance) func _key_event(keycode: Key) -> InputEventKey: var event := InputEventKey.new() diff --git a/tests/e2e/test_hud_prompts_e2e.gd b/tests/e2e/test_hud_prompts_e2e.gd index 9f5b8cb..bee52b5 100644 --- a/tests/e2e/test_hud_prompts_e2e.gd +++ b/tests/e2e/test_hud_prompts_e2e.gd @@ -36,8 +36,9 @@ func test_real_game_exposes_door_and_stairs_prompts_with_help_and_event_log() -> assert_eq(prompt_label.text, "Espace — Descendre l’escalier") var stairs_results := game.submit_test_interact() assert_eq(stairs_results[0].kind, &"stairs_used") - assert_eq(hud.get_context_prompt(), "Sortie atteinte — prochain étage bientôt disponible") - assert_eq(prompt_label.text, "Sortie atteinte — prochain étage bientôt disponible") + assert_eq(game.get_run_state().current_floor, 1) + assert_eq(hud.get_context_prompt(), "Espace — Monter l’escalier") + assert_eq(prompt_label.text, "Espace — Monter l’escalier") var results := game.submit_test_action(Vector2i.LEFT) assert_eq(results[0].kind, &"moved") diff --git a/tests/e2e/test_menu_multi_floor_e2e.gd b/tests/e2e/test_menu_multi_floor_e2e.gd new file mode 100644 index 0000000..ecee80d --- /dev/null +++ b/tests/e2e/test_menu_multi_floor_e2e.gd @@ -0,0 +1,67 @@ +extends GutTest + +const MainMenuScene = preload("res://scenes/menu/MainMenu.tscn") +var menu: MainMenu + +func before_each() -> void: + menu = MainMenuScene.instantiate() + add_child_autofree(menu) + await get_tree().process_frame + await get_tree().process_frame + +func test_actual_menu_new_run_descends_to_generated_floor_one_and_returns_to_surface() -> void: + await _click_button(menu.get_node("Menu/Layout/NewRunButton") as Button) + var creation := menu.get_node("CharacterCreation") as CharacterCreation + (creation.get_node("Panel/Layout/Fields/SeedEdit") as LineEdit).text = "B5-MENU-E2E" + await _click_button(creation.get_node("Panel/Layout/Actions/StartRunButton") as Button) + await get_tree().process_frame + + var game := menu.get_node("Game") as Game + assert_not_null(game) + assert_eq(game.get_run_state().current_floor, 0) + var surface := game.get_game_state().floor_state + assert_true(surface.metadata.get("hand_authored", false)) + game.get_game_state().player.cell = surface.exit + game.submit_test_interact() + await get_tree().process_frame + + var first_floor := game.get_game_state().floor_state + assert_eq(game.get_run_state().current_floor, 1) + assert_false(first_floor.metadata.get("hand_authored", false)) + assert_eq(game.get_game_state().player.cell, first_floor.entrance) + assert_eq(first_floor.map.get_cell(first_floor.entrance), GridMapState.CellKind.STAIRS) + assert_eq((game.get_node("HUD") as GameHud).get_context_prompt(), "Espace — Monter l’escalier") + game.get_game_state().player.cell = first_floor.entrance + game.submit_test_interact() + await get_tree().process_frame + + assert_string_contains((game.get_node("HUD/EventLog") as RichTextLabel).get_parsed_text(), "You ascend the stairs.") + assert_eq(game.get_run_state().current_floor, 0) + assert_same(game.get_game_state().floor_state, surface) + assert_eq(game.get_game_state().player.cell, surface.exit) + +func _click_button(button: Button) -> void: + assert_not_null(button) + assert_true(button.visible) + var click_position := button.get_global_rect().get_center() + get_tree().root.push_input(_mouse_motion_event(click_position), true) + await get_tree().process_frame + get_tree().root.push_input(_mouse_button_event(click_position, true), true) + await get_tree().process_frame + get_tree().root.push_input(_mouse_button_event(click_position, false), true) + await get_tree().process_frame + +func _mouse_motion_event(position: Vector2) -> InputEventMouseMotion: + var event := InputEventMouseMotion.new() + event.position = position + event.global_position = position + return event + +func _mouse_button_event(position: Vector2, pressed: bool) -> InputEventMouseButton: + var event := InputEventMouseButton.new() + event.position = position + event.global_position = position + event.button_index = MOUSE_BUTTON_LEFT + event.button_mask = MOUSE_BUTTON_MASK_LEFT if pressed else 0 + event.pressed = pressed + return event diff --git a/tests/e2e/test_menu_multi_floor_e2e.gd.uid b/tests/e2e/test_menu_multi_floor_e2e.gd.uid new file mode 100644 index 0000000..19a7317 --- /dev/null +++ b/tests/e2e/test_menu_multi_floor_e2e.gd.uid @@ -0,0 +1 @@ +uid://ctrkpp7a6m58j diff --git a/tests/unit/test_stairs_interaction.gd b/tests/unit/test_stairs_interaction.gd index 611aad1..0b09666 100644 --- a/tests/unit/test_stairs_interaction.gd +++ b/tests/unit/test_stairs_interaction.gd @@ -48,7 +48,7 @@ func test_stairs_usage_triggers_exactly_one_enemy_response_without_transitioning assert_eq(state.player.cell, stairs) assert_same(state.map, map) -func test_stairs_usage_refreshes_exact_exit_prompt_and_writes_the_journal() -> void: +func test_stairs_usage_transitions_to_floor_one_and_writes_the_journal() -> void: var game := GameScene.instantiate() as Game add_child_autofree(game) await get_tree().process_frame @@ -62,7 +62,8 @@ func test_stairs_usage_refreshes_exact_exit_prompt_and_writes_the_journal() -> v var results := game.submit_test_interact() assert_eq(results[0].kind, &"stairs_used") - assert_true(state.exit_reached) - assert_eq(hud.get_context_prompt(), "Sortie atteinte — prochain étage bientôt disponible") - assert_eq(prompt_label.text, "Sortie atteinte — prochain étage bientôt disponible") + assert_eq(game.get_run_state().current_floor, 1) + assert_eq(game.get_game_state().player.cell, game.get_game_state().floor_state.entrance) + assert_eq(hud.get_context_prompt(), "Espace — Monter l’escalier") + assert_eq(prompt_label.text, "Espace — Monter l’escalier") assert_string_contains(event_log.get_parsed_text(), "You descend the stairs.") diff --git a/tests/unit/test_surface_floor_transition.gd b/tests/unit/test_surface_floor_transition.gd new file mode 100644 index 0000000..accdc9f --- /dev/null +++ b/tests/unit/test_surface_floor_transition.gd @@ -0,0 +1,66 @@ +extends GutTest + +const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd") +const SurfaceGeneratorType = preload("res://scripts/domain/world/surface_generator.gd") +const FloorTransitionType = preload("res://scripts/domain/run/floor_transition.gd") +const RunStateType = preload("res://scripts/domain/run/run_state.gd") +const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd") + +const ASCII_CATALOG_PATH := "res://data/ascii" + +func test_floor_zero_is_hand_authored_and_floor_one_is_seed_floor_one_procedural() -> void: + var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH) + var run := RunStateType.new("B5-surface-contract") + var transitions := FloorTransitionType.new(catalog) + + var surface := transitions.initial_floor(run) + var descend := transitions.transition(run, surface, surface.exit) + var first_floor: FloorState = descend["floor"] + + assert_eq(surface.floor_number, 0) + assert_true(surface.metadata.get("hand_authored", false)) + assert_eq(surface.map.size, Vector2i(30, 20)) + assert_eq(surface.entrance, Vector2i(4, 12)) + assert_eq(surface.exit, Vector2i(18, 4)) + assert_eq(surface.map.get_cell(surface.exit), GridMapStateType.CellKind.STAIRS) + assert_eq(first_floor.floor_number, 1) + assert_false(first_floor.metadata.get("hand_authored", false)) + assert_eq(first_floor.seed, _seed_value("B5-surface-contract", 1)) + assert_eq(first_floor.map.get_cell(first_floor.entrance), GridMapStateType.CellKind.STAIRS) + assert_eq(first_floor.map.get_cell(first_floor.exit), GridMapStateType.CellKind.STAIRS) + assert_eq(descend["arrival"], first_floor.entrance) + + var independently_authored := SurfaceGeneratorType.generate("another-seed") + assert_eq(independently_authored.to_dict(), surface.to_dict(), "floor zero is authored rather than seed-generated") + +func test_transition_cache_keeps_mutated_floor_state_and_arrival_cells_across_return_trip() -> void: + var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH) + var run := RunStateType.new("B5-cache-contract") + var transitions := FloorTransitionType.new(catalog) + var surface := transitions.initial_floor(run) + var first_descent := transitions.transition(run, surface, surface.exit) + var first_floor: FloorState = first_descent["floor"] + first_floor.chests[Vector2i(3, 3)] = {"opened": true} + first_floor.enemies.clear() + first_floor.events.append({"id": "first-floor-event", "resolved": true}) + + var ascent := transitions.transition(run, first_floor, first_floor.entrance) + var returned_surface: FloorState = ascent["floor"] + returned_surface.map.set_cell(Vector2i(4, 7), GridMapStateType.CellKind.FLOOR) + var second_descent := transitions.transition(run, returned_surface, returned_surface.exit) + var cached_first_floor: FloorState = second_descent["floor"] + + assert_same(returned_surface, surface) + assert_eq(ascent["arrival"], surface.exit) + assert_same(cached_first_floor, first_floor) + assert_eq(second_descent["arrival"], first_floor.entrance) + assert_eq(cached_first_floor.chests[Vector2i(3, 3)], {"opened": true}) + assert_eq(cached_first_floor.enemies.size(), 0) + assert_true(cached_first_floor.events.has({"id": "first-floor-event", "resolved": true})) + assert_eq(surface.map.get_cell(Vector2i(4, 7)), GridMapStateType.CellKind.FLOOR) + +func _seed_value(seed: String, floor_number: int) -> int: + var value := 0 + for index in range(seed.length()): + value = (value * 31 + seed.unicode_at(index)) % 2147483647 + return value + floor_number diff --git a/tests/unit/test_surface_floor_transition.gd.uid b/tests/unit/test_surface_floor_transition.gd.uid new file mode 100644 index 0000000..007afcc --- /dev/null +++ b/tests/unit/test_surface_floor_transition.gd.uid @@ -0,0 +1 @@ +uid://20vpsc7klakm