Improve GUI menus

This commit is contained in:
Lucas Schumacher 2024-03-17 16:16:54 -04:00
parent 783d5db03e
commit f8af7e2f23
4 changed files with 145 additions and 56 deletions

View File

@ -2,9 +2,16 @@ extends CanvasLayer
@export var address = "127.0.0.1" @export var address = "127.0.0.1"
@export var port = 8910 @export var port = 8910
@export var player_name: LineEdit
@export var map_root: Node
@export var host_button: Button
@export var join_button: Button
@export var address_input: LineEdit
@export var playerList: ItemList
@export var start_button: Button
var peer var peer
#var nick = "NO_NICKNAME"
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
@ -12,23 +19,32 @@ func _ready():
multiplayer.peer_disconnected.connect(peer_disconnected) multiplayer.peer_disconnected.connect(peer_disconnected)
multiplayer.connected_to_server.connect(connected_to_server) multiplayer.connected_to_server.connect(connected_to_server)
multiplayer.connection_failed.connect(connection_failed) multiplayer.connection_failed.connect(connection_failed)
pass # Replace with function body. join_button.button_up.connect(_on_join_button_down)
host_button.button_up.connect(_on_host_button_down)
start_button.button_up.connect(_on_start_button_down)
start_button.disabled = true
### Server and Client ### Server and Client
func peer_connected(id): func peer_connected(id):
print("Peer connected: ", id) print("Peer connected: ", id);
### Server and Client ### Server and Client
func peer_disconnected(id): func peer_disconnected(id):
print("Peer disconnected: ", id) print("Peer disconnected: ", id)
GameManager.players.erase(id)
if id == 1: if id == 1:
print("Server Disconnected !!!!!") print("Server Disconnected !!!!!")
var tree = get_children() var tree = get_children()
print("Children: ", tree) print("Children: ", tree)
for child in $Map.get_children(): for child in map_root.get_children():
child.queue_free() child.queue_free()
GameManager.players.clear()
$Lobby.hide()
$MainMenu.show()
self.show() self.show()
GameManager.players.erase(id)
playerList.clear()
for i in GameManager.players:
playerList.add_item(GameManager.players[i].name, null, false)
var players = get_tree().get_nodes_in_group("Player") var players = get_tree().get_nodes_in_group("Player")
for i in players: for i in players:
if i.name == str(id): if i.name == str(id):
@ -38,7 +54,7 @@ func peer_disconnected(id):
### Client ### Client
func connected_to_server(): func connected_to_server():
print("Connected to server :)") print("Connected to server :)")
SendPlayerInformation.rpc_id(1, $Nickname.text, multiplayer.get_unique_id()) SendPlayerInformation.rpc_id(1, player_name.text, multiplayer.get_unique_id())
#SendPlayerInformation(nick, multiplayer.get_unique_id()) #SendPlayerInformation(nick, multiplayer.get_unique_id())
### Client ### Client
@ -56,6 +72,7 @@ func SendPlayerInformation(nickname, id):
"id" : id, "id" : id,
"score": 0 "score": 0
} }
playerList.add_item(str(nickname), null, false)
if multiplayer.is_server(): if multiplayer.is_server():
print(me, ": Is Server!") print(me, ": Is Server!")
for i in GameManager.players: for i in GameManager.players:
@ -68,33 +85,32 @@ func StartGame():
print(me, ": Starting Game\n", me, ": Players: ", GameManager.players) print(me, ": Starting Game\n", me, ": Players: ", GameManager.players)
var scene = load("res://test_map.tscn").instantiate() var scene = load("res://test_map.tscn").instantiate()
#get_tree().root.add_child(scene) #get_tree().root.add_child(scene)
$Map.add_child(scene) map_root.add_child(scene)
self.hide() self.hide()
func hostGame(): func _on_host_button_down():
peer = ENetMultiplayerPeer.new() peer = ENetMultiplayerPeer.new()
var error = peer.create_server(port, 2) var error = peer.create_server(port, 2)
if error != OK: if error != OK:
print("cannot host: " + error) print("cannot host: " + error)
return return
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER) peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer) multiplayer.set_multiplayer_peer(peer)
print("Waiting For Players!") print("Waiting For Players!")
SendPlayerInformation(player_name.text, multiplayer.get_unique_id())
$MainMenu.hide()
$Lobby.show()
func _on_host_button_down(): start_button.disabled = false
hostGame()
SendPlayerInformation($Nickname.text, multiplayer.get_unique_id())
#SendPlayerInformation(nick, multiplayer.get_unique_id()) #SendPlayerInformation(nick, multiplayer.get_unique_id())
func _on_join_button_down(): func _on_join_button_down():
peer = ENetMultiplayerPeer.new() peer = ENetMultiplayerPeer.new()
peer.create_client(address, port) peer.create_client(address, port)
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER) peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer) multiplayer.set_multiplayer_peer(peer)
$MainMenu.hide()
$Lobby.show()
func _on_start_button_down(): func _on_start_button_down():

View File

@ -1,39 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://c0tixmwdmp28k"]
[ext_resource type="Script" path="res://MultiplayerController.gd" id="1_i2fn6"]
[node name="Control" type="CanvasLayer"]
script = ExtResource("1_i2fn6")
[node name="Host" type="Button" parent="."]
offset_right = 8.0
offset_bottom = 8.0
text = "Host
"
[node name="Join" type="Button" parent="."]
offset_left = 5.0
offset_top = 33.0
offset_right = 43.0
offset_bottom = 64.0
text = "Join"
[node name="Start" type="Button" parent="."]
offset_left = 5.0
offset_top = 95.0
offset_right = 51.0
offset_bottom = 126.0
text = "Start"
[node name="Nickname" type="TextEdit" parent="."]
offset_left = 26.0
offset_top = 138.0
offset_right = 378.0
offset_bottom = 170.0
placeholder_text = "nickname"
[node name="Map" type="Node" parent="."]
[connection signal="button_down" from="Host" to="." method="_on_host_button_down"]
[connection signal="button_down" from="Join" to="." method="_on_join_button_down"]
[connection signal="button_down" from="Start" to="." method="_on_start_button_down"]

112
main.tscn Normal file
View File

@ -0,0 +1,112 @@
[gd_scene load_steps=2 format=3 uid="uid://c41wi0dxo2eip"]
[ext_resource type="Script" path="res://MainMenu.gd" id="1_snhhs"]
[node name="Main" type="Node3D"]
[node name="Map" type="Node3D" parent="."]
[node name="UI" type="CanvasLayer" parent="." node_paths=PackedStringArray("player_name", "map_root", "host_button", "join_button", "address_input", "playerList", "start_button")]
script = ExtResource("1_snhhs")
player_name = NodePath("MainMenu/TabContainer/Settings/name/NameLineEdit")
map_root = NodePath("../Map")
host_button = NodePath("MainMenu/TabContainer/Host/HostButton")
join_button = NodePath("MainMenu/TabContainer/Join/Button")
address_input = NodePath("MainMenu/TabContainer/Join/Server/LineEdit")
playerList = NodePath("Lobby/MarginContainer/VBoxContainer/PlayerItemList")
start_button = NodePath("Lobby/MarginContainer/VBoxContainer/StartButton")
[node name="MainMenu" type="PanelContainer" parent="UI"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -139.5
offset_top = -60.5
offset_right = 139.5
offset_bottom = 60.5
grow_horizontal = 2
grow_vertical = 2
[node name="TabContainer" type="TabContainer" parent="UI/MainMenu"]
layout_mode = 2
[node name="Host" type="CenterContainer" parent="UI/MainMenu/TabContainer"]
layout_mode = 2
[node name="HostButton" type="Button" parent="UI/MainMenu/TabContainer/Host"]
layout_mode = 2
text = "Start Game"
[node name="Join" type="VBoxContainer" parent="UI/MainMenu/TabContainer"]
visible = false
layout_mode = 2
[node name="Server" type="HBoxContainer" parent="UI/MainMenu/TabContainer/Join"]
layout_mode = 2
[node name="ServerLabel" type="Label" parent="UI/MainMenu/TabContainer/Join/Server"]
layout_mode = 2
text = "Server:"
[node name="LineEdit" type="LineEdit" parent="UI/MainMenu/TabContainer/Join/Server"]
layout_mode = 2
size_flags_horizontal = 3
text = "127.0.0.1"
[node name="Button" type="Button" parent="UI/MainMenu/TabContainer/Join"]
layout_mode = 2
text = "Connect"
[node name="Settings" type="VBoxContainer" parent="UI/MainMenu/TabContainer"]
visible = false
layout_mode = 2
[node name="name" type="HBoxContainer" parent="UI/MainMenu/TabContainer/Settings"]
layout_mode = 2
[node name="NameLabel" type="Label" parent="UI/MainMenu/TabContainer/Settings/name"]
layout_mode = 2
text = "Player Name:"
[node name="NameLineEdit" type="LineEdit" parent="UI/MainMenu/TabContainer/Settings/name"]
layout_mode = 2
size_flags_horizontal = 3
text = "Player"
[node name="Lobby" type="PanelContainer" parent="UI"]
visible = false
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -137.5
offset_top = -230.5
offset_right = 137.5
offset_bottom = 230.5
grow_horizontal = 2
grow_vertical = 2
[node name="MarginContainer" type="MarginContainer" parent="UI/Lobby"]
layout_mode = 2
theme_override_constants/margin_left = 10
theme_override_constants/margin_top = 10
theme_override_constants/margin_right = 10
theme_override_constants/margin_bottom = 10
[node name="VBoxContainer" type="VBoxContainer" parent="UI/Lobby/MarginContainer"]
layout_mode = 2
[node name="PlayersLabel" type="Label" parent="UI/Lobby/MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Players: "
[node name="PlayerItemList" type="ItemList" parent="UI/Lobby/MarginContainer/VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
[node name="StartButton" type="Button" parent="UI/Lobby/MarginContainer/VBoxContainer"]
layout_mode = 2
text = "Start!"

View File

@ -11,7 +11,7 @@ config_version=5
[application] [application]
config/name="game" config/name="game"
run/main_scene="res://Multiplayer.tscn" run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2", "Forward Plus") config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://Assets/icon.svg" config/icon="res://Assets/icon.svg"