breadwinner/scenes/player/movement_component.gd
Hellie-xd 8c950a1710 ficed some issues, continued refactoring
current bugs are the not right connections of the signals. im working on
it.
2026-08-09 23:18:13 +02:00

169 lines
5.7 KiB
GDScript

class_name MovementComponent extends Node
@export var camera_pivot: Node3D
@export var player_camera: Camera3D
@export var player_hit_box: CollisionShape3D
@export var player: Player
signal jumped
signal landed(speed:float)
signal running(is_running:bool)
signal crouching(is_crouching:bool)
signal walking(is_walking:bool)
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
@onready var global_delta: float
@onready var target_speed = PlayerConsts.SPEED
func jump():
player.velocity.y = PlayerConsts.JUMP_VELOCITY
jumped.emit()
func land(velocity: float):
if _player_spawned:
landed.emit(velocity)
func crouch(is_crouched:bool):
if is_crouched:
target_speed *= PlayerConsts.CROUCH_SPEED_MULTIPLIER
else:
target_speed /= PlayerConsts.CROUCH_SPEED_MULTIPLIER
crouched = is_crouched
crouching.emit(is_crouched)
func run(is_running:bool):
if player.velocity.length() > 0:
if is_running:
target_speed *= PlayerConsts.SPRINT_MULTIPLIER
else:
target_speed /= PlayerConsts.SPRINT_MULTIPLIER
running.emit(is_running)
func walk(is_walking:bool):
if is_walking:
walks = true
walking.emit(true)
else:
walks = false
walking.emit(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 _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 _process(delta: float) -> void:
global_delta = delta
if Input.is_action_just_pressed("jump") and player.is_on_floor() and player.stamina_component.stamina > PlayerConsts.JUMP_STAMINA_COST:
jump()
if crouched:
player_hit_box.shape.height = lerpf(player_hit_box.shape.height, .5, delta * 10)
else:
player_hit_box.shape.height = lerpf(player_hit_box.shape.height, 2, delta * 10)
func _physics_process(delta: float) -> void:
if not Console.toggled or not UiManager.in_game_menu.visible:
mouse_motion.y = clamp(mouse_motion.y, -1560, 1560)
player.transform.basis = lerp(player.transform.basis, Basis.from_euler(Vector3(0, mouse_motion.x * -0.001 * PlayerConsts.LOOK_SENSITIVITY, 0)), delta * 30)
player_camera.transform.basis = lerp(player_camera.transform.basis, Basis.from_euler(Vector3(mouse_motion.y * -0.001 * PlayerConsts.LOOK_SENSITIVITY, 0, 0)), delta * 30)
handle_joypad_movement()
var was_on_floor: bool = player.is_on_floor()
var fall_velocity: float = player.velocity.y
if not player.is_on_floor():
player.velocity += player.get_gravity() * delta
if not UiManager.in_game_menu.visible and not Console.toggled:
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 is_moving = input_dir != Vector2.ZERO
if Input.is_action_just_pressed("crouch"):
crouch(true)
elif Input.is_action_just_released("crouch"):
crouch(false)
if Input.is_action_just_pressed("sprint") and player.stamina_component.stamina > 0:
run(true)
elif Input.is_action_just_released("sprint") or player.stamina_component.stamina == 0:
run(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 (Input.is_action_just_pressed("forward") \
or Input.is_action_just_pressed("backward") \
or Input.is_action_just_pressed("left") \
or Input.is_action_just_pressed("right")
) and not runs and not crouched and not walks:
walk(true)
elif walks and not (Input.is_action_pressed("forward") \
or Input.is_action_pressed("backward") \
or Input.is_action_pressed("left") \
or Input.is_action_pressed("right")):
walk(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()
if not was_on_floor and player.is_on_floor():
land(fall_velocity)
# 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)
#