multiplayer: added basic server and client functionality

This commit is contained in:
Simon Ward 2026-01-27 12:12:18 +13:00 committed by Jeremy Baxter
parent e16f87cbfa
commit f1a4ad761c
5 changed files with 138 additions and 6 deletions

View file

@ -1,7 +1,7 @@
[gd_scene load_steps=16 format=3 uid="uid://eiaw4xbs3suk"]
[ext_resource type="Script" uid="uid://cnbm3aqyg0p2o" path="res://server.gd" id="1_5vw27"]
[ext_resource type="PackedScene" uid="uid://qb8cbljxgnub" path="res://world/killbrick.tscn" id="1_h2yge"]
[ext_resource type="PackedScene" uid="uid://cfceg80unq0pe" path="res://player/player.tscn" id="1_ig7tw"]
[ext_resource type="PackedScene" uid="uid://bcmrj6qkemrll" path="res://world/radiohead_cube.tscn" id="2_0xm2m"]
[ext_resource type="Texture2D" uid="uid://wdjmyv260he1" path="res://world/textures/grass.jpg" id="2_272bh"]
[ext_resource type="PackedScene" uid="uid://of6tq8gpjxtu" path="res://gears/gear_pickup.tscn" id="3_lquwl"]
@ -40,9 +40,7 @@ glow_hdr_luminance_cap = 0.0
[sub_resource type="Compositor" id="Compositor_5vw27"]
[node name="Main" type="Node3D"]
[node name="Player" parent="." instance=ExtResource("1_ig7tw")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0)
script = ExtResource("1_5vw27")
[node name="Baseplate" type="StaticBody3D" parent="."]
@ -83,3 +81,45 @@ compositor = SubResource("Compositor_5vw27")
transform = Transform3D(0.70710665, 0.5, -0.5, 0, 0.7071067, 0.7071067, 0.7071069, -0.49999988, 0.49999988, -500, 200, 500)
light_color = Color(1, 0.87058824, 0.12941177, 1)
shadow_enabled = true
[node name="UI" type="CanvasLayer" parent="."]
[node name="Menu" type="Panel" parent="UI"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="MarginContainer" type="MarginContainer" parent="UI/Menu"]
layout_mode = 0
offset_right = 1149.0
offset_bottom = 134.0
theme_override_constants/margin_left = 15
theme_override_constants/margin_top = 15
[node name="VBoxContainer" type="VBoxContainer" parent="UI/Menu/MarginContainer"]
layout_mode = 2
[node name="Label" type="Label" parent="UI/Menu/MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Menu
"
horizontal_alignment = 1
[node name="ServerButton2" type="Button" parent="UI/Menu/MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Host
"
[node name="ClientButton" type="Button" parent="UI/Menu/MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Join
"
[node name="MultiplayerSpawner" type="MultiplayerSpawner" parent="."]
_spawnable_scenes = PackedStringArray("uid://cfceg80unq0pe")
spawn_path = NodePath("..")
[connection signal="pressed" from="UI/Menu/MarginContainer/VBoxContainer/ServerButton2" to="." method="_on_server_button_pressed"]
[connection signal="pressed" from="UI/Menu/MarginContainer/VBoxContainer/ClientButton" to="." method="_on_client_button_pressed"]

21
menu.tscn Normal file
View file

@ -0,0 +1,21 @@
[gd_scene format=3 uid="uid://dbv3ty2ulxir4"]
[node name="Menu" type="Control"]
layout_mode = 3
anchors_preset = 0
[node name="client_button" type="Button" parent="."]
layout_mode = 0
offset_right = 200.0
offset_bottom = 100.0
text = "Client
"
[node name="server_button" type="Button" parent="."]
layout_mode = 0
offset_top = 165.0
offset_right = 200.0
offset_bottom = 265.0
text = "Server
"

View file

@ -18,6 +18,11 @@ var target_velocity = Vector3.ZERO
var _health = 100
# Multiplayer
func _enter_tree():
set_multiplayer_authority(str(name).to_int())
# Player UI
func resize_ui():
@ -239,8 +244,18 @@ func _ready():
resize_ui()
respawn()
if is_multiplayer_authority():
$CameraGimbal/InnerGimbal/Camera3D.current = true
else:
$CameraGimbal/InnerGimbal/Camera3D.current = false
func _physics_process(delta):
if dead:
if dead or not is_multiplayer_authority():
return
if health() < 1 or position.y <= -1000:
die()
if suspended:
return
if health() < 1 or position.y <= -1000:

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=4 uid="uid://cfceg80unq0pe"]
[gd_scene load_steps=9 format=4 uid="uid://cfceg80unq0pe"]
[ext_resource type="Script" uid="uid://bfflnag3p4gen" path="res://player/player.gd" id="1_onrkg"]
[ext_resource type="Script" uid="uid://oi6sint7jkc6" path="res://player/camera_gimbal.gd" id="2_onrkg"]
@ -47,6 +47,32 @@ _surfaces = [{
}]
shadow_mesh = SubResource("ArrayMesh_e57bw")
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_g1dw6"]
properties/0/path = NodePath(".:position")
properties/0/spawn = true
properties/0/replication_mode = 1
properties/1/path = NodePath(".:rotation")
properties/1/spawn = true
properties/1/replication_mode = 1
properties/2/path = NodePath("CameraGimbal:position")
properties/2/spawn = true
properties/2/replication_mode = 1
properties/3/path = NodePath("CameraGimbal:rotation")
properties/3/spawn = true
properties/3/replication_mode = 1
properties/4/path = NodePath("CameraGimbal/InnerGimbal:position")
properties/4/spawn = true
properties/4/replication_mode = 1
properties/5/path = NodePath("CameraGimbal/InnerGimbal:rotation")
properties/5/spawn = true
properties/5/replication_mode = 1
properties/6/path = NodePath("CameraGimbal/InnerGimbal/Camera3D:position")
properties/6/spawn = true
properties/6/replication_mode = 1
properties/7/path = NodePath("CameraGimbal/InnerGimbal/Camera3D:rotation")
properties/7/spawn = true
properties/7/replication_mode = 1
[node name="Player" type="CharacterBody3D"]
collision_layer = 2
collision_mask = 3
@ -102,6 +128,9 @@ text = "Pictures of you"
horizontal_alignment = 2
vertical_alignment = 2
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_g1dw6")
[connection signal="child_entered_tree" from="Backpack" to="." method="_on_backpack_child_entered_tree"]
[connection signal="child_exiting_tree" from="Backpack" to="." method="_on_backpack_child_exiting_tree"]
[connection signal="timeout" from="RespawnTimer" to="." method="respawn"]

27
server.gd Normal file
View file

@ -0,0 +1,27 @@
extends Node3D
@onready var menu = $UI/Menu
const PORT = 9999
const PLAYER = preload("res://player/player.tscn")
var peer = ENetMultiplayerPeer.new()
func _on_server_button_pressed() -> void:
menu.hide()
peer.create_server(PORT)
multiplayer.multiplayer_peer = peer
multiplayer.peer_connected.connect(add_player)
add_player(multiplayer.get_unique_id())
func _on_client_button_pressed() -> void:
menu.hide()
peer.create_client("localhost",PORT)
multiplayer.multiplayer_peer = peer
func add_player(peer_id):
var player = PLAYER.instantiate()
player.name = str(peer_id)
add_child(player)