93 lines
2.7 KiB
GDScript
93 lines
2.7 KiB
GDScript
extends CharacterBody3D
|
|
|
|
@export var player_id := 69:
|
|
set(id):
|
|
player_id = id
|
|
%Input.set_multiplayer_authority(id)
|
|
|
|
@export var state_machine: Node
|
|
|
|
@export_group("Camera")
|
|
@export_range(0.0, 1.0) var mouse_sensitivity := 0.2
|
|
@export var camera_follow_speed := 10.0
|
|
@export var smooth_camera := false
|
|
|
|
@export_group("Movement")
|
|
@export var run_speed := 8.0
|
|
@export var walk_speed := 4.0
|
|
@export var acceleration := 45.0
|
|
@export var air_deceleration := 5.0
|
|
@export var rotation_speed := 20.0
|
|
|
|
@export_group("Client Tweening")
|
|
@export var client_smoothing := false
|
|
@export var client_smoothing_speed := 5.0
|
|
@export var client_smoothing_rotation_speed := 10.0
|
|
|
|
@onready var _camera_pivot: Node3D = %CameraPivot
|
|
@onready var _camera_target: Node3D = %CameraTarget
|
|
@onready var _camera: Node3D = %Camera
|
|
@onready var skin: Node3D = %Skin
|
|
|
|
var camera_input_direction := Vector2.ZERO
|
|
var last_direction := Vector3.FORWARD
|
|
|
|
@export_group("Server variables")
|
|
@export var server_position := Vector3.ZERO
|
|
@export var server_rotation := Vector3.ZERO
|
|
|
|
func _ready() -> void:
|
|
state_machine.set_subject(self)
|
|
if multiplayer.is_server():
|
|
return
|
|
|
|
if multiplayer.get_unique_id() == player_id:
|
|
%Camera.make_current()
|
|
else:
|
|
%Camera.clear_current(false)
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if multiplayer.get_unique_id() != player_id:
|
|
return
|
|
|
|
if event.is_action_pressed("mouse_capture"):
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
|
elif event.is_action_pressed("mouse_release"):
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
|
|
|
if event is InputEventMouseMotion:
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
camera_input_direction += event.screen_relative * mouse_sensitivity
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
state_machine.process_physics(delta)
|
|
if multiplayer.get_unique_id() == player_id:
|
|
_camera_pivot.rotation.x = clamp(
|
|
_camera_pivot.rotation.x - camera_input_direction.y * delta,
|
|
-PI / 3.0,
|
|
PI / 6.0,
|
|
)
|
|
|
|
_camera_pivot.rotation.y -= camera_input_direction.x * delta
|
|
_camera.global_rotation = _camera_pivot.global_rotation
|
|
|
|
if smooth_camera:
|
|
_camera.global_position = lerp(_camera.global_position, _camera_target.global_position, camera_follow_speed * delta)
|
|
else:
|
|
_camera.global_position = _camera_target.global_position
|
|
|
|
camera_input_direction = Vector2.ZERO
|
|
|
|
if not multiplayer.is_server():
|
|
if client_smoothing:
|
|
position = lerp(position, server_position, client_smoothing_speed * delta)
|
|
skin.global_rotation.y = lerp_angle(skin.global_rotation.y, server_rotation.y, client_smoothing_rotation_speed * delta)
|
|
else:
|
|
position = server_position
|
|
skin.global_rotation.y = server_rotation.y
|
|
|
|
if multiplayer.is_server():
|
|
server_position = position
|
|
server_rotation = skin.global_rotation
|
|
#print("Velocity: %s" % velocity)
|