Clean up player input handling

This commit is contained in:
Jarrod Doyle 2024-02-23 21:32:34 +00:00
parent e3f652e6b1
commit 4d95b7d9d1
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 10 additions and 20 deletions

View File

@ -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)