277 lines
9.3 KiB
GDScript
277 lines
9.3 KiB
GDScript
#class_name MovementComponent extends Node
|
|
#
|
|
#@export var camera_pivot: Node3D
|
|
#@export var player_hit_box: CollisionShape3D
|
|
#@export var player: Player
|
|
#
|
|
#signal jumped
|
|
#signal landed(speed: float)
|
|
#signal state_changed(state: String)
|
|
#
|
|
#var mouse_motion: Vector2 = Vector2.ZERO
|
|
#var mouse_motion_delta: Vector2 = Vector2.ZERO
|
|
#var crouched: bool = false
|
|
#var runs: bool = false
|
|
#var walks: bool = false
|
|
#
|
|
#var _player_spawned: bool = false
|
|
#var _current_state: String = "idle"
|
|
#var _move_toggled: bool = false
|
|
#
|
|
## CACHE: Fetching Scene Unique Nodes (%) every frame is slightly expensive.
|
|
## Cache it once when the node loads.
|
|
#@onready var noclip_component: Node = %Noclip
|
|
#
|
|
#func _ready() -> void:
|
|
#Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
#get_tree().create_timer(0.5).timeout.connect(func(): _player_spawned = true)
|
|
#
|
|
#func _input(event: InputEvent) -> void:
|
|
## EARLY EXIT: Check the fastest conditions first to save CPU cycles
|
|
#if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
|
#return
|
|
#
|
|
#if event is InputEventMouseMotion and not UiManager.in_game_menu.visible:
|
|
## Anti-yank threshold
|
|
#if event.relative.length() > 400:
|
|
#return
|
|
#
|
|
#mouse_motion += event.relative
|
|
#mouse_motion_delta = event.relative
|
|
#
|
|
#func toggle_movement():
|
|
#_move_toggled = !_move_toggled
|
|
#if _move_toggled:
|
|
#Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
#else:
|
|
#Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
#
|
|
#func handle_joypad_movement():
|
|
#var controller_look = Input.get_vector("look_left", "look_right", "look_up", "look_down") * 50.0
|
|
#if controller_look != Vector2.ZERO:
|
|
#mouse_motion += controller_look
|
|
#mouse_motion_delta += controller_look
|
|
#
|
|
#func head_movement(delta: float) -> void:
|
|
## 1. Gather all inputs FIRST
|
|
#handle_joypad_movement()
|
|
#
|
|
## 2. Clamp the Y motion BEFORE applying it to rotation (Fixes 1-frame camera jitter)
|
|
#mouse_motion.y = clampf(mouse_motion.y, -1560.0, 1560.0)
|
|
#
|
|
## 3. Apply rotations based on the finalized inputs
|
|
#player.rotation.y = lerp_angle(player.rotation.y, mouse_motion.x * -0.001 * PlayerConsts.LOOK_SENSITIVITY, delta * 30.0)
|
|
#
|
|
#var target_pitch = mouse_motion.y * -0.001 * PlayerConsts.LOOK_SENSITIVITY
|
|
#camera_pivot.rotation.x = lerp_angle(camera_pivot.rotation.x, target_pitch, delta * 30.0)
|
|
#
|
|
#func _physics_process(delta: float) -> void:
|
|
#head_movement(delta)
|
|
#
|
|
#if Console.toggled or UiManager.in_game_menu.visible or UiManager.inventory_menu.visible:
|
|
## Use move_toward for stopping. lerpf() never truly reaches 0, which
|
|
## leaves tiny float decimals calculating forever.
|
|
#player.velocity.x = move_toward(player.velocity.x, 0.0, delta * 100.0)
|
|
#player.velocity.z = move_toward(player.velocity.z, 0.0, delta * 100.0)
|
|
#player.move_and_slide()
|
|
#return
|
|
#
|
|
#if noclip_component.is_noclip:
|
|
#noclip_component._process_noclip_movement(delta)
|
|
#return
|
|
#
|
|
#var was_on_floor: bool = player.is_on_floor()
|
|
#var fall_velocity: float = player.velocity.y
|
|
#
|
|
#if not was_on_floor:
|
|
#player.velocity += player.get_gravity() * delta
|
|
#
|
|
#var input_dir := Vector2.ZERO
|
|
#if not _move_toggled:
|
|
#input_dir = Input.get_vector("left", "right", "forward", "backward")
|
|
#
|
|
#var target_speed = PlayerConsts.SPEED
|
|
#var new_state := "idle"
|
|
#var is_moving := input_dir != Vector2.ZERO
|
|
#
|
|
#if Input.is_action_pressed("crouch"):
|
|
#target_speed *= PlayerConsts.CROUCH_SPEED_MULTIPLIER
|
|
#new_state = "crouching"
|
|
#player_hit_box.shape.height = lerpf(player_hit_box.shape.height, 0.5, delta * 3.0)
|
|
#else:
|
|
#player_hit_box.shape.height = lerpf(player_hit_box.shape.height, 2.0, delta * 3.0)
|
|
#
|
|
#if is_moving:
|
|
#if Input.is_action_pressed("sprint") and player.stamina_component.stamina > 0:
|
|
#target_speed *= PlayerConsts.SPRINT_MULTIPLIER
|
|
#new_state = "running"
|
|
#else:
|
|
#new_state = "walking"
|
|
#
|
|
#if new_state != _current_state:
|
|
#_current_state = new_state
|
|
#state_changed.emit(new_state)
|
|
#
|
|
#if Input.is_action_just_pressed("jump") and was_on_floor and player.stamina_component.stamina > PlayerConsts.JUMP_STAMINA_COST:
|
|
#player.velocity.y += PlayerConsts.JUMP_VELOCITY
|
|
#jumped.emit()
|
|
#
|
|
## Calculate horizontal velocity
|
|
#var direction = (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
#var accel_rate = 6.0 if is_moving else 9.0
|
|
#
|
|
## Vectorize the lerp for cleaner math
|
|
#var h_vel = Vector2(player.velocity.x, player.velocity.z)
|
|
#var target_h_vel = Vector2(direction.x, direction.z) * target_speed
|
|
#h_vel = h_vel.lerp(target_h_vel, delta * accel_rate)
|
|
#
|
|
#player.velocity.x = h_vel.x
|
|
#player.velocity.z = h_vel.y
|
|
#
|
|
## Leaning
|
|
#var lean_dir = -Input.get_axis("lean_left", "lean_right") * 0.1
|
|
#camera_pivot.rotation.z = lerpf(camera_pivot.rotation.z, lean_dir, delta * 10.0)
|
|
#
|
|
#var target_lean_pos = -lean_dir * 5.0
|
|
#camera_pivot.position.x = lerpf(camera_pivot.position.x, target_lean_pos, delta * 7.0)
|
|
#
|
|
#player.move_and_slide()
|
|
#
|
|
#if not was_on_floor and player.is_on_floor() and _player_spawned:
|
|
#landed.emit(fall_velocity)
|
|
|
|
class_name MovementComponent extends Node
|
|
|
|
@export var camera_pivot: Node3D
|
|
@export var player_hit_box: CollisionShape3D
|
|
@export var player: Player
|
|
|
|
signal jumped
|
|
signal landed(speed:float)
|
|
signal state_changed(state:String)
|
|
|
|
var mouse_motion: Vector2 = Vector2.ZERO
|
|
var mouse_motion_delta: Vector2 = Vector2.ZERO
|
|
var crouched: bool = false
|
|
var runs: bool = false
|
|
var walks: bool = false
|
|
|
|
var _player_spawned: bool = false
|
|
var _current_state: String = "idle"
|
|
var _move_toggled: bool = false
|
|
|
|
func handle_joypad_movement():
|
|
var controller_look_vector = Input.get_vector("look_left", "look_right", "look_up", "look_down") * 50
|
|
|
|
if controller_look_vector.length() > 0:
|
|
mouse_motion += controller_look_vector
|
|
mouse_motion_delta += controller_look_vector
|
|
|
|
func toggle_movement():
|
|
_move_toggled = !_move_toggled
|
|
if _move_toggled:
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
else:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if not UiManager.in_game_menu.visible:
|
|
if event is InputEventMouseMotion:
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
#so it doesnt yank
|
|
if event.relative.length() > 400:
|
|
return
|
|
|
|
mouse_motion += event.relative
|
|
mouse_motion_delta = event.relative
|
|
|
|
func _ready() -> void:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
get_tree().create_timer(0.5).timeout.connect(func(): _player_spawned = true)
|
|
|
|
func head_movement(delta:float) -> void:
|
|
player.rotation.y = lerp_angle(player.rotation.y, mouse_motion.x * -0.001 * PlayerConsts.LOOK_SENSITIVITY, delta * 30)
|
|
|
|
var target_pitch = mouse_motion.y * -0.001 * PlayerConsts.LOOK_SENSITIVITY
|
|
camera_pivot.rotation.x = lerp_angle(camera_pivot.rotation.x, target_pitch, delta * 30)
|
|
|
|
handle_joypad_movement()
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
head_movement(delta)
|
|
|
|
if Console.toggled or UiManager.in_game_menu.visible or UiManager.inventory_menu.visible:
|
|
player.velocity.x = lerpf(player.velocity.x, 0, delta * 10)
|
|
player.velocity.z = lerpf(player.velocity.z, 0, delta * 10)
|
|
player.move_and_slide()
|
|
return
|
|
|
|
if %Noclip.is_noclip:
|
|
%Noclip._process_noclip_movement(delta)
|
|
return
|
|
|
|
mouse_motion.y = clamp(mouse_motion.y, -1560, 1560)
|
|
|
|
var was_on_floor: bool = player.is_on_floor()
|
|
var fall_velocity: float = player.velocity.y
|
|
|
|
if not was_on_floor:
|
|
player.velocity += player.get_gravity() * delta
|
|
|
|
var input_dir: Vector2
|
|
|
|
if not _move_toggled:
|
|
input_dir = Input.get_vector("left", "right", "forward", "backward")
|
|
else:
|
|
input_dir = Vector2.ZERO
|
|
|
|
var target_speed = PlayerConsts.SPEED
|
|
var new_state = "idle"
|
|
var is_moving = input_dir != Vector2.ZERO
|
|
|
|
if Input.is_action_pressed("crouch"):
|
|
target_speed *= PlayerConsts.CROUCH_SPEED_MULTIPLIER
|
|
new_state = "crouching"
|
|
player_hit_box.shape.height = lerpf(player_hit_box.shape.height, 0.5, delta * 3)
|
|
|
|
else:
|
|
player_hit_box.shape.height = lerpf(player_hit_box.shape.height, 2, delta * 3)
|
|
|
|
if is_moving:
|
|
if Input.is_action_pressed("sprint") and player.stamina_component.stamina > 0:
|
|
target_speed *= PlayerConsts.SPRINT_MULTIPLIER
|
|
new_state = "running"
|
|
else:
|
|
new_state = "walking"
|
|
|
|
if new_state != _current_state:
|
|
_current_state = new_state
|
|
state_changed.emit(new_state)
|
|
|
|
if Input.is_action_just_pressed("jump") and player.is_on_floor() and player.stamina_component.stamina > PlayerConsts.JUMP_STAMINA_COST:
|
|
player.velocity.y += PlayerConsts.JUMP_VELOCITY
|
|
jumped.emit()
|
|
|
|
var direction = (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
var accel_rate = 6.0 if is_moving else 9.0
|
|
|
|
if direction:
|
|
player.velocity.x = lerpf(player.velocity.x, direction.x * target_speed, delta * accel_rate)
|
|
player.velocity.z = lerpf(player.velocity.z, direction.z * target_speed, delta * accel_rate)
|
|
else:
|
|
player.velocity.x = lerpf(player.velocity.x, 0.0, delta * accel_rate)
|
|
player.velocity.z = lerpf(player.velocity.z, 0.0, delta * accel_rate)
|
|
|
|
var lean_dir = -Input.get_axis("lean_left", "lean_right") * .1
|
|
|
|
if lean_dir:
|
|
camera_pivot.rotation.z = lerpf(camera_pivot.rotation.z, lean_dir, delta*10)
|
|
camera_pivot.position.x = lerpf(camera_pivot.position.x, -lean_dir * 5, delta * 7)
|
|
else:
|
|
camera_pivot.rotation.z = lerpf(camera_pivot.rotation.z, 0, delta*10)
|
|
camera_pivot.position.x = lerpf(camera_pivot.position.x, 0, delta * 7)
|
|
|
|
player.move_and_slide()
|
|
|
|
if not was_on_floor and player.is_on_floor() and _player_spawned:
|
|
landed.emit(fall_velocity)
|