class_name InputHandler
-var camera: Camera3D
+var camera_input_method := CameraInput.MOUSE
+var camera_input_direction := Vector2.ZERO
-func get_camera_input_direction() -> Vector2:
- return Input.get_vector(
- "camera-left", "camera-right", "camera-up", "camera-down"
- )
-
+func _unhandled_input(event: InputEvent) -> void:
+ # If user clicks on the window, capture the mouse and direct the camera with it
+ if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
+ return
-# Get the XZ input direction based on player's input relative to the camera
-func get_player_input_relative_to_camera(raw_input: Vector2) -> Vector3:
- if camera:
- var forward := camera.global_basis.z
- var right := camera.global_basis.x
- var dir := (
- forward * raw_input.y + right * raw_input.x
- ).normalized()
- return dir
- else:
- return Vector3.ZERO
+ if event is InputEventMouseMotion:
+ camera_input_method = CameraInput.MOUSE
+ camera_input_direction = event.screen_relative
+ elif event is InputEventJoypadMotion:
+ # TODO: add these settings!
+ camera_input_method = CameraInput.JOYSTICK
+ camera_input_direction = Input.get_vector(
+ "camera-left", "camera-right", "camera-up", "camera-down"
+ )
func get_player_input() -> InputPacket:
var p: InputPacket = InputPacket.new()
-
- p.movement_direction = Input.get_vector(
+
+ p.camera_input_method = camera_input_method
+ if camera_input_method == CameraInput.MOUSE:
+ # TODO: clumsy handling of last relative mouse event!
+ if Input.get_last_mouse_velocity().length() > 0.05:
+ p.camera_input_direction = camera_input_direction
+
+ p.player_movement_direction = Input.get_vector(
"player-left", "player-right", "player-forward", "player-backward"
)
- p.camera_movement_direction = get_player_input_relative_to_camera(
- p.movement_direction
- )
- if p.movement_direction != Vector2.ZERO:
- p.actions.append("walk")
+ if p.player_movement_direction != Vector2.ZERO:
+ p.player_actions.append("walk")
if Input.is_action_just_pressed("player-dash"):
- p.actions.append("dash")
+ p.player_actions.append("dash")
if Input.is_action_just_pressed("player-slash"):
- p.actions.append("slash")
+ p.player_actions.append("slash")
if Input.is_action_just_pressed("player-shoot"):
- p.actions.append("shoot")
+ p.player_actions.append("shoot")
- if p.actions.is_empty():
- p.actions.append("idle")
+ if p.player_actions.is_empty():
+ p.player_actions.append("idle")
return p