-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gamestate.gd
187 lines (146 loc) · 5.53 KB
/
gamestate.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
extends Node
# Default game port
const DEFAULT_PORT = 10567
# Max number of players
const MAX_PEERS = 12
# Name for my player
var player_name = "The Warrior"
# Names for remote players in id:name format
var players = {}
# Signals to let lobby GUI know what's going on
signal player_list_changed()
signal connection_failed()
signal connection_succeeded()
signal game_ended()
signal game_error(what)
signal lobby_joined(lobby)
# Callback from SceneTree
func _player_connected(_id):
# This is not used in this demo, because _connected_ok is called for clients
# on success and will do the job.
pass
# Callback from SceneTree
func _player_disconnected(id):
if get_tree().is_network_server():
if has_node("/root/world"): # Game is in progress
emit_signal("game_error", "Player " + players[id] + " disconnected")
end_game()
else: # Game is not in progress
# If we are the server, send to the new dude all the already registered players
unregister_player(id)
for p_id in players:
# Erase in the server
rpc_id(p_id, "unregister_player", id)
# Callback from SceneTree, only for clients (not server)
func _connected_ok():
# Registration of a client beings here, tell everyone that we are here
rpc("register_player", get_tree().get_network_unique_id(), player_name)
emit_signal("connection_succeeded")
# Callback from SceneTree, only for clients (not server)
func _server_disconnected():
emit_signal("game_error", "Server disconnected")
end_game()
# Callback from SceneTree, only for clients (not server)
func _connected_fail():
get_tree().set_network_peer(null) # Remove peer
emit_signal("connection_failed")
# Lobby management functions
remote func register_player(id, new_player_name):
if get_tree().is_network_server():
# If we are the server, let everyone know about the new player
rpc_id(id, "register_player", 1, player_name) # Send myself to new dude
for p_id in players: # Then, for each remote player
rpc_id(id, "register_player", p_id, players[p_id]) # Send player to new dude
rpc_id(p_id, "register_player", id, new_player_name) # Send new dude to player
players[id] = new_player_name
emit_signal("player_list_changed")
remote func unregister_player(id):
players.erase(id)
emit_signal("player_list_changed")
remote func pre_start_game(spawn_points):
# Change scene
var world = load("res://world.tscn").instance()
get_tree().get_root().add_child(world)
get_tree().get_root().get_node("lobby").hide()
var player_scene = load("res://player.tscn")
for p_id in spawn_points:
var spawn_pos = world.get_node("spawn_points/" + str(spawn_points[p_id])).position
var player = player_scene.instance()
player.set_name(str(p_id)) # Use unique ID as node name
player.position=spawn_pos
player.set_network_master(p_id) #set unique id as master
if p_id == get_tree().get_network_unique_id():
# If node for this peer id, set name
player.set_player_name(player_name)
else:
# Otherwise set name from peer
player.set_player_name(players[p_id])
world.get_node("players").add_child(player)
# Set up score
world.get_node("score").add_player(get_tree().get_network_unique_id(), player_name)
for pn in players:
world.get_node("score").add_player(pn, players[pn])
if not get_tree().is_network_server():
# Tell server we are ready to start
rpc_id(1, "ready_to_start", get_tree().get_network_unique_id())
elif players.size() == 0:
post_start_game()
remote func post_start_game():
get_tree().set_pause(false) # Unpause and unleash the game!
var players_ready = []
remote func ready_to_start(id):
assert(get_tree().is_network_server())
if not id in players_ready:
players_ready.append(id)
if players_ready.size() == players.size():
for p in players:
rpc_id(p, "post_start_game")
post_start_game()
func host_game(new_player_name, ip):
player_name = new_player_name
Client.start(ip)
func join_game(ip, new_player_name, lobby):
player_name = new_player_name
Client.start(ip, lobby)
func _signaling_disconnected():
if not Client.sealed: # Game has not started yet
emit_signal("game_error", "Signaling server disconnected:\n%d: %s" % [Client.code, Client.reason])
end_game()
func _signaling_inited(lobby):
get_tree().set_network_peer(Client.rtc_mp)
emit_signal("lobby_joined", lobby)
emit_signal("player_list_changed")
func get_player_list():
return players.values()
func get_player_name():
return player_name
func begin_game():
assert(get_tree().is_network_server())
Client.seal_lobby()
# Create a dictionary with peer id and respective spawn points, could be improved by randomizing
var spawn_points = {}
spawn_points[1] = 0 # Server in spawn point 0
var spawn_point_idx = 1
for p in players:
spawn_points[p] = spawn_point_idx
spawn_point_idx += 1
# Call to pre-start game with the spawn points
for p in players:
rpc_id(p, "pre_start_game", spawn_points)
pre_start_game(spawn_points)
func end_game():
if has_node("/root/world"): # Game is in progress
# End it
get_node("/root/world").queue_free()
emit_signal("game_ended")
players.clear()
get_tree().set_network_peer(null) # End networking
Client.stop()
func _ready():
get_tree().connect("network_peer_connected", self, "_player_connected")
get_tree().connect("network_peer_disconnected", self,"_player_disconnected")
get_tree().connect("connected_to_server", self, "_connected_ok")
get_tree().connect("connection_failed", self, "_connected_fail")
get_tree().connect("server_disconnected", self, "_server_disconnected")
Client.connect("lobby_joined", self, "_signaling_inited")
Client.connect("disconnected", self, "_signaling_disconnected")