-
Notifications
You must be signed in to change notification settings - Fork 1
/
end.gd
65 lines (56 loc) · 1.67 KB
/
end.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
extends Node2D
var client = Client
var winner = null
var match_over = false
var return_scene = "res://menu.tscn"
onready var p0 = $players/p0
onready var p1 = $players/p1
onready var p0_title = $GUI/purple_wins
onready var p1_title = $GUI/yellow_wins
onready var scores = $GUI/scores
onready var play_button = $GUI/play_button
func _ready():
update_ui()
func _process(delta):
if Globals.mode == Globals.ONLINE_MULTIPLAYER:
# Discard old packets
client.rtc_mp.poll()
while client.rtc_mp.get_available_packet_count() > 0:
var packet = client.rtc_mp.get_packet()
if packet != null:
var msg = packet.get_string_from_utf8()
var json = JSON.parse(msg).result
# TODO(Richo): Handle score discrepancies!
# Send a keep alive packet
var data = {"t": Globals.get_timestamp(),
"scores": Globals.scores}
var msg = JSON.print(data)
if client.rtc_mp.put_packet(msg.to_utf8()) != 0:
# TODO(Richo): Handle errors
print("ERROR!")
func update_ui():
if winner == "p0":
p0.play("alive")
p1.play("dead")
elif winner == "p1":
p1.play("alive")
p0.play("dead")
else:
p0.play("dead")
p1.play("dead")
scores.text = str(Globals.scores[0]) + " - " + str(Globals.scores[1])
if Globals.scores[0] >= 5:
p0_title.visible = true
match_over = true
elif Globals.scores[1] >= 5:
p1_title.visible = true
match_over = true
if match_over:
# Move the scores label down to the center
scores.margin_top = get_viewport().get_visible_rect().size.y/2 - scores.rect_size.y/2
play_button.text = "REMATCH!"
func _on_play_button_pressed():
if match_over:
Globals.scores = [0, 0]
get_tree().change_scene(return_scene)
get_tree().get_root().remove_child(self)