Add names in multiplayer

pull/2/head
Ensar Sarajčić 2022-01-20 19:56:06 +01:00
parent ad4c8140e2
commit 0c80cab346
6 changed files with 116 additions and 41 deletions

View File

@ -18,6 +18,8 @@ onready var automatic_transmission_cb: CheckBox = $MarginContainer/VSplitContain
onready var fullscreen_cb: CheckBox = $MarginContainer/VSplitContainer/CenterContainer/VBoxContainer/FullscreenCheckBox onready var fullscreen_cb: CheckBox = $MarginContainer/VSplitContainer/CenterContainer/VBoxContainer/FullscreenCheckBox
# gdlint: ignore=max-line-length # gdlint: ignore=max-line-length
onready var borderless_cb: CheckBox = $MarginContainer/VSplitContainer/CenterContainer/VBoxContainer/BorderlessCheckBox onready var borderless_cb: CheckBox = $MarginContainer/VSplitContainer/CenterContainer/VBoxContainer/BorderlessCheckBox
# gdlint: ignore=max-line-length
onready var multiplayer_name_box: LineEdit = $MarginContainer/VSplitContainer/CenterContainer/VBoxContainer/MultiplayerNameBox
func _ready() -> void: func _ready() -> void:
@ -27,6 +29,7 @@ func _ready() -> void:
music_slider.value = db2linear(AudioServer.get_bus_volume_db(music_bus)) music_slider.value = db2linear(AudioServer.get_bus_volume_db(music_bus))
auto_clutch_cb.pressed = GlobalSettings.auto_clutch auto_clutch_cb.pressed = GlobalSettings.auto_clutch
automatic_transmission_cb.pressed = GlobalSettings.automatic_transmission automatic_transmission_cb.pressed = GlobalSettings.automatic_transmission
multiplayer_name_box.text = GlobalSettings.multiplayer_name
fullscreen_cb.pressed = false fullscreen_cb.pressed = false
borderless_cb.pressed = false borderless_cb.pressed = false
if OS.is_window_fullscreen(): if OS.is_window_fullscreen():
@ -55,6 +58,7 @@ func _on_automatictransmission_toggled(new_state: bool) -> void:
func _on_BackButton_pressed() -> void: func _on_BackButton_pressed() -> void:
GlobalSettings.multiplayer_name = multiplayer_name_box.text
get_tree().change_scene("res://menu/main_menu.tscn") get_tree().change_scene("res://menu/main_menu.tscn")

View File

@ -37,7 +37,7 @@ margin_bottom = -20.0
[node name="VSplitContainer" type="VSplitContainer" parent="MarginContainer"] [node name="VSplitContainer" type="VSplitContainer" parent="MarginContainer"]
margin_right = 984.0 margin_right = 984.0
margin_bottom = 615.0 margin_bottom = 705.0
dragger_visibility = 1 dragger_visibility = 1
[node name="HSplitContainer" type="HSplitContainer" parent="MarginContainer/VSplitContainer"] [node name="HSplitContainer" type="HSplitContainer" parent="MarginContainer/VSplitContainer"]
@ -62,12 +62,12 @@ valign = 1
[node name="CenterContainer" type="CenterContainer" parent="MarginContainer/VSplitContainer"] [node name="CenterContainer" type="CenterContainer" parent="MarginContainer/VSplitContainer"]
margin_top = 96.0 margin_top = 96.0
margin_right = 984.0 margin_right = 984.0
margin_bottom = 615.0 margin_bottom = 705.0
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/VSplitContainer/CenterContainer"] [node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/VSplitContainer/CenterContainer"]
margin_left = 356.0 margin_left = 356.0
margin_right = 627.0 margin_right = 627.0
margin_bottom = 519.0 margin_bottom = 609.0
[node name="Gameplay" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="Gameplay" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_right = 271.0 margin_right = 271.0
@ -101,18 +101,37 @@ __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="AudioLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="MultiplayerNameLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 147.0 margin_top = 147.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 174.0 margin_bottom = 165.0
custom_fonts/font = SubResource( 5 )
text = "Multiplayer name"
align = 1
__meta__ = {
"_edit_use_anchors_": false
}
[node name="MultiplayerNameBox" type="LineEdit" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 173.0
margin_right = 271.0
margin_bottom = 229.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="AudioLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 237.0
margin_right = 271.0
margin_bottom = 264.0
custom_fonts/font = SubResource( 4 ) custom_fonts/font = SubResource( 4 )
text = "Audio" text = "Audio"
align = 1 align = 1
[node name="MasterLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="MasterLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 182.0 margin_top = 272.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 200.0 margin_bottom = 290.0
custom_fonts/font = SubResource( 5 ) custom_fonts/font = SubResource( 5 )
text = "Master" text = "Master"
align = 1 align = 1
@ -121,9 +140,9 @@ __meta__ = {
} }
[node name="MasterSlider" type="HSlider" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="MasterSlider" type="HSlider" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 208.0 margin_top = 298.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 240.0 margin_bottom = 330.0
max_value = 1.0 max_value = 1.0
step = 0.05 step = 0.05
value = 1.0 value = 1.0
@ -132,9 +151,9 @@ __meta__ = {
} }
[node name="SoundEffectsLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="SoundEffectsLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 248.0 margin_top = 338.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 266.0 margin_bottom = 356.0
custom_fonts/font = SubResource( 5 ) custom_fonts/font = SubResource( 5 )
text = "Sound Effects" text = "Sound Effects"
align = 1 align = 1
@ -143,9 +162,9 @@ __meta__ = {
} }
[node name="SoundEffectsSlider" type="HSlider" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="SoundEffectsSlider" type="HSlider" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 274.0 margin_top = 364.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 306.0 margin_bottom = 396.0
max_value = 1.0 max_value = 1.0
step = 0.05 step = 0.05
value = 1.0 value = 1.0
@ -154,9 +173,9 @@ __meta__ = {
} }
[node name="MusicLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="MusicLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 314.0 margin_top = 404.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 332.0 margin_bottom = 422.0
custom_fonts/font = SubResource( 5 ) custom_fonts/font = SubResource( 5 )
text = "Music" text = "Music"
align = 1 align = 1
@ -165,9 +184,9 @@ __meta__ = {
} }
[node name="MusicSlider" type="HSlider" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="MusicSlider" type="HSlider" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 340.0 margin_top = 430.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 372.0 margin_bottom = 462.0
max_value = 1.0 max_value = 1.0
step = 0.05 step = 0.05
value = 1.0 value = 1.0
@ -176,17 +195,17 @@ __meta__ = {
} }
[node name="VideoLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="VideoLabel" type="Label" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 380.0 margin_top = 470.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 407.0 margin_bottom = 497.0
custom_fonts/font = SubResource( 4 ) custom_fonts/font = SubResource( 4 )
text = "Video" text = "Video"
align = 1 align = 1
[node name="FullscreenCheckBox" type="CheckBox" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="FullscreenCheckBox" type="CheckBox" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 415.0 margin_top = 505.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 463.0 margin_bottom = 553.0
custom_fonts/font = SubResource( 3 ) custom_fonts/font = SubResource( 3 )
text = "Fullscreen" text = "Fullscreen"
align = 1 align = 1
@ -195,9 +214,9 @@ __meta__ = {
} }
[node name="BorderlessCheckBox" type="CheckBox" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"] [node name="BorderlessCheckBox" type="CheckBox" parent="MarginContainer/VSplitContainer/CenterContainer/VBoxContainer"]
margin_top = 471.0 margin_top = 561.0
margin_right = 271.0 margin_right = 271.0
margin_bottom = 519.0 margin_bottom = 609.0
custom_fonts/font = SubResource( 3 ) custom_fonts/font = SubResource( 3 )
text = "Borderless" text = "Borderless"
align = 1 align = 1

View File

@ -4,13 +4,38 @@ const GUI_SCENE = "res://player/gui.tscn"
var enet_peer = NetworkedMultiplayerENet.new() var enet_peer = NetworkedMultiplayerENet.new()
var peers = {} var peers = {}
var peers_vehicles = {}
var current_track: Node = null var current_track: Node = null
var current_track_path: String var current_track_path: String
var current_vehicle: String var current_vehicle: String
class PeerInfo:
var id: int
var vehicle: String
var name: String
var spawned: bool
static func new_peer(
new_id: int, new_vehicle: String = "", new_name: String = "", new_spawned: bool = false
) -> PeerInfo:
var new_instance = PeerInfo.new()
new_instance.id = new_id
new_instance.vehicle = new_vehicle
new_instance.name = new_name
new_instance.spawned = new_spawned
return new_instance
func to_array() -> Array:
return [id, vehicle, name, spawned]
func from_array(data: Array) -> void:
self.id = data[0]
self.vehicle = data[1]
self.name = data[2]
self.spawned = data[3]
func _ready(): func _ready():
enet_peer.connect("peer_connected", self, "_peer_connected") enet_peer.connect("peer_connected", self, "_peer_connected")
enet_peer.connect("peer_disconnected", self, "_peer_disconnected") enet_peer.connect("peer_disconnected", self, "_peer_disconnected")
@ -32,21 +57,28 @@ func create_server(port, track, vehicle):
current_vehicle = vehicle current_vehicle = vehicle
enet_peer.create_server(port, 2) enet_peer.create_server(port, 2)
get_tree().network_peer = enet_peer get_tree().network_peer = enet_peer
peers[1] = true peers[1] = PeerInfo.new_peer(1, vehicle, GlobalSettings.multiplayer_name, true)
create_player(1, vehicle) create_player(1, vehicle)
get_tree().root.call_deferred("add_child", current_track) get_tree().root.call_deferred("add_child", current_track)
func create_client(address, port, vehicle): func create_client(address, port, vehicle):
current_vehicle = vehicle current_vehicle = vehicle
peers[get_tree().get_network_unique_id()] = true
enet_peer.create_client(address, port) enet_peer.create_client(address, port)
get_tree().network_peer = enet_peer get_tree().network_peer = enet_peer
peers[get_tree().get_network_unique_id()] = PeerInfo.new_peer(
get_tree().get_network_unique_id(), vehicle, GlobalSettings.multiplayer_name, true
)
func _peer_connected(peer_id): func _peer_connected(peer_id):
peers[peer_id] = false peers[peer_id] = PeerInfo.new_peer(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(),
peers[get_tree().get_network_unique_id()].to_array()
)
if get_tree().get_network_unique_id() == 1: if get_tree().get_network_unique_id() == 1:
rpc_id(peer_id, "select_track", current_track_path) rpc_id(peer_id, "select_track", current_track_path)
@ -62,7 +94,6 @@ func _connected_to_server():
func _connection_failed(): func _connection_failed():
current_track = null current_track = null
peers_vehicles.clear()
get_tree().network_peer = null get_tree().network_peer = null
_server_disconnected() _server_disconnected()
@ -70,7 +101,6 @@ func _connection_failed():
func _server_disconnected(): func _server_disconnected():
current_track = null current_track = null
peers.clear() peers.clear()
peers_vehicles.clear()
get_tree().network_peer = null get_tree().network_peer = null
get_tree().root.get_child(get_tree().root.get_child_count() - 1).queue_free() get_tree().root.get_child(get_tree().root.get_child_count() - 1).queue_free()
get_tree().change_scene("res://menu/main_menu.tscn") get_tree().change_scene("res://menu/main_menu.tscn")
@ -101,22 +131,42 @@ func quit():
current_track = null current_track = null
get_tree().network_peer = null get_tree().network_peer = null
peers.clear() peers.clear()
peers_vehicles.clear()
remote func add_player(peer_id, vehicle_path): remote func add_player(peer_id, peer_info: Array):
if peers[peer_id] == false: if peers[peer_id] == null:
peers[peer_id] = true peers[peer_id] = PeerInfo.new_peer(peer_id, "", "", false)
if peers[peer_id].spawned == false:
peers[peer_id].from_array(peer_info)
# Check for duplicate names on server side
if get_tree().get_network_unique_id() == 1:
var names = []
for peer in peers:
if peer != peer_id:
names.append(peers[peer].name)
if names.has(peers[peer_id].name):
peers[peer_id].name = peers[peer_id].name + ("(%s)" % peer_id)
rpc("update_name", peer_id, peers[peer_id].name)
if current_track != null: if current_track != null:
create_player(peer_id, vehicle_path) peers[peer_id].spawned = true
create_player(peer_id, peers[peer_id].vehicle)
else: else:
peers_vehicles[peer_id] = vehicle_path peers[peer_id].spawned = false
remote func select_track(track_path): remote func select_track(track_path):
current_track = load(track_path).instance() current_track = load(track_path).instance()
get_tree().root.call_deferred("add_child", current_track) get_tree().root.call_deferred("add_child", current_track)
create_player(get_tree().get_network_unique_id(), current_vehicle) create_player(get_tree().get_network_unique_id(), current_vehicle)
for peer in peers_vehicles: for peer in peers:
create_player(peer, peers_vehicles[peer]) if peers[peer].spawned == false:
peers_vehicles.clear() peers[peer].spawned = true
create_player(peer, peers[peer].vehicle)
remote func update_name(peer_id, name: String):
peers[peer_id].name = name

View File

@ -59,7 +59,8 @@ remote func update_leaderboard_time(peer_id: String, lap_time: float):
for entry in leaderboards_sorted: for entry in leaderboards_sorted:
var label = Label.new() var label = Label.new()
label.text = "%s - %s" % [entry[0], _format_time(entry[1])] var player_name = MultiplayerController.peers[int(float(entry[0]))].name
label.text = "%s - %s" % [player_name, _format_time(entry[1])]
leaderboards_list.add_child(label) leaderboards_list.add_child(label)

View File

@ -3,3 +3,4 @@ extends Node
var auto_clutch: bool = false var auto_clutch: bool = false
var automatic_transmission: bool = true var automatic_transmission: bool = true
var selected_camera: int = 0 var selected_camera: int = 0
var multiplayer_name: String = "Player"

View File

@ -26,7 +26,7 @@ radial_segments = 4
rings = 4 rings = 4
[sub_resource type="AudioStreamGenerator" id=6] [sub_resource type="AudioStreamGenerator" id=6]
mix_rate = 5500.0 mix_rate = 5000.0
[node name="tire_smoke" type="Particles"] [node name="tire_smoke" type="Particles"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.541801, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.541801, 0 )