-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgamestate.gd
178 lines (130 loc) · 4.75 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
extends Node
# Max number of players.
const MAX_PEERS = 6
# Name for my player.
var player_name = "Armhulen"
# Node of this client
var player_node = null
# Names for remote players in id:name format.
var players = {}
var players_ready = []
# 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)
# Callback from SceneTree.
func _player_connected(id):
# Registration of a client beings here, tell the connected player that we are here.
rpc_id(id, "register_player", player_name)
# Callback from SceneTree.
func _player_disconnected(id):
if has_node("/root/World"): # Game is in progress.
if get_tree().is_network_server():
emit_signal("game_error", "Player " + players[id] + " disconnected")
end_game()
else: # Game is not in progress.
# Unregister this player.
unregister_player(id)
# Callback from SceneTree, only for clients (not server).
func _connected_ok():
# We just connected to a server
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(new_player_name):
var id = get_tree().get_rpc_sender_id()
print(id)
players[id] = new_player_name
emit_signal("player_list_changed")
func unregister_player(id):
players.erase(id)
emit_signal("player_list_changed")
remote func pre_start_game(spawning_positions_for_players):
# Change scene.
print(spawning_positions_for_players)
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://shrimp.tscn")
for p_id in spawning_positions_for_players:
var player = player_scene.instance()
player.global_position = spawning_positions_for_players[p_id]
# This is extremely important. This name is what determines everything.
player.set_name(str(p_id))
player.set_player_id(p_id)
if p_id == get_tree().get_network_unique_id():
player_node = player
world.add_child(player)
player_node.set_player_name(player_name)
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())
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!
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, port):
player_name = new_player_name
var host = NetworkedMultiplayerENet.new()
host.create_server(port, MAX_PEERS)
get_tree().set_network_peer(host)
func join_game(ip, new_player_name, port):
player_name = new_player_name
var client = NetworkedMultiplayerENet.new()
client.create_client(ip, int(port))
get_tree().set_network_peer(client)
func get_player_list():
return players.values()
func get_player_name():
return player_name
func begin_game():
assert(get_tree().is_network_server())
# Create a dictionary with peer id and a coordinate for them to move on
var spawning_positions_for_players = {}
var spawnx = 200
var spawny = 200
spawning_positions_for_players[1] = Vector2(spawnx,spawny) #reserved for server itself
spawnx += 100
for player_id in players:
spawning_positions_for_players[player_id] = Vector2(spawnx,spawny)
spawnx += 100
for peer in players:
rpc_id(peer, "pre_start_game", spawning_positions_for_players)
pre_start_game(spawning_positions_for_players)
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()
func _ready():
# warning-ignore:return_value_discarded
get_tree().connect("network_peer_connected", self, "_player_connected")
# warning-ignore:return_value_discarded
get_tree().connect("network_peer_disconnected", self,"_player_disconnected")
# warning-ignore:return_value_discarded
get_tree().connect("connected_to_server", self, "_connected_ok")
# warning-ignore:return_value_discarded
get_tree().connect("connection_failed", self, "_connected_fail")
# warning-ignore:return_value_discarded
get_tree().connect("server_disconnected", self, "_server_disconnected")