still has bugs, but almost works like the original, but has much better structure. still might have to change around a bit, but closing in to the final form.
113 lines
4.5 KiB
GDScript
113 lines
4.5 KiB
GDScript
class_name MovementComponent extends Node
|
|
|
|
@export var camera_pivot: Node3D
|
|
@export var player_camera: Camera3D
|
|
@export var player: Player
|
|
@export var player_hit_box: CollisionShape3D
|
|
@export var player_state: Node
|
|
|
|
var mouse_motion: Vector2 = Vector2.ZERO
|
|
var mouse_motion_delta: Vector2 = Vector2.ZERO
|
|
|
|
signal jumped
|
|
|
|
func jump():
|
|
player.velocity.y = PlayerConsts.JUMP_VELOCITY
|
|
player.stamina_component.stamina -= PlayerConsts.JUMP_STAMINA_COST
|
|
jumped.emit()
|
|
|
|
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
|
|
|
|
func _process(_delta: float) -> void:
|
|
if Input.is_action_just_pressed("jump") and player.is_on_floor() and player.stamina_component.stamina > PlayerConsts.JUMP_STAMINA_COST:
|
|
jump()
|
|
|
|
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()
|
|
|
|
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 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.stamina_component.stamina > 0:
|
|
target_speed *= PlayerConsts.SPRINT_MULTIPLIER
|
|
player_state.is_running(true)
|
|
player.stamina_component.stamina -= delta * PlayerConsts.SPRINT_STAMINA_COST
|
|
GlobalSignals.stamina_recharge_off.emit()
|
|
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)
|
|
#
|