Add leaderboards to multiplayer games

pull/2/head
Ensar Sarajčić 2022-01-20 19:11:54 +01:00
parent 2e2a73e207
commit ad4c8140e2
5 changed files with 79 additions and 21 deletions

View File

@ -45,7 +45,6 @@ func create_client(address, port, vehicle):
func _peer_connected(peer_id): func _peer_connected(peer_id):
print("_peer_connected(%s)" % peer_id)
peers[peer_id] = false peers[peer_id] = false
rpc_id(peer_id, "add_player", get_tree().get_network_unique_id(), current_vehicle) rpc_id(peer_id, "add_player", get_tree().get_network_unique_id(), current_vehicle)
if get_tree().get_network_unique_id() == 1: if get_tree().get_network_unique_id() == 1:
@ -53,14 +52,12 @@ func _peer_connected(peer_id):
func _peer_disconnected(peer_id): func _peer_disconnected(peer_id):
print("_peer_disconnected(%s)" % peer_id)
peers.erase(peer_id) peers.erase(peer_id)
destroy_player(peer_id) destroy_player(peer_id)
print("new peers state: %s" % peers)
func _connected_to_server(): func _connected_to_server():
print("_connected_to_server") pass
func _connection_failed(): func _connection_failed():

View File

@ -46,7 +46,6 @@ func reset_player_to(node_to_reset_to: Node, player_node: BuggedVehicle) -> void
func _spawn_in_player(): func _spawn_in_player():
print("SPAWNING IN PLAYER")
reset_player_to(track.get_furthest_checkpoint(), player_node) reset_player_to(track.get_furthest_checkpoint(), player_node)
add_child(player_node) add_child(player_node)
add_child(gui) add_child(gui)

View File

@ -1,15 +1,22 @@
extends MarginContainer extends MarginContainer
var best_time = -1 var best_time = -1
var leaderboards_data = {}
onready var time_value = $VBoxContainer/HBoxContainer/TimeValue onready var time_value = $VBoxContainer/HBoxContainer/TimeValue
onready var best_time_value = $VBoxContainer/HBoxContainer/BestTimeValue onready var best_time_value = $VBoxContainer/HBoxContainer/BestTimeValue
onready var wrong_way_label = $VBoxContainer/WrongWayLabel onready var wrong_way_label = $CenterContainer/WrongWayLabel
onready var leaderboards = $VBoxContainer/LeaderboardsLine
onready var leaderboards_list = $VBoxContainer/LeaderboardsLine/VBoxContainer/Leaderboards
func _ready() -> void: func _ready() -> void:
time_value.text = "NaN" time_value.text = "NaN"
best_time_value.text = "NaN" best_time_value.text = "NaN"
if MultiplayerController.is_online():
leaderboards.visible = true
else:
leaderboards.visible = false
func _on_time_updated(new_time: float) -> void: func _on_time_updated(new_time: float) -> void:
@ -20,6 +27,9 @@ func _on_lap_complete(lap_time: float) -> void:
if lap_time < best_time or best_time < 0: if lap_time < best_time or best_time < 0:
best_time = lap_time best_time = lap_time
best_time_value.text = _format_time(best_time) best_time_value.text = _format_time(best_time)
if MultiplayerController.is_online():
update_leaderboard_time(String(get_tree().get_network_unique_id()), lap_time)
rpc("update_leaderboard_time", String(get_tree().get_network_unique_id()), lap_time)
func _on_wrong_way_detected() -> void: func _on_wrong_way_detected() -> void:
@ -33,3 +43,26 @@ func _format_time(time: float) -> String:
var second = floor((time / 1000.0) - minute * 60) var second = floor((time / 1000.0) - minute * 60)
var millisecond = time - minute * 60 * 1000 - second * 1000 var millisecond = time - minute * 60 * 1000 - second * 1000
return "%02d:%02d.%03d" % [minute, second, millisecond] return "%02d:%02d.%03d" % [minute, second, millisecond]
remote func update_leaderboard_time(peer_id: String, lap_time: float):
leaderboards_data[peer_id] = lap_time
var leaderboards_sorted = []
for peer in leaderboards_data:
leaderboards_sorted.append([peer, leaderboards_data[peer]])
leaderboards_sorted.sort_custom(self, "_leaderbords_comparison")
for child in leaderboards_list.get_children():
child.queue_free()
for entry in leaderboards_sorted:
var label = Label.new()
label.text = "%s - %s" % [entry[0], _format_time(entry[1])]
leaderboards_list.add_child(label)
func _leaderbords_comparison(left: Array, right: Array) -> bool:
return left[1] < right[1]

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=5 format=2]
[ext_resource path="res://assets/fonts/kenney-future-narrow.ttf" type="DynamicFontData" id=1] [ext_resource path="res://assets/fonts/kenney-future-narrow.ttf" type="DynamicFontData" id=1]
[ext_resource path="res://scenes/trackgui.gd" type="Script" id=2] [ext_resource path="res://scenes/trackgui.gd" type="Script" id=2]
@ -9,6 +9,10 @@ outline_size = 2
outline_color = Color( 1, 1, 1, 0.317647 ) outline_color = Color( 1, 1, 1, 0.317647 )
font_data = ExtResource( 1 ) font_data = ExtResource( 1 )
[sub_resource type="Theme" id=2]
default_font = SubResource( 1 )
Label/colors/font_color = Color( 0.482353, 0, 0, 1 )
[node name="TrackGUI" type="MarginContainer"] [node name="TrackGUI" type="MarginContainer"]
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -16,6 +20,7 @@ margin_left = 20.0
margin_top = 20.0 margin_top = 20.0
margin_right = -20.0 margin_right = -20.0
margin_bottom = -20.0 margin_bottom = -20.0
theme = SubResource( 2 )
script = ExtResource( 2 ) script = ExtResource( 2 )
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
@ -34,8 +39,6 @@ margin_right = 289.0
margin_bottom = 41.0 margin_bottom = 41.0
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 0 size_flags_vertical = 0
custom_colors/font_color = Color( 0.482353, 0, 0, 1 )
custom_fonts/font = SubResource( 1 )
text = "Current lap -" text = "Current lap -"
align = 1 align = 1
valign = 1 valign = 1
@ -46,8 +49,6 @@ margin_right = 489.0
margin_bottom = 41.0 margin_bottom = 41.0
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 0 size_flags_vertical = 0
custom_colors/font_color = Color( 0.482353, 0, 0, 1 )
custom_fonts/font = SubResource( 1 )
text = "12:23.245" text = "12:23.245"
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
@ -59,8 +60,6 @@ margin_right = 562.0
margin_bottom = 41.0 margin_bottom = 41.0
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 0 size_flags_vertical = 0
custom_colors/font_color = Color( 0.482353, 0, 0, 1 )
custom_fonts/font = SubResource( 1 )
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
@ -71,8 +70,6 @@ margin_right = 784.0
margin_bottom = 41.0 margin_bottom = 41.0
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 0 size_flags_vertical = 0
custom_colors/font_color = Color( 0.482353, 0, 0, 1 )
custom_fonts/font = SubResource( 1 )
text = "Best Lap -" text = "Best Lap -"
[node name="BestTimeValue" type="Label" parent="VBoxContainer/HBoxContainer"] [node name="BestTimeValue" type="Label" parent="VBoxContainer/HBoxContainer"]
@ -81,22 +78,48 @@ margin_right = 984.0
margin_bottom = 41.0 margin_bottom = 41.0
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 0 size_flags_vertical = 0
custom_colors/font_color = Color( 0.482353, 0, 0, 1 )
custom_fonts/font = SubResource( 1 )
text = "01:23.256" text = "01:23.256"
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="WrongWayLabel" type="Label" parent="VBoxContainer"] [node name="LeaderboardsLine" type="HBoxContainer" parent="VBoxContainer"]
visible = false
margin_top = 49.0 margin_top = 49.0
margin_right = 984.0 margin_right = 984.0
margin_bottom = 90.0 margin_bottom = 98.0
alignment = 2
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/LeaderboardsLine"]
margin_left = 684.0
margin_right = 984.0
margin_bottom = 49.0
[node name="LeaderboardsLabel" type="Label" parent="VBoxContainer/LeaderboardsLine/VBoxContainer"]
margin_right = 300.0
margin_bottom = 41.0
text = "Leaderboards"
[node name="Leaderboards" type="VBoxContainer" parent="VBoxContainer/LeaderboardsLine/VBoxContainer"]
margin_top = 49.0
margin_right = 300.0
margin_bottom = 49.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="CenterContainer" type="CenterContainer" parent="."]
margin_right = 984.0
margin_bottom = 560.0
[node name="WrongWayLabel" type="Label" parent="CenterContainer"]
visible = false
margin_left = 238.0
margin_top = 259.0
margin_right = 745.0
margin_bottom = 300.0
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 0 size_flags_vertical = 0
custom_colors/font_color = Color( 0.482353, 0, 0, 1 ) custom_colors/font_color = Color( 0.482353, 0, 0, 1 )
custom_fonts/font = SubResource( 1 )
text = "Wrong Way! Turn Around!" text = "Wrong Way! Turn Around!"
align = 1 align = 1
__meta__ = { __meta__ = {

View File

@ -52,6 +52,12 @@ func _process(_delta: float) -> void:
func _on_body_entered_area(body: Node, area: Area) -> void: func _on_body_entered_area(body: Node, area: Area) -> void:
if (
MultiplayerController.is_online()
and body.get_network_master() != get_tree().get_network_unique_id()
):
return
if body.get_groups().has("car"): if body.get_groups().has("car"):
if area.get_index() < last_checkpoint || abs(area.get_index() - last_checkpoint) > 1: if area.get_index() < last_checkpoint || abs(area.get_index() - last_checkpoint) > 1:
emit_signal("wrong_way") emit_signal("wrong_way")