diff --git a/README.md b/README.md index 1326cd0..62c240d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Godot 4.2+ is required (verified with Godot 4.5). GUT **9.5.0** is vendored and "${GODOT_BIN:-$(command -v godot4 || command -v godot)}" --path . ``` -Use WASD / arrow keys (and QZC diagonals). The HUD help band uses the French labels `Déplacer : ZQSD / Flèches` and `Interagir : Espace ou E`; both **Space** and **E** are bound to `interact`. `Game.submit_test_action(Vector2i)` and `Game.submit_test_interact()` are deterministic public E2E seams and follow the same processors as keyboard input. Interacting with an adjacent closed door opens it into floor space, logs the action, refreshes FOV, and gives living enemies exactly one normal response. A closed chest is likewise non-walkable; interacting with it replaces it with floor, logs `chest_opened` as `You open the chest.`, refreshes FOV, and gives at most one normal enemy response. It deliberately grants no loot or inventory item. Interacting with no target only logs `nothing_to_interact` and does not consume an enemy turn. Interacting while standing on stairs logs `stairs_used`, sets the demonstration exit state, and gives living enemies exactly one normal response. +Use WASD / arrow keys (and QZC diagonals). The HUD help band uses the French labels `Déplacer : ZQSD / Flèches` and `Interagir : Espace ou E`; both **Space** and **E** are bound to `interact`. All inputs pass through `ActionRouter`: movement and interaction are implemented, while the registered future commands emit a journaled `action_unavailable` event rather than being ignored. The rebindable command vocabulary and defaults are: **.** wait, **F** attack, **R** ranged, **X** cast, **T** talk, **H** rest, **I** inventory, **P** character, **J** quests, **U** audience, **Y** recap, **M** map, and **O** options. `Game.submit_test_action(Vector2i)` and `Game.submit_test_interact()` are deterministic public E2E seams and follow the same action router as keyboard input. Interacting with an adjacent closed door opens it into floor space, logs the action, refreshes FOV, and gives living enemies exactly one normal response. A closed chest is likewise non-walkable; interacting with it replaces it with floor, logs `chest_opened` as `You open the chest.`, refreshes FOV, and gives at most one normal enemy response. It deliberately grants no loot or inventory item. Interacting with no target only logs `nothing_to_interact` and does not consume an enemy turn. Interacting while standing on stairs logs `stairs_used`, sets the demonstration exit state, and gives living enemies exactly one normal response. ## Display, grid, and camera diff --git a/project.godot b/project.godot index d5fac00..7e8b42b 100644 --- a/project.godot +++ b/project.godot @@ -33,3 +33,16 @@ move_northwest={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode" move_northeast={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":69)]} move_southwest={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":90)]} move_southeast={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":67)]} +wait={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":46)]} +attack={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":70)]} +ranged={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":82)]} +cast={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":88)]} +talk={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":84)]} +rest={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":72)]} +inventory={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":73)]} +character={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":80)]} +quest={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":74)]} +audience={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":85)]} +recap={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":89)]} +map={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":77)]} +options={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":79)]} diff --git a/scripts/game/action_router.gd b/scripts/game/action_router.gd new file mode 100644 index 0000000..3b9114a --- /dev/null +++ b/scripts/game/action_router.gd @@ -0,0 +1,24 @@ +class_name ActionRouter +extends RefCounted + +const InputBindingsType = preload("res://scripts/game/input_bindings.gd") +const PlayerActionType = preload("res://scripts/domain/run/player_action.gd") + +var _game: Game + +func _init(game: Game) -> void: + _game = game + +func action_from_input(event: InputEvent) -> PlayerAction: + for input_action in InputBindingsType.input_actions(): + if event.is_action_pressed(input_action): + return PlayerActionType.new( + InputBindingsType.player_action_for(input_action), + InputBindingsType.direction_for(input_action) + ) + return null + +func submit(action: PlayerAction) -> Array[GameEvent]: + if _game == null: + return [] + return _game.submit_player_action(action) diff --git a/scripts/game/action_router.gd.uid b/scripts/game/action_router.gd.uid new file mode 100644 index 0000000..c8d49dd --- /dev/null +++ b/scripts/game/action_router.gd.uid @@ -0,0 +1 @@ +uid://ce4dlu81er111 diff --git a/scripts/game/game.gd b/scripts/game/game.gd index 8090e36..ca71fff 100644 --- a/scripts/game/game.gd +++ b/scripts/game/game.gd @@ -8,6 +8,8 @@ const ActorStateType = preload("res://scripts/domain/actor_state.gd") const GameStateType = preload("res://scripts/domain/game_state.gd") const RunStateType = preload("res://scripts/domain/run/run_state.gd") 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") @onready var event_presenter: EventPresenter = $EventPresenter @onready var map_renderer: MapRenderer = $World/MapRenderer @@ -20,10 +22,12 @@ var game_state: GameState var run_state: RunState var enemy_views: Dictionary[StringName, ActorView] = {} var last_turn_results: Array[TurnResult] = [] +var action_router: ActionRouter func _ready() -> void: game_state = _create_initial_state() run_state = RunStateType.new("demo-run") + action_router = ActionRouterType.new(self) event_presenter.set_actor_lookup(_get_actor_view) event_presenter.event_presented.connect(_on_event_presented) map_renderer.set_map(game_state.map) @@ -66,30 +70,47 @@ func _prepare_exit_capture_state() -> void: _sync_camera_to_player() func _unhandled_input(event: InputEvent) -> void: - if event.is_action_pressed(&"interact"): - last_turn_results = submit_test_interact() - elif event.is_action_pressed(&"move_north"): - last_turn_results = _process_action(Vector2i.UP) - elif event.is_action_pressed(&"move_south"): - last_turn_results = _process_action(Vector2i.DOWN) - elif event.is_action_pressed(&"move_west"): - last_turn_results = _process_action(Vector2i.LEFT) - elif event.is_action_pressed(&"move_east"): - last_turn_results = _process_action(Vector2i.RIGHT) - elif event.is_action_pressed(&"move_northwest"): - last_turn_results = _process_action(Vector2i(-1, -1)) - elif event.is_action_pressed(&"move_northeast"): - last_turn_results = _process_action(Vector2i(1, -1)) - elif event.is_action_pressed(&"move_southwest"): - last_turn_results = _process_action(Vector2i(-1, 1)) - elif event.is_action_pressed(&"move_southeast"): - last_turn_results = _process_action(Vector2i(1, 1)) + var action := action_router.action_from_input(event) + if action != null: + action_router.submit(action) func submit_test_action(direction: Vector2i) -> Array[TurnResult]: - return _process_action(direction) + action_router.submit(PlayerActionType.new(&"move", direction)) + return last_turn_results func submit_test_interact() -> Array[TurnResult]: - return _process_interact() + action_router.submit(PlayerActionType.new(&"interact")) + return last_turn_results + +func submit_player_action(action: PlayerAction) -> Array[GameEvent]: + match action.kind: + &"move": + last_turn_results = _process_action(action.direction) + return run_state.recent_events(last_turn_results.size()) + &"interact": + last_turn_results = _process_interact() + return run_state.recent_events(last_turn_results.size()) + _: + last_turn_results = [] + return _record_unavailable_action(action) + +func _record_unavailable_action(action: PlayerAction) -> Array[GameEvent]: + var event := GameEventType.new( + run_state.next_turn(), + run_state.current_floor, + &"action_unavailable", + {"action": action.kind}, + "Action unavailable: %s." % action.kind, + &"player", + &"", + game_state.player.cell + ) + var events: Array[GameEvent] = [event] + run_state.append_events(events) + event_presenter.enqueue(events) + event_presenter.present_next() + hud.refresh(game_state, run_state.recent_events(run_state.log.size())) + return events func get_game_state() -> GameState: return game_state diff --git a/scripts/game/input_bindings.gd b/scripts/game/input_bindings.gd new file mode 100644 index 0000000..4910e80 --- /dev/null +++ b/scripts/game/input_bindings.gd @@ -0,0 +1,77 @@ +class_name InputBindings +extends RefCounted + +# The Input Map owns physical bindings; this catalog gives every rebindable action +# its stable identifier, player command and user-facing French help name. +const ACTIONS: Array[StringName] = [ + &"interact", + &"move_north", + &"move_south", + &"move_west", + &"move_east", + &"move_northwest", + &"move_northeast", + &"move_southwest", + &"move_southeast", + &"wait", + &"attack", + &"ranged", + &"cast", + &"talk", + &"rest", + &"inventory", + &"character", + &"quest", + &"audience", + &"recap", + &"map", + &"options", +] + +const HELP_NAMES := { + &"move_north": "Déplacer vers le nord", + &"move_south": "Déplacer vers le sud", + &"move_west": "Déplacer vers l’ouest", + &"move_east": "Déplacer vers l’est", + &"move_northwest": "Déplacer vers le nord-ouest", + &"move_northeast": "Déplacer vers le nord-est", + &"move_southwest": "Déplacer vers le sud-ouest", + &"move_southeast": "Déplacer vers le sud-est", + &"wait": "Attendre", + &"interact": "Interagir", + &"attack": "Attaquer", + &"ranged": "Attaque à distance", + &"cast": "Lancer un sort", + &"talk": "Parler", + &"rest": "Se reposer", + &"inventory": "Inventaire", + &"character": "Personnage", + &"quest": "Quêtes", + &"audience": "Audience", + &"recap": "Récapitulatif", + &"map": "Carte", + &"options": "Options", +} + +const MOVE_DIRECTIONS := { + &"move_north": Vector2i.UP, + &"move_south": Vector2i.DOWN, + &"move_west": Vector2i.LEFT, + &"move_east": Vector2i.RIGHT, + &"move_northwest": Vector2i(-1, -1), + &"move_northeast": Vector2i(1, -1), + &"move_southwest": Vector2i(-1, 1), + &"move_southeast": Vector2i(1, 1), +} + +static func input_actions() -> Array[StringName]: + return ACTIONS.duplicate() + +static func help_name_for(input_action: StringName) -> String: + return HELP_NAMES.get(input_action, "") + +static func direction_for(input_action: StringName) -> Vector2i: + return MOVE_DIRECTIONS.get(input_action, Vector2i.ZERO) + +static func player_action_for(input_action: StringName) -> StringName: + return &"move" if MOVE_DIRECTIONS.has(input_action) else input_action diff --git a/scripts/game/input_bindings.gd.uid b/scripts/game/input_bindings.gd.uid new file mode 100644 index 0000000..c0d2539 --- /dev/null +++ b/scripts/game/input_bindings.gd.uid @@ -0,0 +1 @@ +uid://c7e22jfm2l137 diff --git a/tests/e2e/test_input_action_router_e2e.gd b/tests/e2e/test_input_action_router_e2e.gd new file mode 100644 index 0000000..4270bb8 --- /dev/null +++ b/tests/e2e/test_input_action_router_e2e.gd @@ -0,0 +1,39 @@ +extends GutTest + +const GameScene = preload("res://scenes/Game.tscn") +var game: Game + +func before_each() -> void: + game = GameScene.instantiate() + add_child_autofree(game) + await get_tree().process_frame + +func test_real_keyboard_input_routes_movement_and_interaction_through_action_router() -> void: + var state := game.get_game_state() + var start_cell := state.player.cell + + game._unhandled_input(_key_event(KEY_SPACE)) + assert_eq(state.map.get_cell(Vector2i(4, 11)), GridMapState.CellKind.FLOOR) + assert_eq(game.get_run_state().recent_events(2)[0].kind, &"door_opened") + + game._unhandled_input(_key_event(KEY_DOWN)) + assert_eq(state.player.cell, start_cell + Vector2i.DOWN) + assert_eq(game.get_run_state().recent_events(2)[0].kind, &"moved") + +func test_real_future_command_input_records_unavailable_event_in_the_journal_and_hud() -> void: + var event_log := game.get_node("HUD/EventLog") as RichTextLabel + + game._unhandled_input(_key_event(KEY_I)) + + var event := game.get_run_state().recent_events(1)[0] + assert_eq(event.kind, &"action_unavailable") + assert_eq(event.payload, {"action": &"inventory"}) + assert_eq(event.turn, 1) + assert_string_contains(event_log.get_parsed_text(), "inventory") + +func _key_event(keycode: Key) -> InputEventKey: + var event := InputEventKey.new() + event.keycode = keycode + event.physical_keycode = keycode + event.pressed = true + return event diff --git a/tests/e2e/test_input_action_router_e2e.gd.uid b/tests/e2e/test_input_action_router_e2e.gd.uid new file mode 100644 index 0000000..b21ed34 --- /dev/null +++ b/tests/e2e/test_input_action_router_e2e.gd.uid @@ -0,0 +1 @@ +uid://ky15sj26s431 diff --git a/tests/unit/test_action_router.gd b/tests/unit/test_action_router.gd new file mode 100644 index 0000000..dd114b4 --- /dev/null +++ b/tests/unit/test_action_router.gd @@ -0,0 +1,70 @@ +extends GutTest + +const ActionRouter = preload("res://scripts/game/action_router.gd") +const InputBindings = preload("res://scripts/game/input_bindings.gd") +const PlayerAction = preload("res://scripts/domain/run/player_action.gd") +const GameScene = preload("res://scenes/Game.tscn") + +func test_every_declared_input_action_is_registered_and_has_user_facing_help() -> void: + for input_action in InputBindings.input_actions(): + assert_true(InputMap.has_action(input_action), "Missing Input Map action: %s" % input_action) + assert_gt(InputMap.action_get_events(input_action).size(), 0, "Missing keyboard binding: %s" % input_action) + assert_ne(InputBindings.help_name_for(input_action), "", "Missing help name: %s" % input_action) + +func test_action_from_input_maps_the_complete_command_vocabulary() -> void: + var router := ActionRouter.new(null) + var expected := { + &"move_north": [&"move", Vector2i.UP], + &"move_south": [&"move", Vector2i.DOWN], + &"move_west": [&"move", Vector2i.LEFT], + &"move_east": [&"move", Vector2i.RIGHT], + &"move_northwest": [&"move", Vector2i(-1, -1)], + &"move_northeast": [&"move", Vector2i(1, -1)], + &"move_southwest": [&"move", Vector2i(-1, 1)], + &"move_southeast": [&"move", Vector2i(1, 1)], + &"wait": [&"wait", Vector2i.ZERO], + &"interact": [&"interact", Vector2i.ZERO], + &"attack": [&"attack", Vector2i.ZERO], + &"ranged": [&"ranged", Vector2i.ZERO], + &"cast": [&"cast", Vector2i.ZERO], + &"talk": [&"talk", Vector2i.ZERO], + &"rest": [&"rest", Vector2i.ZERO], + &"inventory": [&"inventory", Vector2i.ZERO], + &"character": [&"character", Vector2i.ZERO], + &"quest": [&"quest", Vector2i.ZERO], + &"audience": [&"audience", Vector2i.ZERO], + &"recap": [&"recap", Vector2i.ZERO], + &"map": [&"map", Vector2i.ZERO], + &"options": [&"options", Vector2i.ZERO], + } + + for input_action in expected: + var action: PlayerAction = router.action_from_input(_action_event(input_action)) + assert_not_null(action, "No player action for %s" % input_action) + assert_eq(action.kind, expected[input_action][0], "Incorrect kind for %s" % input_action) + assert_eq(action.direction, expected[input_action][1], "Incorrect direction for %s" % input_action) + +func test_unmapped_input_returns_no_player_action() -> void: + var router := ActionRouter.new(null) + assert_null(router.action_from_input(_action_event(&"not_a_game_action"))) + +func test_submit_unimplemented_action_records_action_unavailable_event() -> void: + var game := GameScene.instantiate() as Game + add_child_autofree(game) + await get_tree().process_frame + var router := ActionRouter.new(game) + + var events: Array[GameEvent] = router.submit(PlayerAction.new(&"inventory")) + + assert_eq(events.size(), 1) + assert_eq(events[0].kind, &"action_unavailable") + assert_eq(events[0].payload, {"action": &"inventory"}) + assert_eq(events[0].turn, 1) + assert_eq(game.get_run_state().recent_events(1)[0], events[0]) + assert_eq(game.get_game_state().player.cell, Vector2i(4, 12)) + +func _action_event(input_action: StringName) -> InputEventAction: + var event := InputEventAction.new() + event.action = input_action + event.pressed = true + return event diff --git a/tests/unit/test_action_router.gd.uid b/tests/unit/test_action_router.gd.uid new file mode 100644 index 0000000..400dc78 --- /dev/null +++ b/tests/unit/test_action_router.gd.uid @@ -0,0 +1 @@ +uid://w66kxy6qx633