From 4d95b7d9d1f5ba8511204226c90e3bbde858eb5a Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Fri, 23 Feb 2024 21:32:34 +0000 Subject: [PATCH] Clean up player input handling --- content/scripts/player.gd | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/content/scripts/player.gd b/content/scripts/player.gd index 3cb68d4..0643c63 100644 --- a/content/scripts/player.gd +++ b/content/scripts/player.gd @@ -60,12 +60,8 @@ enum MovementType { VERTICAL, LATERAL } ## accurate. 4 is a decent number for low poly terrain! @export var max_iteration_count := 4 -var jump_pressed := false - var gravity := 9.8 var _velocity: Vector3 = Vector3() -var at_max_speed := true - var grounded: bool = false var ground_normal: Vector3 var steep_slope_normals: Array[Vector3] = [] @@ -109,18 +105,12 @@ func _input(event: InputEvent) -> void: lock_mouse() -# TODO should this be in unhandled input? -# TODO this should return void and just set the appropriate fields (input_dir!!) -# Input buffering? lol lmao -func get_input() -> Vector2: +func get_move_dir() -> Vector2: # We don't want to be moving if the mouse isn't captured! if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED: - return Vector2(0, 0) + return Vector2.ZERO - jump_pressed = Input.is_action_just_pressed("cc_jump") - at_max_speed = Input.is_action_pressed("cc_sprint") - - var input_dir: Vector2 = Vector2() + var input_dir := Vector2.ZERO if Input.is_action_pressed("cc_forward"): input_dir += Vector2.UP if Input.is_action_pressed("cc_backward"): @@ -427,21 +417,21 @@ func _vert(value: Vector3) -> Vector3: func _on_grounded_state_physics_processing(delta: float) -> void: - var move_speed := max_speed if at_max_speed else slow_speed - var target_velocity_h := get_input() * move_speed + var move_speed := max_speed if Input.is_action_pressed("cc_sprint") else slow_speed + var target_velocity_h := get_move_dir() * move_speed var target_velocity_v := -gravity * delta var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y) move(target_velocity, delta) - if jump_pressed: + if Input.is_action_just_pressed("cc_jump"): state_chart.send_event("Jump") elif !grounded: state_chart.send_event("Airborne") func _on_falling_state_physics_processing(delta: float) -> void: - var move_speed := max_speed if at_max_speed else slow_speed - var target_velocity_h := get_input() * move_speed + var move_speed := max_speed if Input.is_action_pressed("cc_sprint") else slow_speed + var target_velocity_h := get_move_dir() * move_speed var target_velocity_v := _velocity.y - gravity * delta var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y) move(target_velocity, delta) @@ -451,8 +441,8 @@ func _on_falling_state_physics_processing(delta: float) -> void: func _on_jumping_state_physics_processing(delta: float) -> void: - var move_speed := max_speed if at_max_speed else slow_speed - var target_velocity_h := get_input() * move_speed + var move_speed := max_speed if Input.is_action_pressed("cc_sprint") else slow_speed + var target_velocity_h := get_move_dir() * move_speed var target_velocity_v := jump_speed var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y) move(target_velocity, delta)