2 class_name InputHandler
3 ## The base class for managing all inputs
5 ## On request, collects and sorts all meaningful inputs for the model to process.
6 ## It is not aware of external modifiers for input, such as camera; inputs must be
7 ## translated after they are collected.
10 var _camera_input_method := CameraInput.MOUSE
11 var _camera_input_direction := Vector2.ZERO
14 func _unhandled_input(event: InputEvent) -> void:
15 # If user clicks on the window, capture the mouse and direct the camera with it
16 if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
19 if event is InputEventMouseMotion:
20 _camera_input_method = CameraInput.MOUSE
21 _camera_input_direction = event.screen_relative
22 elif event is InputEventJoypadMotion:
23 _camera_input_method = CameraInput.JOYSTICK
24 _camera_input_direction = Input.get_vector(
25 "camera-left", "camera-right", "camera-up", "camera-down"
28 ## Returns a packet of input information, including all player inputs and camera inputs.
29 ## This layer translates digital input settings into meaningful actions for
30 ## the player model, and also collects analogue input vectors
31 func get_player_input() -> InputPacket:
32 var p: InputPacket = InputPacket.new()
34 p.camera_input_method = _camera_input_method
35 if _camera_input_method == CameraInput.MOUSE:
36 # TODO: clumsy handling of last relative mouse event!
37 if Input.get_last_mouse_velocity().length() > 0.05:
38 p.camera_input_direction = _camera_input_direction
39 elif _camera_input_method == CameraInput.JOYSTICK:
40 p.camera_input_direction = _camera_input_direction
42 p.player_movement_direction = Input.get_vector(
43 "player-left", "player-right", "player-forward", "player-backward"
45 if p.player_movement_direction != Vector2.ZERO:
46 p.player_actions.append("Walk")
48 if Input.is_action_just_pressed("player-dash"):
49 p.player_actions.append("Dash")
51 if Input.is_action_just_pressed("player-slash"):
52 p.player_combat_actions.append("slash")
54 if Input.is_action_just_pressed("player-shoot"):
55 p.player_combat_actions.append("shoot")
57 if p.player_actions.is_empty():
58 p.player_actions.append("Idle")