Skip to content

Commit

Permalink
Implemented ties if both players die at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
RichoM committed Sep 8, 2022
1 parent d6e3566 commit 0202a59
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
43 changes: 27 additions & 16 deletions end.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,37 @@ 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")
else:
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])

Expand All @@ -30,22 +55,8 @@ func _ready():

if match_over:
# Move the scores label down to the center
scores.margin_top = get_viewport().size.y/2 - scores.rect_size.y/2
scores.margin_top = get_viewport().get_visible_rect().size.y/2 - scores.rect_size.y/2
play_button.text = "REMATCH!"

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()

# Send a keep alive packet
var data = {"t": Globals.get_timestamp()}
var msg = JSON.print(data)
if client.rtc_mp.put_packet(msg.to_utf8()) != 0:
# TODO(Richo): Handle errors
print("ERROR!")

func _on_play_button_pressed():
if match_over:
Expand Down
16 changes: 7 additions & 9 deletions end.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ region = Rect2( 16, 64, 16, 16 )

[sub_resource type="SpriteFrames" id=6]
animations = [ {
"frames": [ ExtResource( 1 ) ],
"frames": [ SubResource( 4 ), SubResource( 5 ) ],
"loop": true,
"name": "dead",
"name": "alive",
"speed": 5.0
}, {
"frames": [ SubResource( 4 ), SubResource( 5 ) ],
"frames": [ ExtResource( 1 ) ],
"loop": true,
"name": "alive",
"name": "dead",
"speed": 5.0
} ]

Expand All @@ -86,13 +86,12 @@ script = ExtResource( 4 )
[node name="GUI" type="CanvasLayer" parent="."]

[node name="play_button" type="Button" parent="GUI"]
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -204.0
margin_left = 30.0
margin_top = -153.0
margin_right = 204.0
margin_right = -30.0
margin_bottom = -75.0
custom_colors/font_color = Color( 0, 0, 0, 1 )
custom_colors/font_color_hover = Color( 0.32549, 0.623529, 0.701961, 1 )
Expand Down Expand Up @@ -183,7 +182,6 @@ position = Vector2( 55, 0 )
scale = Vector2( 3, 3 )
frames = SubResource( 6 )
animation = "alive"
frame = 1
playing = true
flip_h = true

Expand Down
7 changes: 5 additions & 2 deletions game_local.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ func _on_player1_tree_exited():
call_deferred("winner", "p0")

func winner(winner):
var winner_idx = 0 if winner == "p0" else 1
Globals.scores[winner_idx] = Globals.scores[winner_idx] + 1
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()

Expand Down
11 changes: 8 additions & 3 deletions game_online.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ onready var network_unstable = $GUI/network_unstable
onready var waiting = $GUI/waiting

var game_over = false

var player = null
var opponent = null

Expand Down Expand Up @@ -103,7 +104,7 @@ func receive_incoming_messages():
t_player,
t_opponent,
t_diff]

if json.has("player"):
var player_data = json["player"]
waiting.visible = false
Expand Down Expand Up @@ -173,6 +174,7 @@ func send_outgoing_messages():
p.explode()

var data = {"t": Globals.get_timestamp(),
"scores": Globals.scores,
"player": player_data,
"projectiles": projectile_data}
var msg = JSON.print(data)
Expand All @@ -191,8 +193,11 @@ func _on_player1_tree_exited():
call_deferred("winner", "p0")

func winner(winner):
var winner_idx = 0 if winner == "p0" else 1
Globals.scores[winner_idx] = Globals.scores[winner_idx] + 1
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()

Expand Down

0 comments on commit 0202a59

Please sign in to comment.