Created a basic Lobby system, to further test networking features

This commit is contained in:
PersonGuyGit 2023-07-23 12:55:33 -06:00
parent bd6acd0611
commit 80787c9f2a
8 changed files with 214 additions and 10 deletions

View File

@ -1,5 +1,33 @@
extends Control extends Control
onready var player_name = $CenterContainer/VBoxContainer/GridContainer/NameLineEdit
func _on_Button_pressed(): onready var selected_IP = $CenterContainer/VBoxContainer/GridContainer/IPLineEdit
onready var selected_port = $CenterContainer/VBoxContainer/GridContainer/PortLineEdit
onready var waiting_room = $WaitingRoom
onready var ready_btn = $WaitingRoom/CenterContainer/VBoxContainer/ReadyButton
func _ready():
player_name.text = Save.save_data["player_name"]
selected_IP.text = Server.DEFAULT_IP
selected_port.text = str(Server.DEFAULT_PORT)
func show_waiting_room():
waiting_room.popup_centered()
func _on_ReadyBtn_pressed():
ready_btn.disabled = true
func _on_NameLineEdit_text_changed(_new_text):
Save.save_data["Player_name"] = player_name.text
Save.save_game()
func _on_JoinButton_pressed():
Server.selected_IP = selected_IP.text
Server.selected_port = int(selected_port.text)
Server._connect_to_server() Server._connect_to_server()
show_waiting_room()

View File

@ -1,21 +1,89 @@
[gd_scene load_steps=2 format=2] [gd_scene load_steps=3 format=2]
[ext_resource path="res://Lobby/Lobby.gd" type="Script" id=1] [ext_resource path="res://Lobby/Lobby.gd" type="Script" id=1]
[ext_resource path="res://Lobby/WatingRoom.tscn" type="PackedScene" id=2]
[node name="Control" type="Control"] [node name="Lobby" type="Control"]
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="ColorRect" type="ColorRect" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0.231373, 0.231373, 0.258824, 1 )
[node name="CenterContainer" type="CenterContainer" parent="."] [node name="CenterContainer" type="CenterContainer" parent="."]
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
[node name="Button" type="Button" parent="CenterContainer"] [node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"]
margin_left = 468.0 margin_left = 338.0
margin_top = 290.0 margin_top = 207.0
margin_right = 556.0 margin_right = 685.0
margin_bottom = 310.0 margin_bottom = 393.0
custom_constants/separation = 20
[node name="LobbyLabel" type="Label" parent="CenterContainer/VBoxContainer"]
margin_right = 347.0
margin_bottom = 14.0
text = "Lobby"
align = 1
[node name="GridContainer" type="GridContainer" parent="CenterContainer/VBoxContainer"]
margin_top = 34.0
margin_right = 347.0
margin_bottom = 146.0
custom_constants/vseparation = 20
custom_constants/hseparation = 20
columns = 2
[node name="NameLabel" type="Label" parent="CenterContainer/VBoxContainer/GridContainer"]
margin_top = 5.0
margin_right = 77.0
margin_bottom = 19.0
text = "Name:"
[node name="NameLineEdit" type="LineEdit" parent="CenterContainer/VBoxContainer/GridContainer"]
margin_left = 97.0
margin_right = 347.0
margin_bottom = 24.0
rect_min_size = Vector2( 250, 0 )
[node name="IPLabel" type="Label" parent="CenterContainer/VBoxContainer/GridContainer"]
margin_top = 49.0
margin_right = 77.0
margin_bottom = 63.0
text = "IP Address:"
[node name="IPLineEdit" type="LineEdit" parent="CenterContainer/VBoxContainer/GridContainer"]
margin_left = 97.0
margin_top = 44.0
margin_right = 347.0
margin_bottom = 68.0
rect_min_size = Vector2( 250, 0 )
[node name="PortLabel" type="Label" parent="CenterContainer/VBoxContainer/GridContainer"]
margin_top = 93.0
margin_right = 77.0
margin_bottom = 107.0
text = "Port:"
[node name="PortLineEdit" type="LineEdit" parent="CenterContainer/VBoxContainer/GridContainer"]
margin_left = 97.0
margin_top = 88.0
margin_right = 347.0
margin_bottom = 112.0
rect_min_size = Vector2( 250, 0 )
[node name="JoinButton" type="Button" parent="CenterContainer/VBoxContainer"]
margin_top = 166.0
margin_right = 347.0
margin_bottom = 186.0
text = "Join Session" text = "Join Session"
[connection signal="pressed" from="CenterContainer/Button" to="." method="_on_Button_pressed"] [node name="WaitingRoom" parent="." instance=ExtResource( 2 )]
visible = false
[connection signal="text_changed" from="CenterContainer/VBoxContainer/GridContainer/NameLineEdit" to="." method="_on_NameLineEdit_text_changed"]
[connection signal="pressed" from="CenterContainer/VBoxContainer/JoinButton" to="." method="_on_JoinButton_pressed"]

View File

@ -0,0 +1,12 @@
extends Popup
onready var player_list = $CenterContainer/VBoxContainer/ItemList
func _ready():
player_list.clear()
func refresh_players(players):
player_list.clear()
for player_id in players:
var player = players[player_id]["Player_name"]
player_list.add_item(player, null, false)

View File

@ -0,0 +1,47 @@
[gd_scene format=2]
[node name="WatingRoom" type="Popup" groups=["WaitingRoom"]]
visible = true
anchor_right = 1.0
anchor_bottom = 1.0
[node name="ColorRect" type="ColorRect" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0.109804, 0.129412, 0.243137, 1 )
[node name="CenterContainer" type="CenterContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"]
margin_left = 412.0
margin_top = 194.0
margin_right = 612.0
margin_bottom = 406.0
[node name="Label" type="Label" parent="CenterContainer/VBoxContainer"]
margin_right = 200.0
margin_bottom = 31.0
text = "Waiting Room
"
align = 1
valign = 1
[node name="ItemList" type="ItemList" parent="CenterContainer/VBoxContainer"]
margin_top = 35.0
margin_right = 200.0
margin_bottom = 188.0
rect_min_size = Vector2( 200, 0 )
custom_constants/vseparation = 10
items = [ "Player 1", null, false, "Player 2", null, false, "Player 3", null, false, "Player 4", null, false ]
auto_height = true
same_column_width = true
fixed_column_width = 200
icon_mode = 0
[node name="ReadyButton" type="Button" parent="CenterContainer/VBoxContainer"]
margin_top = 192.0
margin_right = 200.0
margin_bottom = 212.0
text = "Ready"

View File

@ -0,0 +1,29 @@
extends Node
const save_file = "user://save_file.json"
var save_data = {}
# Called when the node enters the scene tree for the first time.
func _ready():
save_data = get_data()
func get_data():
var file = File.new()
if not file.file_exists(save_file):
save_data = {"player_name": "Unnamed"}
save_game()
file.open(save_file, File.READ)
var content = file.get_as_text()
var data = parse_json(content)
save_data = data
file.close()
return(data)
func save_game():
var save_game = File.new()
save_game.open(save_file, File.WRITE)
save_game.store_line(to_json(save_data))

View File

@ -19,7 +19,7 @@ func _ready():
func _connect_to_server(): func _connect_to_server():
get_tree().connect("connected_to_server", self, "_connected_ok") get_tree().connect("connected_to_server", self, "_connected_ok")
network.create_client(DEFAULT_IP, DEFAULT_PORT) network.create_client(selected_IP, selected_port)
get_tree().set_network_peer(network) get_tree().set_network_peer(network)
func _player_connected(id): func _player_connected(id):
@ -30,9 +30,22 @@ func _player_disconnected(id):
func _connected_ok(): func _connected_ok():
print("Successfully connected to server") print("Successfully connected to server")
register_player()
rpc_id(1, "send_player_info", local_player_id, player_data)
func _connected_fail(): func _connected_fail():
print("Failed to connect") print("Failed to connect")
func _server_disconnected(): func _server_disconnected():
print("Server Disconnected") print("Server Disconnected")
func register_player():
local_player_id = get_tree().get_network_unique_id()
player_data = Save.save_data
players[local_player_id] = player_data
sync func update_waiting_room():
get_tree().call_group("WaitingRoom", "refresh_players", players)

View File

@ -17,6 +17,7 @@ config/icon="res://icon.png"
[autoload] [autoload]
Server="*res://Server/Server.gd" Server="*res://Server/Server.gd"
Save="*res://Save/Save.gd"
[gui] [gui]

View File

@ -4,6 +4,8 @@ var network = NetworkedMultiplayerENet.new()
var port = 8080 var port = 8080
var max_players = 4 var max_players = 4
var players = {}
func _ready(): func _ready():
start_server() start_server()
@ -21,3 +23,7 @@ func _player_connected(player_id):
func _player_disconnected(player_id): func _player_disconnected(player_id):
print("Player: " + str(player_id) + " Disconnected") print("Player: " + str(player_id) + " Disconnected")
remote func send_player_info(id, player_data):
players[id] = player_data
rset("players", players)
rpc("update_waiting_room")