-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_online.gd
233 lines (193 loc) · 6.57 KB
/
game_online.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
extends Node2D
onready var client = Client
onready var p0 = $player0
onready var p1 = $player1
onready var debug_label = $GUI/debug_label
onready var error_panel = $GUI/error_panel
onready var network_unstable = $GUI/network_unstable
onready var waiting = $GUI/waiting
var game_over = false
var player = null
var opponent = null
var projectile_counter = 0
var player_projectiles = {} # Projectile -> int (id)
var opponent_projectiles = {} # int (id) -> Projectile
onready var last_msg_time = OS.get_ticks_msec()
var network_unstable_counter = 0
func _ready():
OS.hide_virtual_keyboard()
Globals.mode = Globals.ONLINE_MULTIPLAYER
if Globals.player == 0:
player = p0
opponent = p1
else:
player = p1
opponent = p0
player.is_local = true
player.input_map = "p"
player.visible = true
player.input_enabled = false
opponent.is_local = false
opponent.input_map = "p"
opponent.visible = false
opponent.input_enabled = false
for touch_btn in $touch.get_children():
touch_btn.visible = OS.has_touchscreen_ui_hint()
Globals.level = Globals.level + 1
seed(Globals.level)
for planet in $planets.get_children():
var sprite = planet.get_node("sprite") as AnimatedSprite
sprite.frame = rand_range(0, sprite.frames.get_frame_count(sprite.animation) - 1)
planet.scale = Vector2.ONE * rand_range(1, 4.5)
player.connect("projectile_shot", self, "_on_projectile_shot")
waiting.visible = false
yield(get_tree().create_timer(0.25), "timeout")
if not opponent.visible:
waiting.visible = true
func _on_projectile_shot(p):
if not player_projectiles.has(p):
projectile_counter += 1
player_projectiles[p] = projectile_counter
func _process(delta):
if Input.is_action_just_pressed("toggle_debug_info"):
debug_label.visible = !debug_label.visible
if error_panel.visible: return
check_connection_health()
receive_incoming_messages()
call_deferred("send_outgoing_messages")
func check_connection_health():
if last_msg_time == null: return
var time_without_msg = OS.get_ticks_msec() - last_msg_time
if time_without_msg > 3000:
connection_error()
elif time_without_msg > 500:
connection_unstable()
else:
connection_reliable()
func receive_incoming_messages():
client.rtc_mp.poll()
while client.rtc_mp.get_available_packet_count() > 0:
var packet = null
packet = client.rtc_mp.get_packet()
if packet != null:
last_msg_time = OS.get_ticks_msec()
var msg = packet.get_string_from_utf8()
var json = JSON.parse(msg).result
var t_player = Globals.get_timestamp()
var t_opponent = json["t"]
var t_diff = t_player - t_opponent
debug_label.text = "%d\n\nPlayer_projectiles: %d\nOpponent_projectiles: %d\n\nP_time: %d\nO_time: %d\nD_time: %d" \
% [projectile_counter,
player_projectiles.size(),
opponent_projectiles.size(),
t_player,
t_opponent,
t_diff]
if json.has("player"):
var player_data = json["player"]
waiting.visible = false
player.input_enabled = true
opponent.visible = true
opponent.global_position = Vector2(player_data["x"], player_data["y"])
opponent.global_rotation = player_data["r"]
if player_data["facing_right"]:
opponent.face_right()
else:
opponent.face_left()
opponent.sprite.play(player_data["animation"])
if player_data["aiming"] != null:
if not opponent.rocket_launcher.visible:
opponent.rocket_launcher.rotation = player_data["aiming"]
opponent.rocket_launcher.visible = true
else:
opponent.rocket_launcher.visible = false
if player_data["dead"] and not opponent.dead:
opponent.die()
print("OPPONENT DEAD")
var projectile_data = json["projectiles"]
for id in opponent_projectiles.keys():
if not projectile_data.has(id):
opponent_projectiles[id].explode()
opponent_projectiles.erase(id)
for id in projectile_data.keys():
var p = projectile_data[id]
var pos = Vector2(p["x"], p["y"])
var vel = Vector2(p["v"]["x"], p["v"]["y"])
var exploded = p["exploded"]
var projectile = null
if opponent_projectiles.has(id):
projectile = opponent_projectiles[id]
projectile.global_position = pos
projectile.velocity = vel
else:
projectile = opponent.rocket_launcher.shoot_at(pos, vel)
opponent_projectiles[id] = projectile
projectile.is_local = false
if exploded:
projectile.explode()
opponent_projectiles.erase(id)
func send_outgoing_messages():
var player_data = {"x": player.global_position.x,
"y": player.global_position.y,
"r": player.global_rotation,
"facing_right": player.facing_right(),
"animation": player.sprite.animation,
"aiming": player.rocket_launcher.rotation if player.aiming else null,
"dead": player.dead}
var projectile_data = {}
for p in player_projectiles.keys():
var p_id = player_projectiles[p]
projectile_data[p_id] = {"x": p.global_position.x,
"y": p.global_position.y,
"v": {"x": p.velocity.x, "y": p.velocity.y},
"exploded": p.exploded}
if p.exploded:
player_projectiles.erase(p)
p.explode()
var data = {"t": Globals.get_timestamp(),
"scores": Globals.scores,
"player": player_data,
"projectiles": projectile_data}
var msg = JSON.print(data)
if client.rtc_mp.put_packet(msg.to_utf8()) != 0:
# TODO(Richo): Handle errors
print("ERROR!")
func _on_player0_tree_exited():
if game_over: return
game_over = true
call_deferred("winner", "p1")
func _on_player1_tree_exited():
if game_over: return
game_over = true
call_deferred("winner", "p0")
func winner(winner):
if player.dead and opponent.dead:
winner = null
else:
var winner_idx = 0 if winner == "p0" else 1
Globals.scores[winner_idx] = Globals.scores[winner_idx] + 1
var root = get_tree().get_root()
# Remove the current level
var level = get_tree().get_current_scene()
root.remove_child(level)
level.call_deferred("free")
# Add end scene
var end_scene = preload("res://end.tscn").instance()
end_scene.winner = winner
end_scene.return_scene = "res://game_online.tscn"
root.add_child(end_scene)
func _on_back_button_pressed():
back()
func back():
get_tree().change_scene("res://menu.tscn")
func connection_error():
game_over = true # no winner
error_panel.connect("closed", self, "back", [], CONNECT_ONESHOT)
error_panel.show_message("CONNECTION ERROR!", "Reason: Player disconnected")
func connection_unstable():
network_unstable_counter += 1
if network_unstable_counter >= 10:
network_unstable.visible = true
func connection_reliable():
network_unstable_counter = 0
network_unstable.visible = false