extends TestCase ## A13: every in-game action works from a gamepad, driven here by simulated pad ## events through ClientInput exactly as the session passes them on. var input: ClientInput func before_each() -> void: super.before_each() input = ClientInput.new() static func button(index: JoyButton, pressed: bool = true) -> InputEventJoypadButton: var event: InputEventJoypadButton = InputEventJoypadButton.new() event.button_index = index event.pressed = pressed return event static func axis(which: JoyAxis, value: float) -> InputEventJoypadMotion: var event: InputEventJoypadMotion = InputEventJoypadMotion.new() event.axis = which event.axis_value = value return event func _tap(index: JoyButton) -> void: input.handle(button(index, true), false) input.handle(button(index, false), false) func _command() -> PlayerCommand: input.poll(1.0 / 60.0) return input.command(1) func test_the_left_stick_moves_with_a_dead_zone() -> void: input.handle(axis(JOY_AXIS_LEFT_Y, -0.1), false) input.poll(0.016) assert_eq(input.move, Vector2.ZERO, "inside the dead zone") input.handle(axis(JOY_AXIS_LEFT_Y, -1.0), false) input.poll(0.016) assert_near(input.move.y, 1.0, 0.001, "stick up walks forward") input.handle(axis(JOY_AXIS_LEFT_Y, 0.0), false) input.handle(axis(JOY_AXIS_LEFT_X, 0.6), false) input.poll(0.016) assert_true(input.move.x > 0.4 and input.move.x < 0.6, "part way right is part speed: %s" % input.move) assert_near(input.move.y, 0.0, 0.001) func test_every_held_action_has_a_button_or_trigger() -> void: var cases: Array[Array] = [ [button(JOY_BUTTON_A), PlayerCommand.BUTTON_JUMP], [button(JOY_BUTTON_B), PlayerCommand.BUTTON_CROUCH], [button(JOY_BUTTON_X), PlayerCommand.BUTTON_RELOAD], [button(JOY_BUTTON_Y), PlayerCommand.BUTTON_INTERACT], [button(JOY_BUTTON_DPAD_DOWN), PlayerCommand.BUTTON_DROP], [axis(JOY_AXIS_TRIGGER_RIGHT, 1.0), PlayerCommand.BUTTON_FIRE], [axis(JOY_AXIS_TRIGGER_LEFT, 1.0), PlayerCommand.BUTTON_AIM], ] for case: Array in cases: input = ClientInput.new() input.handle(case[0], false) input.poll(0.016) assert_eq(input.buttons, int(case[1]), "%s holds %d" % [(case[0] as InputEvent).as_text(), case[1]]) # A trigger only lightly touched isn't held. input = ClientInput.new() input.handle(axis(JOY_AXIS_TRIGGER_RIGHT, 0.2), false) input.poll(0.016) assert_eq(input.buttons, 0) func test_clicking_the_left_stick_sprints_until_you_stop() -> void: input.handle(axis(JOY_AXIS_LEFT_Y, -1.0), false) _tap(JOY_BUTTON_LEFT_STICK) assert_true(_command().buttons & PlayerCommand.BUTTON_SPRINT != 0, "sprinting") assert_true(_command().buttons & PlayerCommand.BUTTON_SPRINT != 0, "still sprinting") input.handle(axis(JOY_AXIS_LEFT_Y, 0.0), false) assert_eq(_command().buttons & PlayerCommand.BUTTON_SPRINT, 0, "stopping ends the sprint") input.handle(axis(JOY_AXIS_LEFT_Y, -1.0), false) assert_eq(_command().buttons & PlayerCommand.BUTTON_SPRINT, 0, "walking again doesn't sprint") func test_bumpers_pick_slots_and_pieces_and_the_dpad_the_material() -> void: _tap(JOY_BUTTON_RIGHT_SHOULDER) assert_eq(input.slot, 1) _tap(JOY_BUTTON_LEFT_SHOULDER) _tap(JOY_BUTTON_LEFT_SHOULDER) assert_eq(input.slot, Inventory.slot_count(), "wraps round to the last slot") _tap(JOY_BUTTON_RIGHT_STICK) assert_true(input.build_mode, "clicking the right stick enters build mode") _tap(JOY_BUTTON_RIGHT_SHOULDER) assert_eq(input.build_kind, StructureSet.FLOOR) _tap(JOY_BUTTON_LEFT_SHOULDER) _tap(JOY_BUTTON_LEFT_SHOULDER) assert_eq(input.build_kind, StructureSet.KINDS.size() - 1) _tap(JOY_BUTTON_DPAD_RIGHT) assert_eq(input.material, Materials.STONE) _tap(JOY_BUTTON_DPAD_LEFT) _tap(JOY_BUTTON_DPAD_LEFT) assert_eq(input.material, Materials.NAMES.size() - 1) var command: PlayerCommand = _command() assert_true(command.buttons & PlayerCommand.BUTTON_BUILD != 0) _tap(JOY_BUTTON_RIGHT_STICK) assert_false(input.build_mode) _tap(JOY_BUTTON_DPAD_UP) assert_true(input.map_open, "D-pad up opens the chart") _tap(JOY_BUTTON_DPAD_UP) assert_false(input.map_open) func test_a_held_button_acts_once() -> void: input.handle(button(JOY_BUTTON_RIGHT_SHOULDER, true), false) input.handle(button(JOY_BUTTON_RIGHT_SHOULDER, true), false) assert_eq(input.slot, 1, "a repeat of a held button isn't a second press") func test_the_right_stick_turns_by_time_and_settings() -> void: input.handle(axis(JOY_AXIS_RIGHT_X, 1.0), false) input.poll(0.5) var turned: float = angle_difference(input.yaw, 0.0) assert_near(turned, PadInput.LOOK_RADIANS_PER_SECOND * 0.5, 0.001, "full right turns right at the full rate") input = ClientInput.new() input.settings.pad_sensitivity = 2.0 input.handle(axis(JOY_AXIS_RIGHT_X, 1.0), false) input.poll(0.25) assert_near(angle_difference(input.yaw, 0.0), PadInput.LOOK_RADIANS_PER_SECOND * 0.5, 0.001, "twice the speed") input = ClientInput.new() input.handle(axis(JOY_AXIS_RIGHT_Y, -1.0), false) input.poll(0.1) assert_true(input.pitch > 0.0, "stick up looks up") input.settings.invert_y = true input.poll(0.1) assert_near(input.pitch, 0.0, 0.0001, "inverted, it looks back down") # A small push turns much less than in proportion (the response curve). input = ClientInput.new() input.handle(axis(JOY_AXIS_RIGHT_X, 0.5), false) input.poll(1.0) assert_true(angle_difference(input.yaw, 0.0) < PadInput.LOOK_RADIANS_PER_SECOND * 0.35) func test_the_pad_is_active_until_the_keyboard_is_used() -> void: assert_false(input.pad.active) _tap(JOY_BUTTON_A) assert_true(input.pad.active) var key: InputEventKey = InputEventKey.new() key.physical_keycode = KEY_W key.pressed = true input.handle(key, true) assert_false(input.pad.active) func test_release_forgets_what_was_held() -> void: input.handle(axis(JOY_AXIS_TRIGGER_RIGHT, 1.0), false) input.handle(button(JOY_BUTTON_A), false) input.pad.release() input.poll(0.016) assert_eq(input.buttons, 0) func test_every_layout_entry_names_a_real_input() -> void: for action: String in ["jump", "crouch", "reload", "interact", "fire", "aim", "sprint", "build", "map", "drop", "material", "slot_next", "pause", "forward"]: assert_ne(PadInput.describe(action), "?", action) var named: Array[String] = [] for pair: Array in PadInput.LAYOUT: named.append(String(pair[1])) for which: String in PadInput.NAMES.values(): assert_true(named.has(which) or which == "D-pad right" or which == "D-pad left", "%s is in the listed layout" % which) func test_menus_accept_and_go_back_on_the_pad() -> void: PadInput.install_menu_actions() PadInput.install_menu_actions() var accept: int = 0 for event: InputEvent in InputMap.action_get_events("ui_accept"): if event is InputEventJoypadButton and (event as InputEventJoypadButton).button_index == JOY_BUTTON_A: accept += 1 assert_eq(accept, 1, "South chooses, added once") assert_true(button(JOY_BUTTON_B).is_action_pressed("ui_cancel"), "East goes back") assert_true(button(JOY_BUTTON_START).is_action_pressed("ui_cancel"), "Start goes back") assert_true(button(JOY_BUTTON_DPAD_DOWN).is_action_pressed("ui_down"), "the D-pad moves the focus") assert_true(axis(JOY_AXIS_LEFT_Y, 1.0).is_action_pressed("ui_down"), "so does the left stick")