extends Node class_name InputHandler ## The base class for managing all inputs ## ## On request, collects and sorts all meaningful inputs for the model to process. ## It is not aware of external modifiers for input, such as camera; inputs must be ## translated after they are collected. ## var _camera_input_method := CameraInput.MOUSE var _camera_input_direction := Vector2.ZERO 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 if event is InputEventMouseMotion: _camera_input_method = CameraInput.MOUSE _camera_input_direction = event.screen_relative elif event is InputEventJoypadMotion: _camera_input_method = CameraInput.JOYSTICK _camera_input_direction = Input.get_vector( "camera-left", "camera-right", "camera-up", "camera-down" ) ## Returns a packet of input information, including all player inputs and camera inputs. ## This layer translates digital input settings into meaningful actions for ## the player model, and also collects analogue input vectors func get_player_input() -> InputPacket: var p: InputPacket = InputPacket.new() 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 elif _camera_input_method == CameraInput.JOYSTICK: p.camera_input_direction = _camera_input_direction p.player_movement_direction = Input.get_vector( "player-left", "player-right", "player-forward", "player-backward" ) if p.player_movement_direction != Vector2.ZERO: p.player_actions.append("Walk") if Input.is_action_just_pressed("player-dash"): p.player_actions.append("Dash") if Input.is_action_just_pressed("player-slash"): p.player_combat_actions.append("slash") if Input.is_action_just_pressed("player-shoot"): p.player_combat_actions.append("shoot") if p.player_actions.is_empty(): p.player_actions.append("Idle") return p