Files

110 lines
5.1 KiB
GDScript

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_f_starts_ranged_targeting_and_ctrl_s_or_v_requests_a_manual_save() -> void:
assert_true(InputMap.action_has_event(&"ranged", _key_event(KEY_F)))
assert_true(InputMap.action_has_event(&"save", _key_event(KEY_S, true)))
assert_true(InputMap.action_has_event(&"save", _key_event(KEY_V)))
assert_false(InputMap.has_action(&"attack"), "Melee is resolved by moving into an adjacent enemy, not by a conflicting F action.")
var router := ActionRouter.new(null)
assert_eq(router.action_from_input(_action_event(&"ranged")).kind, &"ranged")
assert_eq(router.action_from_input(_action_event(&"save")).kind, &"save")
func test_character_opens_with_physical_c_while_p_is_free_and_southeast_uses_keypad_3() -> void:
var character_c := _key_event(KEY_C)
var legacy_character_p := _key_event(KEY_P)
var southeast_keypad_3 := _key_event(KEY_KP_3)
assert_true(InputMap.action_has_event(&"character", character_c), "C is the physical character-sheet binding")
assert_false(InputMap.action_has_event(&"character", legacy_character_p), "P must no longer open the character sheet")
assert_true(InputMap.action_has_event(&"move_southeast", southeast_keypad_3), "South-east is bound to keypad 3")
assert_false(InputMap.action_has_event(&"move_southeast", character_c), "C must no longer move south-east")
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],
&"ranged": [&"ranged", Vector2i.ZERO],
&"save": [&"save", 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_inventory_action_opens_the_inventory_panel() -> 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, &"inventory_opened")
assert_eq(events[0].payload, {"item_count": 0})
assert_eq(events[0].turn, 1)
assert_eq(game.get_run_state().recent_events(1)[0], events[0])
assert_true((game.get_node("HUD/InventoryPanel") as PanelContainer).visible)
assert_eq(game.get_game_state().player.cell, Vector2i(4, 7))
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, ctrl_pressed: bool = false) -> InputEventKey:
var event := InputEventKey.new()
event.device = 16
event.keycode = keycode
event.physical_keycode = keycode
event.ctrl_pressed = ctrl_pressed
event.pressed = true
return event