A5: Add unified player action router #16

Merged
goose merged 3 commits from slice/a5-action-router into main 2026-10-02 21:23:02 +00:00
14 changed files with 295 additions and 23 deletions
+1 -1
View File
@@ -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); **keypad 9** moves north-east. The HUD help band explicitly shows `Déplacer : ZQSD / Flèches (nord-est : pavé num. 9)` and `Interagir : Espace ou E`; both **Space** and **E** are bound only 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
+14 -1
View File
@@ -30,6 +30,19 @@ move_south={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":83)
move_west={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":65),Object(InputEventKey,"physical_keycode":4194319)]}
move_east={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":68),Object(InputEventKey,"physical_keycode":4194321)]}
move_northwest={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":81)]}
move_northeast={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":69)]}
move_northeast={"deadzone":0.5,"events":[Object(InputEventKey,"physical_keycode":4194447)]}
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)]}
+1 -1
View File
@@ -214,6 +214,6 @@ grow_horizontal = 2
grow_vertical = 0
theme_override_colors/font_color = Color(0.82, 0.9, 1, 1)
theme_override_font_sizes/font_size = 14
text = "Déplacer : ZQSD / Flèches • Interagir : Espace ou E"
text = "Déplacer : ZQSD / Flèches (nord-est : pavé num. 9) • Interagir : Espace ou E"
horizontal_alignment = 1
vertical_alignment = 1
+24
View File
@@ -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)
+1
View File
@@ -0,0 +1 @@
uid://ce4dlu81er111
+41 -20
View File
@@ -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
+77
View File
@@ -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
+1
View File
@@ -0,0 +1 @@
uid://c7e22jfm2l137
+1
View File
@@ -18,6 +18,7 @@ func test_real_game_exposes_door_and_stairs_prompts_with_help_and_event_log() ->
assert_eq(hud.get_context_prompt(), "Espace — Ouvrir la porte")
assert_eq(prompt_label.text, "Espace — Ouvrir la porte")
assert_string_contains(help_label.text, "Déplacer : ZQSD / Flèches")
assert_string_contains(help_label.text, "nord-est : pavé num. 9")
assert_string_contains(help_label.text, "Interagir : Espace ou E")
var interact_event := InputEventKey.new()
+45
View File
@@ -0,0 +1,45 @@
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_input_pipeline_routes_e_interaction_and_movement_through_action_router() -> void:
var state := game.get_game_state()
var start_cell := state.player.cell
await _dispatch_key(KEY_E)
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")
await _dispatch_key(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_input_pipeline_routes_unavailable_input_to_the_journal_and_hud() -> void:
var event_log := game.get_node("HUD/EventLog") as RichTextLabel
await _dispatch_key(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 _dispatch_key(keycode: Key) -> void:
Input.parse_input_event(_key_event(keycode, true))
await get_tree().process_frame
Input.parse_input_event(_key_event(keycode, false))
await get_tree().process_frame
func _key_event(keycode: Key, pressed: bool) -> InputEventKey:
var event := InputEventKey.new()
event.keycode = keycode
event.physical_keycode = keycode
event.pressed = pressed
return event
@@ -0,0 +1 @@
uid://ky15sj26s431
+86
View File
@@ -0,0 +1,86 @@
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_interact_and_move_northeast_have_distinct_physical_bindings() -> void:
var interact_e := _key_event(KEY_E)
var northeast_keypad_9 := _key_event(KEY_KP_9)
assert_true(InputMap.action_has_event(&"interact", interact_e))
assert_true(InputMap.action_has_event(&"move_northeast", northeast_keypad_9))
assert_false(InputMap.action_has_event(&"move_northeast", interact_e))
assert_false(InputMap.action_has_event(&"interact", northeast_keypad_9))
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
func _key_event(keycode: Key) -> InputEventKey:
var event := InputEventKey.new()
event.keycode = keycode
event.physical_keycode = keycode
event.pressed = true
return event
+1
View File
@@ -0,0 +1 @@
uid://w66kxy6qx633
+1
View File
@@ -13,6 +13,7 @@ func test_help_band_displays_the_exact_control_labels() -> void:
assert_not_null(help_label)
assert_string_contains(help_label.text, "Déplacer : ZQSD / Flèches")
assert_string_contains(help_label.text, "nord-est : pavé num. 9")
assert_string_contains(help_label.text, "Interagir : Espace ou E")
func test_hud_uses_viewport_anchored_panels_with_distinct_stats_and_log_zones() -> void: