]> Untitled Git - frog-ninja.git/blob - player/input/InputHandler.gd
Refactoring
[frog-ninja.git] / player / input / InputHandler.gd
1 extends Node
2 class_name InputHandler
3
4
5 var camera_input_method := CameraInput.MOUSE
6 var camera_input_direction := Vector2.ZERO
7
8
9 func _unhandled_input(event: InputEvent) -> void:
10         # If user clicks on the window, capture the mouse and direct the camera with it
11         if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
12                 return
13
14         if event is InputEventMouseMotion:
15                 camera_input_method = CameraInput.MOUSE
16                 camera_input_direction = event.screen_relative
17         elif event is InputEventJoypadMotion:
18                 # TODO: add these settings!
19                 camera_input_method = CameraInput.JOYSTICK
20                 camera_input_direction = Input.get_vector(
21                         "camera-left", "camera-right", "camera-up", "camera-down"
22                         )
23
24
25 func get_player_input() -> InputPacket:
26         var p: InputPacket = InputPacket.new()
27         
28         p.camera_input_method = camera_input_method
29         if camera_input_method == CameraInput.MOUSE:
30                 # TODO: clumsy handling of last relative mouse event!
31                 if Input.get_last_mouse_velocity().length() > 0.05:
32                         p.camera_input_direction = camera_input_direction
33         
34         p.player_movement_direction = Input.get_vector(
35                 "player-left", "player-right", "player-forward", "player-backward"
36                 )
37         if p.player_movement_direction != Vector2.ZERO:
38                 p.player_actions.append("walk")
39         
40         if Input.is_action_just_pressed("player-dash"):
41                 p.player_actions.append("dash")
42         
43         if Input.is_action_just_pressed("player-slash"):
44                 p.player_actions.append("slash")
45         
46         if Input.is_action_just_pressed("player-shoot"):
47                 p.player_actions.append("shoot")
48         
49         if p.player_actions.is_empty():
50                 p.player_actions.append("idle")
51                 
52         return p