83 lines
3.2 KiB
GDScript
83 lines
3.2 KiB
GDScript
# ---------------------------------------------------------------------
|
|
# This script is supposed to control the node that is rendered on the
|
|
# client side, and send the changes to the server node
|
|
# ---------------------------------------------------------------------
|
|
extends Node3D
|
|
class_name PlayerInputController
|
|
|
|
var jumping: bool = false
|
|
var shooting: bool = false
|
|
var moving: bool = false
|
|
var look_dir: Vector2
|
|
var input_direction: Vector2
|
|
var camera_sens: float = 0.002
|
|
|
|
const JUMP_VELOCITY = 4.5
|
|
var character_speed: int = 5
|
|
|
|
var controlled_node: ServerNode = null
|
|
@export var owner_id: int
|
|
|
|
@onready var shared_node: CharacterBody3D = $SharedNode
|
|
@onready var camera_mount: Node3D = $SharedNode/CameraMount
|
|
@onready var camera: Camera3D = $SharedNode/CameraMount/Camera3D
|
|
|
|
func _ready() -> void:
|
|
set_multiplayer_authority(multiplayer.get_unique_id())
|
|
shared_node.set_collision_layer_value(2, true)
|
|
shared_node.set_collision_mask_value(1, true)
|
|
shared_node.set_collision_mask_value(2, true)
|
|
camera.make_current()
|
|
|
|
func initial_position_sync():
|
|
|
|
shared_node.global_position = controlled_node.shared_node.global_position
|
|
shared_node.rotation = controlled_node.shared_node.rotation
|
|
|
|
func _input(event):
|
|
if multiplayer.get_unique_id() == get_multiplayer_authority():
|
|
if Input.is_action_just_pressed("jump"): jump()
|
|
if Input.is_action_just_released("shoot"): shooting = false
|
|
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
|
look_dir = event.relative * 1
|
|
shared_node.rotation.y -= look_dir.x * camera_sens * 1.0
|
|
camera_mount.rotation.x = clamp(camera_mount.rotation.x - look_dir.y * camera_sens * 1.0, -1.5, 1.5)
|
|
controlled_node.set_rotation_x.rpc_id(1, shared_node.rotation.x)
|
|
controlled_node.set_rotation_y.rpc_id(1, shared_node.rotation.y)
|
|
if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_right") or Input.is_action_pressed("move_forward") or Input.is_action_pressed("move_backwards"):
|
|
moving = true
|
|
else:
|
|
moving = false
|
|
input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
|
|
|
func jump():
|
|
jumping = true
|
|
controlled_node.jump.rpc_id(1)
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if multiplayer.get_unique_id() == get_multiplayer_authority():
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
controlled_node.set_input_direction.rpc_id(1, input_direction)
|
|
if not shared_node.is_on_floor():
|
|
shared_node.velocity += shared_node.get_gravity() * delta
|
|
if shared_node.is_on_floor() && jumping:
|
|
shared_node.velocity.y = consts.DEFAULT_JUMP_VELOCITY
|
|
|
|
#if shooting:
|
|
jumping = false
|
|
|
|
var direction := (shared_node.transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
|
|
if shared_node.is_on_floor():
|
|
if direction:
|
|
#first_view_legs_anim.play("Run Forward")
|
|
shared_node.velocity.x = direction.x * consts.DEFAULT_CHARACTER_SPEED
|
|
shared_node.velocity.z = direction.z * consts.DEFAULT_CHARACTER_SPEED
|
|
else:
|
|
shared_node.velocity.x = move_toward(shared_node.velocity.x, 0, consts.DEFAULT_CHARACTER_SPEED)
|
|
shared_node.velocity.z = move_toward(shared_node.velocity.z, 0, consts.DEFAULT_CHARACTER_SPEED)
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
shared_node.move_and_slide()
|