128 lines
4.2 KiB
GDScript
128 lines
4.2 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
|
|
|
|
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 _physics_process(delta: float) -> void:
|
|
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
|
|
|
|
mouse_motion.y = clamp(mouse_motion.y, -1560, 1560)
|
|
|
|
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()
|
|
|
|
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)
|