First working multiplayer implementation

This commit is contained in:
2025-01-23 20:24:42 +01:00
parent fe08da9d4c
commit 51842836ce
8 changed files with 129 additions and 64 deletions

View File

@ -16,7 +16,6 @@ class_name Player extends CharacterBody3D
@onready var body: Node3D = $RealBody
var jumping: bool = false
var mouse_captured: bool = false
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
var move_dir: Vector2 # Input direction for movement
@ -32,7 +31,11 @@ var is_crouch: bool = false
@onready var character: Node3D = $"."
func _ready() -> void:
$"../MultiplayerSynchronizer".set_multiplayer_authority(str($"..".name).to_int())
enable_camera()
capture_mouse()
#print("I am " + str(multiplayer.get_unique_id()) + "I'm controling " + str($"../MultiplayerSynchronizer".get_multiplayer_authority()))
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
look_dir = event.relative * 0.001
@ -44,12 +47,15 @@ func _unhandled_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("crouch"): crouch()
if Input.is_action_just_released("crouch"): uncrouch()
func enable_camera():
if str($"..".name).to_int() == multiplayer.get_unique_id():
$UpperTorso/ViewModelCamera.make_current()
func _physics_process(delta: float) -> void:
if mouse_captured: _handle_joypad_camera_rotation(delta)
velocity = _walk(delta) + _gravity(delta) + _jump(delta)
move_and_slide()
if str($"..".name).to_int() == multiplayer.get_unique_id():
if mouse_captured: _handle_joypad_camera_rotation(delta)
velocity = _walk(delta) + _gravity(delta) + _jump(delta)
move_and_slide()
func capture_mouse() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

View File

@ -0,0 +1,11 @@
extends Node
var players = {}
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass