Reorganise player controller to make better use of regions
This commit is contained in:
parent
eda51bcb50
commit
78bca9eafb
|
@ -85,17 +85,11 @@ var vertical_collisions: Array[KinematicCollision3D]
|
|||
var lateral_collisions: Array[KinematicCollision3D]
|
||||
var snap_collisions: Array[KinematicCollision3D]
|
||||
|
||||
#region Godot Functions
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
lock_mouse()
|
||||
|
||||
|
||||
func lock_mouse() -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
|
||||
func unlock_mouse() -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
_lock_mouse()
|
||||
|
||||
|
||||
# TODO should this have an action associated?
|
||||
|
@ -113,29 +107,13 @@ func _input(event: InputEvent) -> void:
|
|||
var key_event := event as InputEventKey
|
||||
if key_event.keycode == KEY_ESCAPE:
|
||||
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||||
unlock_mouse()
|
||||
_unlock_mouse()
|
||||
else:
|
||||
lock_mouse()
|
||||
_lock_mouse()
|
||||
|
||||
|
||||
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.ZERO
|
||||
|
||||
var input_dir := Vector2.ZERO
|
||||
if Input.is_action_pressed("cc_forward"):
|
||||
input_dir += Vector2.UP
|
||||
if Input.is_action_pressed("cc_backward"):
|
||||
input_dir -= Vector2.UP
|
||||
if Input.is_action_pressed("cc_left"):
|
||||
input_dir += Vector2.LEFT
|
||||
if Input.is_action_pressed("cc_right"):
|
||||
input_dir -= Vector2.LEFT
|
||||
input_dir = input_dir.normalized()
|
||||
|
||||
# Local rotation is fine given the parent isn't rotating ever
|
||||
return input_dir.rotated(-body.rotation.y)
|
||||
#endregion
|
||||
#region Core Movement
|
||||
|
||||
|
||||
# Entry point to moving
|
||||
|
@ -417,6 +395,10 @@ func snap_down() -> void:
|
|||
position = before_snap_pos
|
||||
|
||||
|
||||
#endregion
|
||||
#region Utilities
|
||||
|
||||
|
||||
func _under_slope_limit(normal: Vector3) -> bool:
|
||||
return normal.angle_to(Vector3.UP) < deg_to_rad(slope_limit)
|
||||
|
||||
|
@ -429,43 +411,40 @@ func _vert(value: Vector3) -> Vector3:
|
|||
return Vector3(0, value.y, 0)
|
||||
|
||||
|
||||
func _on_running_state_physics_processing(delta: float) -> void:
|
||||
if target_speed < run_speed:
|
||||
target_speed += (run_speed / run_acceleration_time) * delta
|
||||
else:
|
||||
# TODO Decelerate
|
||||
target_speed = run_speed
|
||||
|
||||
var target_velocity_h := get_move_dir() * target_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)
|
||||
func _lock_mouse() -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
|
||||
func _on_falling_state_physics_processing(delta: float) -> void:
|
||||
var move_speed := run_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)
|
||||
func _unlock_mouse() -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
|
||||
|
||||
func _on_jumping_state_physics_processing(delta: float) -> void:
|
||||
var move_speed := run_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)
|
||||
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.ZERO
|
||||
|
||||
var input_dir := Vector2.ZERO
|
||||
if Input.is_action_pressed("cc_forward"):
|
||||
input_dir += Vector2.UP
|
||||
if Input.is_action_pressed("cc_backward"):
|
||||
input_dir -= Vector2.UP
|
||||
if Input.is_action_pressed("cc_left"):
|
||||
input_dir += Vector2.LEFT
|
||||
if Input.is_action_pressed("cc_right"):
|
||||
input_dir -= Vector2.LEFT
|
||||
input_dir = input_dir.normalized()
|
||||
|
||||
# Local rotation is fine given the parent isn't rotating ever
|
||||
return input_dir.rotated(-body.rotation.y)
|
||||
|
||||
|
||||
# TODO Rename sprint to crouch
|
||||
func _on_idle_state_processing(_delta: float) -> void:
|
||||
# !HACK - No deceleration. Just reset the target speed for now
|
||||
target_speed = 0.0
|
||||
#endregion
|
||||
#region States
|
||||
|
||||
|
||||
func _on_root_state_processing(_delta: float) -> void:
|
||||
if get_move_dir() == Vector2.ZERO:
|
||||
if _get_move_dir() == Vector2.ZERO:
|
||||
state_chart.send_event("Idle")
|
||||
else:
|
||||
state_chart.send_event("Move")
|
||||
|
@ -478,3 +457,40 @@ func _on_root_state_processing(_delta: float) -> void:
|
|||
var event := "Grounded" if grounded else "Airborne"
|
||||
state_chart.send_event(event)
|
||||
grounded_prev = grounded
|
||||
|
||||
|
||||
# TODO Rename sprint to crouch
|
||||
func _on_idle_state_processing(_delta: float) -> void:
|
||||
# !HACK - No deceleration. Just reset the target speed for now
|
||||
target_speed = 0.0
|
||||
|
||||
|
||||
func _on_running_state_physics_processing(delta: float) -> void:
|
||||
if target_speed < run_speed:
|
||||
target_speed += (run_speed / run_acceleration_time) * delta
|
||||
else:
|
||||
# TODO Decelerate
|
||||
target_speed = run_speed
|
||||
|
||||
var target_velocity_h := _get_move_dir() * target_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)
|
||||
|
||||
|
||||
func _on_falling_state_physics_processing(delta: float) -> void:
|
||||
var move_speed := run_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)
|
||||
|
||||
|
||||
func _on_jumping_state_physics_processing(delta: float) -> void:
|
||||
var move_speed := run_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)
|
||||
|
||||
#endregion
|
||||
|
|
Loading…
Reference in New Issue