feat: add unified player action router
This commit is contained in:
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://w66kxy6qx633
|
||||
Reference in New Issue
Block a user