Working multiplayer

This commit is contained in:
Nikolai Rodionov 2025-01-24 19:33:33 +01:00
parent aadf8722a5
commit ec9f8216ad
No known key found for this signature in database
GPG Key ID: 0639A45505F3BFA6
5 changed files with 33 additions and 30 deletions

View File

@ -11,11 +11,9 @@ var player_side: String
func _ready() -> void:
var char : Node3D = null
var red_spawn: Node3D = $Spawns/Blue/SpawnArea
var position := red_spawn.global_position
if multiplayer.is_server():
for i in GameServerManager.players:
print(i)
char = ResourceLoader.load("res://scenes/utils/character.tscn").instantiate()
char.name = str(GameServerManager.players[i].name)
char.global_position = position
@ -23,9 +21,7 @@ func _ready() -> void:
char.global_position = position
char.global_position.x += my_random_number
$MultiplayerSpawner.spawn(char)
players.add_child(char)
pass
func spawn_player(id: int):
var red_spawn: Node3D = $Spawns/Blue/SpawnArea

View File

@ -13,6 +13,9 @@ height = 1.8
properties/0/path = NodePath(".:position")
properties/0/spawn = true
properties/0/replication_mode = 1
properties/1/path = NodePath("CharacterBody3D:position")
properties/1/spawn = true
properties/1/replication_mode = 1
[node name="Character" type="Node3D"]
@ -28,12 +31,11 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.71312, 0)
radius = 0.001
[node name="ViewModelCamera" parent="CharacterBody3D/UpperTorso" instance=ExtResource("4_al83x")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.00122696, 0.093623, -0.179357)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.00122696, 0.093623, -0.463804)
script = ExtResource("4_uwcjh")
[node name="Pistol" parent="CharacterBody3D/UpperTorso/ViewModelCamera" instance=ExtResource("5_6k7rq")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, -0.0287516, -0.136104, -0.276055)
visible = false
script = null
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]

View File

@ -33,6 +33,7 @@ func join_game(address = ""):
return error
multiplayer.multiplayer_peer = peer
func create_game():
var peer = ENetMultiplayerPeer.new()
var error = peer.create_server(PORT, MAX_CONNECTIONS)
@ -50,7 +51,6 @@ func remove_multiplayer_peer():
# do Lobby.load_game.rpc(filepath)
@rpc("call_local", "reliable")
func load_game():
print("loading")
get_tree().change_scene_to_file("res://scenes/maps/el_test.tscn")
# Every peer will call this when they have loaded the game scene.
@ -66,19 +66,20 @@ func player_loaded():
# When a peer connects, send them my player info.
# This allows transfer of all desired data for each player, not only the unique ID.
func _on_player_connected(id):
_register_player.rpc_id(1, player_info)
player_info = {"name": multiplayer.get_unique_id()}
_register_player.rpc_id(1, multiplayer.get_unique_id(), player_info)
@rpc("any_peer", "reliable")
func _register_player(new_player_info):
var new_player_id = multiplayer.get_remote_sender_id()
players[new_player_id] = new_player_info
if multiplayer.is_server():
print("registering a player")
print(player_info)
print(new_player_id)
GameServerManager.players[new_player_id] = new_player_info
player_connected.emit(new_player_id, new_player_info)
func _register_player(id: int, new_player_info):
var new_player_id: int = 1
if not multiplayer.is_server():
new_player_id = 1
else:
new_player_id = multiplayer.get_remote_sender_id()
players[id] = new_player_info
GameServerManager.players[id] = new_player_info
#player_info = {"name": str(multiplayer.get)}
player_connected.emit(id, new_player_info)
func _on_player_disconnected(id):
@ -103,17 +104,14 @@ func _on_server_disconnected():
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _on_host_pressed() -> void:
player_info = {"name": "host"}
_register_player(player_info)
player_info = {"name": str(1)}
_register_player(1, player_info)
create_game()
func _on_join_pressed() -> void:
player_info = {"name": "guest"}
join_game()
func _on_start_pressed() -> void:
print("starting")
load_game.rpc()
func _process(delta: float) -> void:
if multiplayer.is_server():
print(GameServerManager.players)
pass

View File

@ -37,6 +37,8 @@ surface_material_override/0 = SubResource("StandardMaterial3D_p8o05")
[node name="RayCast3D" type="RayCast3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.100459)
target_position = Vector3(0, 0, -0.5)
hit_back_faces = false
collide_with_areas = true
[node name="GPUParticles3D" type="GPUParticles3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.18476)

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,8 +31,11 @@ var is_crouch: bool = false
@onready var character: Node3D = $"."
func _ready() -> void:
set_process(get_multiplayer_authority() == multiplayer.get_unique_id())
$"../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
@ -45,11 +47,14 @@ 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 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: