cadastery/scripts/player.gd

239 lines
5.5 KiB
GDScript

class_name Player
extends CharacterBody3D
const MOVE_SPEED: float = 5
const RUN_SPEED: float = 10
const CROUCH_SPEED: float = 1
const MOVE_ACCELERATION: float = 50
const MOVE_DECELERATION: float = 25
const JUMP_FORCE: float = 10
const FALL_SPEED: float = 20
const FALL_ACCELERATION: float = 25
const CAMERA_CROUCH_HEIGHT: float = 1
const CAMERA_HEIGHT_SPEED: float = 5
const CAMERA_RUN_EXTRA_FOV: float = 5
const CAMERA_FOV_SPEED: float = 50
@export var _respawn_height: float = -100
var _peer_id: int = 1
var _respawn_point: Vector3
var _default_camera_height: float
var _crouch: bool = false
var _run: bool = false
@onready var _camera: Camera3D = $Camera3D
@onready var _name_label: Label3D = $NameLabel
func _enter_tree() -> void:
_peer_id = str(name).to_int()
print("Player ", Networker.player_name(_peer_id), " enter tree")
set_multiplayer_authority(_peer_id)
func _ready() -> void:
Networker.player_registered.connect(_on_networker_player_registered)
if not is_multiplayer_authority():
return
_default_camera_height = _camera.position.y
_respawn_point = global_position
_camera.make_current()
Referencer.player = self
Referencer.camera = _camera
print("Player ", Networker.player_name(_peer_id), " ready authority")
func _exit_tree() -> void:
print("Player ", Networker.player_name(_peer_id), " exit tree")
if Networker.is_active() and not is_multiplayer_authority():
return
Referencer.player = null
Referencer.camera = null
func _process(delta: float) -> void:
if Networker.is_active() and 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)
_process_camera(delta)
func _physics_process(delta: float) -> void:
if not is_multiplayer_authority():
return
_lateral_movement(delta)
_vertical_movement(delta)
_jumping()
move_and_slide()
func _unhandled_input(event: InputEvent) -> void:
if not is_multiplayer_authority():
return
if (
not Inputer.can_control_player()
or Input.mouse_mode != Input.MOUSE_MODE_CAPTURED
):
return
if event is InputEventMouseMotion:
_handle_mouse_rotating(event as InputEventMouseMotion)
if event.is_action_pressed("crouch"):
_crouch = true
if event.is_action_released("crouch"):
_crouch = false
if event.is_action_pressed("run"):
_run = true
if event.is_action_released("run"):
_run = false
func set_info(player_info: Dictionary) -> void:
_name_label.text = player_info["name"]
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",
)
)
var look_slow: float = 0.25 if Input.is_action_pressed("look_slow") else 1.0
rotate_y(
(
-controller_raw_input.x
* deg_to_rad(Settings.controller_sensitivity_horizontal)
* look_slow
* delta
* (-1.0 if Settings.invert_controller_horizontal else 1.0)
)
)
_camera.rotate_x(
(
-controller_raw_input.y
* deg_to_rad(Settings.controller_sensitivity_vertical)
* look_slow
* delta
* (-1.0 if Settings.invert_controller_vertical else 1.0)
)
)
_camera.rotation.x = clampf(_camera.rotation.x, PI / -2, PI / 2)
func _process_camera(delta: float) -> void:
var camera_height := _default_camera_height
if _crouch:
camera_height = CAMERA_CROUCH_HEIGHT
_camera.position.y = move_toward(
_camera.position.y, camera_height, CAMERA_HEIGHT_SPEED * delta
)
var camera_fov := Settings.camera_fov
if _run:
camera_fov += CAMERA_RUN_EXTRA_FOV
_camera.fov = move_toward(_camera.fov, camera_fov, CAMERA_FOV_SPEED * delta)
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 and Inputer.can_control_player()
if has_input:
var direction := (
(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
)
var speed := MOVE_SPEED
Debugger.text("move_mode", "move", 2)
if _crouch:
speed = CROUCH_SPEED
Debugger.text("move_mode", "crouch", 2)
if _run:
speed = RUN_SPEED
Debugger.text("move_mode", "run", 2)
var new_velocity := direction * 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() -> void:
if (
Inputer.can_control_player()
and 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)
func _on_networker_player_registered(peer_id: int, player_info: Dictionary) -> void:
if peer_id != _peer_id:
return
set_info(player_info)