From 4ba07b93958886380d251b5f11043e53dbfffc49 Mon Sep 17 00:00:00 2001 From: goose Date: Wed, 30 Sep 2026 20:21:43 -0400 Subject: [PATCH] fix: apply map-aware FOV and align actor rendering --- README.md | 10 ++++++-- scripts/domain/game_state.gd | 26 ++++++++++++++++++--- scripts/presentation/actor_view.gd | 2 +- tests/e2e/test_walking_skeleton_e2e.gd | 23 ++++++++++++++++++ tests/unit/test_turn_resolver.gd | 32 ++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2c39d64..edaf435 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Godot 4.2+ (verified with Godot 4.5) is required. GUT **9.5.0** is vendored and ## Run ```bash -GODOT_BIN=/snap/bin/godot4 "$GODOT_BIN" --path . +"${GODOT_BIN:-$(command -v godot4 || command -v godot)}" --path . ``` Use WASD / arrow keys (and QEZC diagonals). The deterministic public E2E seam is `Game.submit_test_action(Vector2i)`; it calls the same action processor as keyboard input. @@ -17,5 +17,11 @@ GODOT_BIN=/snap/bin/godot4 ./scripts/verify.sh ``` Scripts use `GODOT_BIN`, otherwise discover `godot4`, then `godot`, and run headlessly. +## Correctif et revue +- La FOV est désormais calculée contre la carte : les murs et portes fermées restent visibles mais masquent les cases situées derrière eux. +- Le positionnement des vues d’acteurs est aligné sur l’origine des tuiles. +- Les chemins d’entrée clavier et de test E2E sont vérifiés sur le même traitement d’action ; la réponse `enemy_moved` est couverte. +- Vérification effectuée : `./scripts/test-unit.sh` (7/7), `./scripts/test-e2e.sh` (2/2) et `./scripts/verify.sh` (unitaires 7/7, E2E 2/2). + ## Limits -This slice deliberately provides one compact map, one deterministic goblin, direct pursuit, no persistence, inventory, FOV occlusion, audio, or animation. +This slice deliberately provides one compact map, one deterministic goblin, direct pursuit, no persistence, inventory, audio, or animation. diff --git a/scripts/domain/game_state.gd b/scripts/domain/game_state.gd index 1d58a2e..ace3001 100644 --- a/scripts/domain/game_state.gd +++ b/scripts/domain/game_state.gd @@ -20,16 +20,36 @@ func try_player_action(direction: Vector2i) -> Array[TurnResult]: update_exploration() return results -func compute_visible_cells(origin: Vector2i, radius: int) -> Dictionary[Vector2i, bool]: +func compute_visible_cells(game_map: GridMapState, origin: Vector2i, radius: int) -> Dictionary[Vector2i, bool]: var visible: Dictionary[Vector2i, bool] = {} for y in range(origin.y - radius, origin.y + radius + 1): for x in range(origin.x - radius, origin.x + radius + 1): var cell := Vector2i(x, y) - if map.is_inside(cell) and origin.distance_to(cell) <= radius: + if game_map.is_inside(cell) and origin.distance_to(cell) <= radius and _has_line_of_sight(game_map, origin, cell): visible[cell] = true return visible func update_exploration() -> void: - visible_cells = compute_visible_cells(player.cell, 6) + visible_cells = compute_visible_cells(map, player.cell, 6) for cell in visible_cells: explored_cells[cell] = true + +func _has_line_of_sight(game_map: GridMapState, origin: Vector2i, target: Vector2i) -> bool: + var current := origin + var delta_x: int = abs(target.x - origin.x) + var step_x: int = 1 if origin.x < target.x else -1 + var delta_y: int = -abs(target.y - origin.y) + var step_y: int = 1 if origin.y < target.y else -1 + var error: int = delta_x + delta_y + + while current != target: + var doubled_error: int = 2 * error + if doubled_error >= delta_y: + error += delta_y + current.x += step_x + if doubled_error <= delta_x: + error += delta_x + current.y += step_y + if current != target and game_map.is_opaque(current): + return false + return true diff --git a/scripts/presentation/actor_view.gd b/scripts/presentation/actor_view.gd index bf9b4fc..b620f20 100644 --- a/scripts/presentation/actor_view.gd +++ b/scripts/presentation/actor_view.gd @@ -10,4 +10,4 @@ func set_actor(new_actor: ActorState) -> void: func refresh() -> void: if actor != null: - position = Vector2(actor.cell * TILE_SIZE) + Vector2(TILE_SIZE) / 2.0 + position = Vector2(actor.cell) * Vector2(TILE_SIZE) diff --git a/tests/e2e/test_walking_skeleton_e2e.gd b/tests/e2e/test_walking_skeleton_e2e.gd index ab25f6e..b22ed20 100644 --- a/tests/e2e/test_walking_skeleton_e2e.gd +++ b/tests/e2e/test_walking_skeleton_e2e.gd @@ -8,6 +8,29 @@ func before_each() -> void: add_child_autofree(game) await get_tree().process_frame +func test_configured_input_event_uses_the_same_action_path_as_submit_test_action() -> void: + var input_event := InputEventKey.new() + input_event.physical_keycode = KEY_DOWN + input_event.pressed = true + game._unhandled_input(input_event) + + assert_eq(game.get_game_state().player.cell, Vector2i(2, 3)) + assert_eq(game.get_node("HUD/EventLog").get_parsed_text(), "You move to (2, 3).\nThe goblin moves.\n") + + var submitted_game := GameScene.instantiate() as Game + add_child_autofree(submitted_game) + await get_tree().process_frame + var test_results := submitted_game.submit_test_action(Vector2i.DOWN) + + assert_eq(submitted_game.get_game_state().player.cell, Vector2i(2, 3)) + assert_eq(test_results.size(), 2) + assert_eq(test_results[0].kind, &"moved") + assert_eq(test_results[0].from_cell, Vector2i(2, 2)) + assert_eq(test_results[0].to_cell, Vector2i(2, 3)) + assert_eq(test_results[1].kind, &"enemy_moved") + assert_eq(test_results[1].from_cell, Vector2i(4, 2)) + assert_eq(test_results[1].to_cell, Vector2i(3, 2)) + func test_real_game_scene_completes_walking_combat_and_defeat_route() -> void: assert_not_null(game.get_node_or_null("World/MapRenderer")) assert_not_null(game.get_node_or_null("World/Player")) diff --git a/tests/unit/test_turn_resolver.gd b/tests/unit/test_turn_resolver.gd index 20f8c55..f5fb59d 100644 --- a/tests/unit/test_turn_resolver.gd +++ b/tests/unit/test_turn_resolver.gd @@ -53,3 +53,35 @@ func test_lethal_melee_removes_enemy_and_reports_defeat() -> void: assert_eq(results[0].kind, &"melee") assert_eq(results[1].kind, &"enemy_defeated") assert_false(state.enemies.has(&"goblin")) + +func test_non_adjacent_enemy_moves_toward_player() -> void: + var state := make_state() + var results := state.try_player_action(Vector2i.DOWN) + assert_eq(results[0].kind, &"moved") + assert_eq(results[1].kind, &"enemy_moved") + assert_eq(results[1].from_cell, Vector2i(4, 2)) + assert_eq(results[1].to_cell, Vector2i(3, 2)) + assert_eq(state.enemies[&"goblin"].cell, Vector2i(3, 2)) + +func test_compute_visible_cells_keeps_opaque_target_but_hides_cells_behind_it() -> void: + var state := make_state() + var sight_map := GridMapState.new(Vector2i(7, 5), GridMapState.CellKind.FLOOR) + var opaque_cell := Vector2i(3, 2) + var hidden_cell := Vector2i(4, 2) + sight_map.set_cell(opaque_cell, GridMapState.CellKind.WALL) + var visible := state.compute_visible_cells(sight_map, Vector2i(1, 2), 5) + assert_true(visible.has(opaque_cell)) + assert_false(visible.has(hidden_cell)) + assert_true(visible.has(Vector2i(4, 1))) + +func test_update_exploration_uses_map_aware_fov() -> void: + var map := GridMapState.new(Vector2i(7, 5), GridMapState.CellKind.FLOOR) + var opaque_cell := Vector2i(3, 2) + var hidden_cell := Vector2i(4, 2) + map.set_cell(opaque_cell, GridMapState.CellKind.CLOSED_DOOR) + var player := ActorState.new(&"player", Vector2i(1, 2), 12, 3) + var state := GameState.new(map, player, {}) + assert_true(state.visible_cells.has(opaque_cell)) + assert_false(state.visible_cells.has(hidden_cell)) + assert_true(state.explored_cells.has(opaque_cell)) + assert_false(state.explored_cells.has(hidden_cell))