start Multiplayer

This commit is contained in:
2023-01-15 23:59:12 +01:00
parent 5cfc0839f7
commit 1ef5d54d6c
11 changed files with 209 additions and 255 deletions

View File

@@ -2,6 +2,9 @@ extends CharacterBody3D
const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
@onready var label : Label = $CanvasLayer/Label
@onready var synchroniser = $Networking/MultiplayerSynchronizer
@onready var body : Node3D = $Body
@onready var head : Node3D = $Body/Head
@onready var cam : Camera3D = $Body/Head/Camera3D
@@ -26,9 +29,21 @@ const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
var floor_snap := Vector3.ZERO
func _ready():
synchroniser.set_multiplayer_authority(str(name).to_int())
cam.current = is_local_authority()
func is_local_authority():
return synchroniser.get_multiplayer_authority() == multiplayer.get_unique_id()
func _process(_delta):
if not is_local_authority():
return
label.text = "H Velocity: %3.2f" % [Vector2(velocity.x, velocity.z).length()]
label.text += "\nV Velocity: %3.2f" % [velocity.y]
label.text += "\nOn floor: %s" % is_on_floor()
func get_move_direction() -> Vector3:
var input_dir := Vector2(
Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward"),
Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
@@ -36,18 +51,21 @@ func get_move_direction() -> Vector3:
return input_dir.x * body.global_transform.basis.z + input_dir.y * body.global_transform.basis.x
func _input(event):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion:
rotate_look(event.relative * 0.001 * mouse_sensitivity)
if is_local_authority():
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion:
rotate_look(event.relative * 0.001 * mouse_sensitivity)
func rotate_look(amount : Vector2) -> void:
body.rotation.y -= amount.x
head.rotation.x = clamp(head.rotation.x - amount.y, -PI * 0.5, PI * 0.5)
func _physics_process(delta):
if !is_local_authority():
move_and_slide()
return
var was_on_floor : bool = is_on_floor()
var h_target_dir : Vector3 = get_move_direction()
@@ -79,7 +97,6 @@ func _physics_process(delta):
elif was_on_floor and !is_on_floor():
floor_snap = Vector3.ZERO
func accelerate(delta : float, p_target_dir : Vector3, p_target_speed : float, p_accel : float):
var current_speed : float = velocity.dot(p_target_dir)
var add_speed : float = p_target_speed - current_speed
@@ -87,7 +104,6 @@ func accelerate(delta : float, p_target_dir : Vector3, p_target_speed : float, p
var accel_speed : float = min(add_speed, p_accel * delta * p_target_speed)
velocity += p_target_dir * accel_speed
func apply_friction(delta : float):
#var vec : Vector3 = velocity