84 lines
3.5 KiB
GDScript
84 lines
3.5 KiB
GDScript
extends Node
|
|
|
|
@onready var camera_pivot: Node3D = %CameraPivot
|
|
@onready var player_camera: Camera3D = %PlayerCamera
|
|
@onready var player: CharacterBody3D = $"../.."
|
|
@onready var player_state: Node = %PlayerState
|
|
@onready var player_manager: Node = %PlayerManager
|
|
@onready var procedural_camera_mover: Node = %ProceduralCameraMover
|
|
@onready var player_hit_box: CollisionShape3D = $"../../PlayerHitBox"
|
|
@onready var sound_controller: Node = %SoundController
|
|
@onready var ui: Control = %PlayerUi
|
|
|
|
|
|
func _ready() -> void:
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not player.is_on_floor():
|
|
player.velocity += player.get_gravity() * delta
|
|
|
|
if not player_state.in_menu and not Console.toggled:
|
|
if Input.is_action_just_pressed("jump") and player.is_on_floor() and player_manager.stamina > PlayerConsts.JUMP_STAMINA_COST:
|
|
player.velocity.y = PlayerConsts.JUMP_VELOCITY
|
|
player_manager.stamina -= PlayerConsts.JUMP_STAMINA_COST
|
|
player_manager.stamina_recharge_off()
|
|
|
|
var input_dir = Input.get_vector("left", "right", "forward", "backward")
|
|
#var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
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)
|
|
|
|
var target_speed = PlayerConsts.SPEED
|
|
var is_moving = input_dir != Vector2.ZERO
|
|
|
|
if Input.is_action_pressed("crouch"):
|
|
player_hit_box.shape.height = lerpf(player_hit_box.shape.height, .5, delta * 5)
|
|
target_speed *= PlayerConsts.CROUCH_SPEED_MULTIPLIER
|
|
player_state.is_crouched(true)
|
|
else:
|
|
player_hit_box.shape.height = lerpf(player_hit_box.shape.height, 2, delta * 10)
|
|
player_state.is_crouched(false)
|
|
|
|
if Input.is_action_pressed("sprint") and player_manager.stamina > 0:
|
|
target_speed *= PlayerConsts.SPRINT_MULTIPLIER
|
|
player_state.is_running(true)
|
|
player_manager.stamina -= delta * PlayerConsts.SPRINT_STAMINA_COST
|
|
player_manager.stamina_recharge_off()
|
|
else:
|
|
player_state.is_running(false)
|
|
|
|
#apply motion
|
|
var local_vel = player.transform.basis.inverse() * player.velocity
|
|
var target_local_vel = Vector3(player_camera.transform.basis * Vector3(input_dir.x, 0, input_dir.y)) * target_speed
|
|
var accel_rate = 6.0 if is_moving else 9.0
|
|
local_vel.x = lerpf(local_vel.x, target_local_vel.x, delta * accel_rate)
|
|
local_vel.z = lerpf(local_vel.z, target_local_vel.z, delta * accel_rate)
|
|
|
|
var global_horizontal = player.transform.basis * local_vel
|
|
player.velocity.x = global_horizontal.x
|
|
player.velocity.z = global_horizontal.z
|
|
|
|
if is_moving and not player_state.running and not player_state.crouching:
|
|
player_state.is_walking(true)
|
|
else:
|
|
player_state.is_walking(false)
|
|
|
|
else:
|
|
player.velocity.x = lerpf(player.velocity.x, 0, delta * 10)
|
|
player.velocity.z = lerpf(player.velocity.z, 0, delta * 10)
|
|
|
|
player.move_and_slide()
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
#func _process(delta: float) -> void:
|
|
#_mouse_motion.y = clamp(_mouse_motion.y, -1560, 1560)
|
|
#player.transform.basis = Basis.from_euler(Vector3(0, _mouse_motion.x * -0.001 * LOOK_SENSITIVITY, 0))
|
|
#player.transform.basis = lerp(player_camera.transform.basis, Basis.from_euler(Vector3(_mouse_motion.y * -0.001 * LOOK_SENSITIVITY, 0, 0)), delta * 10)
|
|
#
|