cadastery/scripts/player.gd

168 lines
3.7 KiB
GDScript

class_name Player
extends CharacterBody3D
const MOVE_SPEED: float = 5
const MOVE_ACCELERATION: float = 25
const MOVE_DECELERATION: float = 25
const JUMP_FORCE: float = 10
const FALL_SPEED: float = 20
const FALL_ACCELERATION: float = 25
@export var _respawn_height: float = -100
var _respawn_point: Vector3
@onready var _camera: Camera3D = $Camera3D
@onready var _name_label: Label3D = $NameLabel
func _enter_tree() -> void:
set_multiplayer_authority(str(name).to_int())
func _ready() -> void:
_name_label.text = str(name)
if not is_multiplayer_authority():
return
_respawn_point = global_position
_camera.make_current()
Referencer.player = self
Referencer.main_camera = _camera
print("Player ", name, " ready")
func _exit_tree() -> void:
if not is_multiplayer_authority():
return
Referencer.player = null
Referencer.main_camera = null
print("Player ", name, " exiting tree")
func _process(delta: float) -> void:
if not is_multiplayer_authority():
Debugger.vector(
"look" + str(name),
_camera.global_position,
_camera.global_position - _camera.global_basis.z
)
return
_process_respawning()
_process_controller_rotating(delta)
func _physics_process(delta: float) -> void:
if not is_multiplayer_authority():
return
_lateral_movement(delta)
_vertical_movement(delta)
_jumping(delta)
move_and_slide()
func _unhandled_input(event: InputEvent) -> void:
if not is_multiplayer_authority():
return
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion:
_handle_mouse_rotating(event as InputEventMouseMotion)
func _process_respawning() -> void:
if global_position.y < _respawn_height:
global_position = _respawn_point
velocity = Vector3.ZERO
func _process_controller_rotating(delta: float) -> void:
var controller_raw_input := (
Input
. get_vector(
"camera_left",
"camera_right",
"camera_up",
"camera_down",
)
)
rotate_y(
(
-controller_raw_input.x
* deg_to_rad(Settings.controller_sensitivity_horizontal)
* delta
* (-1 if Settings.invert_controller_horizontal else 1)
)
)
_camera.rotate_x(
(
-controller_raw_input.y
* deg_to_rad(Settings.controller_sensitivity_vertical)
* delta
* (-1 if Settings.invert_controller_vertical else 1)
)
)
_camera.rotation.x = clampf(_camera.rotation.x, PI / -2, PI / 2)
func _lateral_movement(delta: float) -> void:
var input_dir := Input.get_vector(
"move_left", "move_right", "move_forward", "move_back"
)
var has_input := input_dir.length() > 0
if has_input:
var direction := (
(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
)
var new_velocity := direction * MOVE_SPEED
new_velocity.y = velocity.y
velocity = velocity.move_toward(new_velocity, MOVE_ACCELERATION * delta)
else:
var new_velocity := Vector3.ZERO
new_velocity.y = velocity.y
velocity = velocity.move_toward(new_velocity, MOVE_DECELERATION * delta)
func _vertical_movement(delta: float) -> void:
if not is_on_floor():
var new_velocity := velocity
new_velocity.y = -FALL_SPEED
velocity = velocity.move_toward(new_velocity, FALL_ACCELERATION * delta)
else:
velocity.y = 0
func _jumping(_delta: float) -> void:
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_FORCE
func _handle_mouse_rotating(event: InputEventMouseMotion) -> void:
var sensitivity := deg_to_rad(Settings.mouse_sensitivity)
rotate_y(
(
-event.screen_relative.x
* sensitivity
* (-1 if Settings.invert_mouse_horizontal else 1)
)
)
_camera.rotate_x(
(
-event.screen_relative.y
* sensitivity
* (-1 if Settings.invert_mouse_vertical else 1)
)
)
_camera.rotation.x = clamp(_camera.rotation.x, -PI / 2, PI / 2)