-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.gd
93 lines (74 loc) · 2.88 KB
/
main.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
extends Node3D
var player_refs : Dictionary = {}
var kinematic_body = preload("res://player/player_physical_character.tscn")
var character = preload("res://player/xr_player.tscn")
var puppet = preload("res://player/xr_puppet.tscn")
func _ready() -> void:
get_tree().get_multiplayer().peer_connected.connect(on_peer_connected)
get_tree().get_multiplayer().peer_disconnected.connect(on_peer_disconnected)
get_tree().get_multiplayer().connected_to_server.connect(on_connected_to_server)
func on_peer_connected(id : int) -> void:
#only handle peer connections on server
if !get_tree().get_multiplayer().is_server():
return
#tell all clients that a new peer has connected
receive_peer_connected.rpc(id)
#send newly connected client info about all clients presently on the server
receive_players_on_server.rpc_id(id, get_tree().get_multiplayer().get_peers())
func on_peer_disconnected(id : int) -> void:
#notify all connected clients of peer's disconnection
receiver_peer_disconnected.rpc(id)
func on_connected_to_server() -> void:
pass
@rpc(call_local, authority, reliable)
func receive_players_on_server(peers : PackedInt32Array) -> void:
print("Client: Received peer IDs connected to server: ", peers)
if !get_tree().get_multiplayer().is_server():
#assume server is a player
create_puppet(1)
#create a puppet for every other player already on the server
for peer in peers:
#prevent from adding a puppet for the local client
if peer == get_tree().get_multiplayer().get_unique_id():
continue
create_puppet(peer)
@rpc(call_local, authority, reliable)
func receive_peer_connected(id : int) -> void:
print("Client: Peer connected with ID: ", id)
#if self
if id == get_tree().get_multiplayer().get_unique_id():
create_character(id)
else:
create_puppet(id)
@rpc(call_local, authority, reliable)
func receiver_peer_disconnected(id : int) -> void:
#delete peer's character
player_refs[id].queue_free()
#remove reference to that player
player_refs.erase(id)
func create_character(id : int) -> void:
#create CharacterBody3D instance
var body_instance : CharacterBody3D = kinematic_body.instantiate()
body_instance.name = str(id)
#create XRPlayer
var instance : XROrigin3D = character.instantiate()
instance.name = str(id)
#save reference to player instance
player_refs[id] = instance
body_instance.add_child(instance)
body_instance.Player = instance
body_instance.set_multiplayer_authority(id)
add_child(body_instance)
func create_puppet(id : int) -> void:
#create CharacterBody3D instance
var body_instance : CharacterBody3D = kinematic_body.instantiate()
body_instance.name = str(id)
#create XRPlayer
var instance : Node3D = puppet.instantiate()
instance.name = str(id)
#save reference to player instance
player_refs[id] = instance
body_instance.add_child(instance)
body_instance.Player = instance
body_instance.set_multiplayer_authority(id)
add_child(body_instance)