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 lateral_collisions: Array[KinematicCollision3D]
|
||||||
var snap_collisions: Array[KinematicCollision3D]
|
var snap_collisions: Array[KinematicCollision3D]
|
||||||
|
|
||||||
|
#region Godot Functions
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
lock_mouse()
|
_lock_mouse()
|
||||||
|
|
||||||
|
|
||||||
func lock_mouse() -> void:
|
|
||||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
||||||
|
|
||||||
|
|
||||||
func unlock_mouse() -> void:
|
|
||||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
||||||
|
|
||||||
|
|
||||||
# TODO should this have an action associated?
|
# TODO should this have an action associated?
|
||||||
|
@ -113,29 +107,13 @@ func _input(event: InputEvent) -> void:
|
||||||
var key_event := event as InputEventKey
|
var key_event := event as InputEventKey
|
||||||
if key_event.keycode == KEY_ESCAPE:
|
if key_event.keycode == KEY_ESCAPE:
|
||||||
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||||||
unlock_mouse()
|
_unlock_mouse()
|
||||||
else:
|
else:
|
||||||
lock_mouse()
|
_lock_mouse()
|
||||||
|
|
||||||
|
|
||||||
func get_move_dir() -> Vector2:
|
#endregion
|
||||||
# We don't want to be moving if the mouse isn't captured!
|
#region Core Movement
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
# Entry point to moving
|
# Entry point to moving
|
||||||
|
@ -417,6 +395,10 @@ func snap_down() -> void:
|
||||||
position = before_snap_pos
|
position = before_snap_pos
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
#region Utilities
|
||||||
|
|
||||||
|
|
||||||
func _under_slope_limit(normal: Vector3) -> bool:
|
func _under_slope_limit(normal: Vector3) -> bool:
|
||||||
return normal.angle_to(Vector3.UP) < deg_to_rad(slope_limit)
|
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)
|
return Vector3(0, value.y, 0)
|
||||||
|
|
||||||
|
|
||||||
func _on_running_state_physics_processing(delta: float) -> void:
|
func _lock_mouse() -> void:
|
||||||
if target_speed < run_speed:
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||||
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:
|
func _unlock_mouse() -> void:
|
||||||
var move_speed := run_speed
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||||
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:
|
func _get_move_dir() -> Vector2:
|
||||||
var move_speed := run_speed
|
# We don't want to be moving if the mouse isn't captured!
|
||||||
var target_velocity_h := get_move_dir() * move_speed
|
if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
||||||
var target_velocity_v := jump_speed
|
return Vector2.ZERO
|
||||||
var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y)
|
|
||||||
move(target_velocity, delta)
|
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
|
#endregion
|
||||||
func _on_idle_state_processing(_delta: float) -> void:
|
#region States
|
||||||
# !HACK - No deceleration. Just reset the target speed for now
|
|
||||||
target_speed = 0.0
|
|
||||||
|
|
||||||
|
|
||||||
func _on_root_state_processing(_delta: float) -> void:
|
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")
|
state_chart.send_event("Idle")
|
||||||
else:
|
else:
|
||||||
state_chart.send_event("Move")
|
state_chart.send_event("Move")
|
||||||
|
@ -478,3 +457,40 @@ func _on_root_state_processing(_delta: float) -> void:
|
||||||
var event := "Grounded" if grounded else "Airborne"
|
var event := "Grounded" if grounded else "Airborne"
|
||||||
state_chart.send_event(event)
|
state_chart.send_event(event)
|
||||||
grounded_prev = grounded
|
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