Internet/Local/Hotseat working (still need to test for bugs). ALso get an RPC error because we switch scene...
This commit is contained in:
parent
c95b8186ab
commit
c4909db2a5
654 changed files with 18133 additions and 39 deletions
126
UI/Lobby/LANHost.gd
Normal file
126
UI/Lobby/LANHost.gd
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
extends VBoxContainer
|
||||
|
||||
var send_socket: PacketPeerUDP = PacketPeerUDP.new()
|
||||
@onready var timer = $Timer
|
||||
@onready var lobby_name = $VBoxContainer/HBoxContainer/LobbyName
|
||||
@onready var port = $VBoxContainer/HBoxContainer/Port
|
||||
@onready var host_button = $VBoxContainer/HostButton
|
||||
@onready var session_info = $VBoxContainer/SessionInfo
|
||||
@onready var stop_hosting_button = $VBoxContainer/StopHostingButton
|
||||
@onready var start_game_button = $VBoxContainer/StartGameButton
|
||||
|
||||
@onready var ip_list = $VBoxContainer/ScrollContainer/IPList
|
||||
|
||||
signal start_game
|
||||
|
||||
func update_ip_list() -> void:
|
||||
for child in ip_list.get_children():
|
||||
child.queue_free()
|
||||
|
||||
var ips = Array(IP.get_local_addresses())
|
||||
ips = ips.filter(func filter_func(element): return element not in ["127.0.0.1", "0:0:0:0:0:0:0:1"])
|
||||
|
||||
for ip in ips:
|
||||
var ip_label: RichTextLabel = RichTextLabel.new()
|
||||
ip_label.text = str(ip)
|
||||
ip_label.selection_enabled = true
|
||||
ip_label.custom_minimum_size.y = 32
|
||||
#ip_label.size_flags_vertical |= Control.SIZE_EXPAND
|
||||
ip_label.size_flags_horizontal |= Control.SIZE_EXPAND
|
||||
ip_list.add_child(ip_label)
|
||||
|
||||
func start_broadcasting() -> void:
|
||||
send_socket.set_broadcast_enabled(true)
|
||||
send_socket.set_dest_address("255.255.255.255", 27475)
|
||||
timer.start()
|
||||
|
||||
update_ip_list()
|
||||
|
||||
func stop_broadcasting() -> void:
|
||||
send_socket.close()
|
||||
timer.stop()
|
||||
|
||||
func start_hosting() -> void:
|
||||
lobby_name.editable = false
|
||||
port.editable = false
|
||||
host_button.disabled = true
|
||||
stop_hosting_button.disabled = false
|
||||
session_info.text = "Waiting for opponent..."
|
||||
|
||||
start_broadcasting()
|
||||
|
||||
func stop_hosting() -> void:
|
||||
lobby_name.editable = true
|
||||
port.editable = true
|
||||
host_button.disabled = false
|
||||
stop_hosting_button.disabled = true
|
||||
|
||||
Networking.close_enet_server()
|
||||
|
||||
stop_broadcasting()
|
||||
|
||||
|
||||
func show_panel() -> void:
|
||||
visible = true
|
||||
#start_broadcasting()
|
||||
pass
|
||||
|
||||
func hide_panel() -> void:
|
||||
visible = false
|
||||
stop_broadcasting()
|
||||
session_info.text = ""
|
||||
Networking.close_enet_server()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
multiplayer.peer_connected.connect(_on_peer_connected)
|
||||
multiplayer.peer_disconnected.connect(_on_peer_disconnected)
|
||||
|
||||
func _on_peer_connected(id: int) -> void:
|
||||
session_info.text = "Opponent connected"
|
||||
start_game_button.disabled = false
|
||||
GameData.peer_id = id
|
||||
|
||||
func _on_peer_disconnected(id: int) -> void:
|
||||
session_info.text = "Opponent disconnected"
|
||||
start_game_button.disabled = true
|
||||
GameData.peer_id = 1
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
|
||||
func _on_timer_timeout():
|
||||
var lname = lobby_name.text
|
||||
|
||||
var raw_data = {
|
||||
"lobby_name": lname,
|
||||
"port": port.text
|
||||
}
|
||||
|
||||
var data = JSON.stringify(raw_data)
|
||||
|
||||
send_socket.put_packet(data.to_utf8_buffer())
|
||||
|
||||
|
||||
func _on_button_pressed():
|
||||
start_hosting()
|
||||
Networking.create_enet_server(int(port.text))
|
||||
|
||||
|
||||
func update_button_states() -> void:
|
||||
host_button.disabled = lobby_name.text.is_empty() or port.text.is_empty()
|
||||
|
||||
func _on_port_text_changed(new_text):
|
||||
update_button_states()
|
||||
|
||||
|
||||
func _on_lobby_name_text_changed(new_text):
|
||||
update_button_states()
|
||||
|
||||
|
||||
func _on_stop_hosting_button_pressed():
|
||||
session_info.text = "Closed server"
|
||||
|
||||
stop_hosting()
|
||||
139
UI/Lobby/LANJoin.gd
Normal file
139
UI/Lobby/LANJoin.gd
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
extends VBoxContainer
|
||||
|
||||
var recv_socket: UDPServer = UDPServer.new()
|
||||
var socket_created: bool = false
|
||||
|
||||
const LOBBY_ENTRY = preload("res://UI/Lobby/LobbyEntry.tscn")
|
||||
@onready var lobby_list = $VBoxContainer/ScrollContainer/LobbyList
|
||||
@onready var connect_button = $VBoxContainer/ConnectButton
|
||||
@onready var ip = $VBoxContainer/HBoxContainer/IP
|
||||
@onready var port = $VBoxContainer/HBoxContainer2/Port
|
||||
@onready var refresh_button = $VBoxContainer/RefreshButton
|
||||
@onready var disconnect_button = $VBoxContainer/DisconnectButton
|
||||
|
||||
@onready var session_info = $VBoxContainer/SessionInfo
|
||||
|
||||
var connected: bool = false
|
||||
|
||||
func start_listening() -> void:
|
||||
recv_socket.listen(27475)
|
||||
|
||||
func stop_listening() -> void:
|
||||
recv_socket.stop()
|
||||
listed_servers.clear()
|
||||
for child in lobby_list.get_children():
|
||||
child.queue_free()
|
||||
|
||||
func show_panel() -> void:
|
||||
visible = true
|
||||
session_info.text = ""
|
||||
start_listening()
|
||||
|
||||
func hide_panel() -> void:
|
||||
visible = false
|
||||
stop_listening()
|
||||
Networking.close_enet_client()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
multiplayer.connection_failed.connect(_on_connection_failed)
|
||||
multiplayer.connected_to_server.connect(_on_connected_to_server)
|
||||
multiplayer.server_disconnected.connect(_on_server_disconnected)
|
||||
|
||||
func _on_connection_failed() -> void:
|
||||
connected = false
|
||||
connect_button.disabled = false
|
||||
refresh_button.disabled = false
|
||||
disconnect_button.disabled = true
|
||||
ip.editable = true
|
||||
port.editable = true
|
||||
session_info.text = "Connection failed"
|
||||
|
||||
func _on_connected_to_server() -> void:
|
||||
connected = true
|
||||
session_info.text = "Connected. Waiting for host to start the game"
|
||||
connect_button.disabled = true
|
||||
refresh_button.disabled = true
|
||||
disconnect_button.disabled = false
|
||||
ip.editable = false
|
||||
port.editable = false
|
||||
_on_refresh_button_pressed()
|
||||
|
||||
func _on_server_disconnected() -> void:
|
||||
connected = false
|
||||
connect_button.disabled = false
|
||||
refresh_button.disabled = false
|
||||
disconnect_button.disabled = true
|
||||
ip.editable = true
|
||||
port.editable = true
|
||||
session_info.text = "Server closed the connection"
|
||||
|
||||
var listed_servers: Dictionary = {}
|
||||
|
||||
func add_server(ip: String, port: int, data: PackedByteArray) -> void:
|
||||
print(data.get_string_from_utf8())
|
||||
var json = JSON.parse_string(data.get_string_from_utf8())
|
||||
|
||||
var game_port: int = int(json["port"])
|
||||
var lobby_name: String = String(json["lobby_name"])
|
||||
var connect_ip: String = ip
|
||||
var id = ip + ":" + str(game_port)
|
||||
json["ip"] = ip
|
||||
|
||||
if listed_servers.has(id):
|
||||
print("Lobby already listed")
|
||||
return
|
||||
|
||||
listed_servers[id] = json
|
||||
print(listed_servers)
|
||||
|
||||
# add visual to list
|
||||
var entry = LOBBY_ENTRY.instantiate()
|
||||
entry.lobby_data = json
|
||||
|
||||
entry.connect_pressed.connect(_on_connect_pressed)
|
||||
|
||||
lobby_list.add_child(entry)
|
||||
|
||||
|
||||
func _on_connect_pressed(data: Dictionary) -> void:
|
||||
session_info.text = "Trying to connect..."
|
||||
Networking.close_enet_client()
|
||||
Networking.connect_with_enet(data["ip"], int(data["port"]))
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
recv_socket.poll()
|
||||
if recv_socket.is_connection_available():
|
||||
if connected:
|
||||
return
|
||||
var peer: PacketPeerUDP = recv_socket.take_connection()
|
||||
add_server(peer.get_packet_ip(), peer.get_packet_port(), peer.get_packet())
|
||||
|
||||
|
||||
func _on_refresh_button_pressed():
|
||||
listed_servers.clear()
|
||||
|
||||
for child in lobby_list.get_children():
|
||||
child.queue_free()
|
||||
|
||||
func _update_button_states() -> void:
|
||||
connect_button.disabled = ip.text.is_empty() or port.text.is_empty()
|
||||
|
||||
func _on_connect_button_pressed():
|
||||
session_info.text = "Trying to connect..."
|
||||
Networking.close_enet_client()
|
||||
Networking.connect_with_enet(ip.text, int(port.text))
|
||||
|
||||
|
||||
func _on_ip_text_changed(new_text):
|
||||
_update_button_states()
|
||||
|
||||
|
||||
func _on_port_text_changed(new_text):
|
||||
_update_button_states()
|
||||
|
||||
|
||||
func _on_disconnect_button_pressed():
|
||||
session_info.text = "Disconnected"
|
||||
Networking.close_enet_client()
|
||||
53
UI/Lobby/LANLobby.gd
Normal file
53
UI/Lobby/LANLobby.gd
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
extends Control
|
||||
|
||||
# Create an UDPServer to receive UDP Broadcast packets
|
||||
# List all received games in a list
|
||||
@onready var margin_container = $PanelContainer/MarginContainer
|
||||
@onready var selection_menu = $PanelContainer/SelectionMenu
|
||||
@onready var lan_host = $PanelContainer/MarginContainer/VBoxContainer2/LANHost
|
||||
@onready var lan_join = $PanelContainer/MarginContainer/VBoxContainer2/LANJoin
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
|
||||
func _on_back_button_pressed():
|
||||
margin_container.visible = false
|
||||
selection_menu.visible = true
|
||||
|
||||
lan_host.hide_panel()
|
||||
lan_join.hide_panel()
|
||||
|
||||
|
||||
func _on_backto_main_menu_pressed():
|
||||
get_tree().change_scene_to_file("res://UI/main_menu.tscn")
|
||||
|
||||
|
||||
func _on_host_pressed():
|
||||
selection_menu.visible = false
|
||||
margin_container.visible = true
|
||||
|
||||
lan_host.show_panel()
|
||||
|
||||
func _on_join_pressed():
|
||||
selection_menu.visible = false
|
||||
margin_container.visible = true
|
||||
|
||||
lan_join.show_panel()
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func load_game() -> void:
|
||||
push_warning("@@@@")
|
||||
push_warning(multiplayer.is_server())
|
||||
get_tree().change_scene_to_file("res://Game.tscn")
|
||||
push_warning("yoyoyoyo")
|
||||
|
||||
func _on_start_game_button_pressed():
|
||||
load_game.rpc()
|
||||
|
||||
230
UI/Lobby/LANLobby.tscn
Normal file
230
UI/Lobby/LANLobby.tscn
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://chu7mm2kn613g"]
|
||||
|
||||
[ext_resource type="Script" path="res://UI/Lobby/LANLobby.gd" id="1_cuhs7"]
|
||||
[ext_resource type="Script" path="res://UI/Lobby/LANJoin.gd" id="2_dcv2l"]
|
||||
[ext_resource type="Script" path="res://UI/Lobby/LANHost.gd" id="2_ioo6d"]
|
||||
|
||||
[node name="LANLobby" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_cuhs7")
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -135.0
|
||||
offset_top = -65.5
|
||||
offset_right = 135.0
|
||||
offset_bottom = 65.5
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="SelectionMenu" type="MarginContainer" parent="PanelContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 4
|
||||
size_flags_vertical = 4
|
||||
theme_override_constants/margin_left = 50
|
||||
theme_override_constants/margin_top = 50
|
||||
theme_override_constants/margin_right = 50
|
||||
theme_override_constants/margin_bottom = 50
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/SelectionMenu"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Host" type="Button" parent="PanelContainer/SelectionMenu/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Host"
|
||||
|
||||
[node name="Join" type="Button" parent="PanelContainer/SelectionMenu/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Join"
|
||||
|
||||
[node name="BacktoMainMenu" type="Button" parent="PanelContainer/SelectionMenu/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Back"
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="PanelContainer"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_left = 50
|
||||
theme_override_constants/margin_top = 50
|
||||
theme_override_constants/margin_right = 50
|
||||
theme_override_constants/margin_bottom = 50
|
||||
|
||||
[node name="VBoxContainer2" type="VBoxContainer" parent="PanelContainer/MarginContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 50
|
||||
|
||||
[node name="LANHost" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
script = ExtResource("2_ioo6d")
|
||||
|
||||
[node name="Timer" type="Timer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost"]
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost"]
|
||||
layout_mode = 2
|
||||
text = "Host"
|
||||
|
||||
[node name="HSeparator" type="HSeparator" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Lobby-Name"
|
||||
|
||||
[node name="LobbyName" type="LineEdit" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_constants/minimum_character_width = 16
|
||||
alignment = 2
|
||||
max_length = 16
|
||||
|
||||
[node name="Label2" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Port"
|
||||
|
||||
[node name="Port" type="LineEdit" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_constants/minimum_character_width = 16
|
||||
text = "27474"
|
||||
alignment = 2
|
||||
max_length = 16
|
||||
|
||||
[node name="HostButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Host"
|
||||
|
||||
[node name="ScrollContainer" type="ScrollContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer"]
|
||||
custom_minimum_size = Vector2(0, 100)
|
||||
layout_mode = 2
|
||||
horizontal_scroll_mode = 0
|
||||
|
||||
[node name="IPList" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/ScrollContainer"]
|
||||
custom_minimum_size = Vector2(0, 200)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="StopHostingButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Stop Hosting"
|
||||
|
||||
[node name="SessionInfo" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Waiting for opponent...."
|
||||
|
||||
[node name="StartGameButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Start Game"
|
||||
|
||||
[node name="LANJoin" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
script = ExtResource("2_dcv2l")
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin"]
|
||||
layout_mode = 2
|
||||
text = "Join"
|
||||
|
||||
[node name="HSeparator" type="HSeparator" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "IP"
|
||||
|
||||
[node name="IP" type="LineEdit" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_constants/minimum_character_width = 16
|
||||
alignment = 2
|
||||
max_length = 16
|
||||
|
||||
[node name="HBoxContainer2" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/HBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = "Port"
|
||||
|
||||
[node name="Port" type="LineEdit" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/HBoxContainer2"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_constants/minimum_character_width = 16
|
||||
text = "27474"
|
||||
alignment = 2
|
||||
max_length = 16
|
||||
|
||||
[node name="ConnectButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Connect"
|
||||
|
||||
[node name="HSeparator2" type="HSeparator" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="ScrollContainer" type="ScrollContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
custom_minimum_size = Vector2(0, 100)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="LobbyList" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/ScrollContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="RefreshButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Refresh"
|
||||
|
||||
[node name="SessionInfo" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "#INFO"
|
||||
|
||||
[node name="DisconnectButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Disconnect"
|
||||
|
||||
[node name="BackButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = "Back"
|
||||
|
||||
[connection signal="pressed" from="PanelContainer/SelectionMenu/VBoxContainer/Host" to="." method="_on_host_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/SelectionMenu/VBoxContainer/Join" to="." method="_on_join_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/SelectionMenu/VBoxContainer/BacktoMainMenu" to="." method="_on_backto_main_menu_pressed"]
|
||||
[connection signal="timeout" from="PanelContainer/MarginContainer/VBoxContainer2/LANHost/Timer" to="PanelContainer/MarginContainer/VBoxContainer2/LANHost" method="_on_timer_timeout"]
|
||||
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/HBoxContainer/LobbyName" to="PanelContainer/MarginContainer/VBoxContainer2/LANHost" method="_on_lobby_name_text_changed"]
|
||||
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/HBoxContainer/Port" to="PanelContainer/MarginContainer/VBoxContainer2/LANHost" method="_on_port_text_changed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/HostButton" to="PanelContainer/MarginContainer/VBoxContainer2/LANHost" method="_on_button_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/StopHostingButton" to="PanelContainer/MarginContainer/VBoxContainer2/LANHost" method="_on_stop_hosting_button_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/LANHost/VBoxContainer/StartGameButton" to="." method="_on_start_game_button_pressed"]
|
||||
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/HBoxContainer/IP" to="PanelContainer/MarginContainer/VBoxContainer2/LANJoin" method="_on_ip_text_changed"]
|
||||
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/HBoxContainer2/Port" to="PanelContainer/MarginContainer/VBoxContainer2/LANJoin" method="_on_port_text_changed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/ConnectButton" to="PanelContainer/MarginContainer/VBoxContainer2/LANJoin" method="_on_connect_button_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/RefreshButton" to="PanelContainer/MarginContainer/VBoxContainer2/LANJoin" method="_on_refresh_button_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/LANJoin/VBoxContainer/DisconnectButton" to="PanelContainer/MarginContainer/VBoxContainer2/LANJoin" method="_on_disconnect_button_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/BackButton" to="." method="_on_back_button_pressed"]
|
||||
13
UI/Lobby/LobbyEntry.gd
Normal file
13
UI/Lobby/LobbyEntry.gd
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
extends HBoxContainer
|
||||
|
||||
signal connect_pressed
|
||||
|
||||
var lobby_data: Dictionary = {}
|
||||
|
||||
@onready var label = $Label
|
||||
|
||||
func _ready() -> void:
|
||||
label.text = lobby_data.get("lobby_name", "Unknown")
|
||||
|
||||
func _on_button_pressed():
|
||||
connect_pressed.emit(lobby_data)
|
||||
19
UI/Lobby/LobbyEntry.tscn
Normal file
19
UI/Lobby/LobbyEntry.tscn
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://b6omw0ynhc15i"]
|
||||
|
||||
[ext_resource type="Script" path="res://UI/Lobby/LobbyEntry.gd" id="1_bjjj1"]
|
||||
|
||||
[node name="LobbyEntry" type="HBoxContainer"]
|
||||
size_flags_vertical = 3
|
||||
script = ExtResource("1_bjjj1")
|
||||
|
||||
[node name="Label" type="Label" parent="."]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "LobbyName"
|
||||
|
||||
[node name="Button" type="Button" parent="."]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 8
|
||||
text = "Connect"
|
||||
|
||||
[connection signal="pressed" from="Button" to="." method="_on_button_pressed"]
|
||||
54
UI/Lobby/WebRTCHost.gd
Normal file
54
UI/Lobby/WebRTCHost.gd
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
extends VBoxContainer
|
||||
|
||||
@onready var lobby_label = $VBoxContainer/HBoxContainer/LobbyLabel
|
||||
@onready var session_info = $VBoxContainer/SessionInfo
|
||||
@onready var start_game_button = $VBoxContainer/StartGameButton
|
||||
|
||||
|
||||
func start_hosting() -> void:
|
||||
Networking.start_webrtc_lobby()
|
||||
|
||||
func stop_hosting() -> void:
|
||||
Networking.stop_webrtc()
|
||||
|
||||
lobby_label.text = ""
|
||||
session_info.text = ""
|
||||
|
||||
func show_panel() -> void:
|
||||
lobby_label.text = ""
|
||||
start_hosting()
|
||||
visible = true
|
||||
|
||||
func hide_panel() -> void:
|
||||
stop_hosting()
|
||||
start_game_button.disabled = true
|
||||
visible = false
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
Networking.ws_client.lobby_joined.connect(_on_lobby_joined)
|
||||
lobby_label.text = ""
|
||||
multiplayer.peer_connected.connect(_on_peer_connected)
|
||||
multiplayer.peer_disconnected.connect(_on_peer_disconnected)
|
||||
|
||||
func _on_peer_connected(id: int) -> void:
|
||||
GameData.peer_id = id
|
||||
session_info.text = "Opponent connected"
|
||||
start_game_button.disabled = false
|
||||
|
||||
func _on_peer_disconnected(id: int) -> void:
|
||||
GameData.peer_id = 1
|
||||
session_info.text = "Opponent disconnected"
|
||||
start_game_button.disabled = true
|
||||
|
||||
func _on_lobby_joined(lobby_id: String) -> void:
|
||||
lobby_label.text = lobby_id
|
||||
session_info.text = "Waiting for opponent..."
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
|
||||
func _on_back_button_pressed():
|
||||
pass # Replace with function body.
|
||||
64
UI/Lobby/WebRTCJoin.gd
Normal file
64
UI/Lobby/WebRTCJoin.gd
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
extends VBoxContainer
|
||||
|
||||
@onready var connect_button = $VBoxContainer/ConnectButton
|
||||
@onready var lobby_code = $VBoxContainer/HBoxContainer/LobbyCode
|
||||
@onready var session_info = $VBoxContainer/SessionInfo
|
||||
@onready var disconnect_button = $VBoxContainer/DisconnectButton
|
||||
|
||||
func show_panel() -> void:
|
||||
session_info.text = ""
|
||||
visible = true
|
||||
pass
|
||||
|
||||
func hide_panel() -> void:
|
||||
visible = false
|
||||
disconnect_from_host()
|
||||
pass
|
||||
|
||||
func connect_to_host() -> void:
|
||||
session_info.text = "Connecting..."
|
||||
lobby_code.editable = false
|
||||
connect_button.disabled = true
|
||||
Networking.join_webrtc_lobby(lobby_code.text)
|
||||
|
||||
func disconnect_from_host() -> void:
|
||||
session_info.text = "Disconnected"
|
||||
lobby_code.editable = true
|
||||
connect_button.disabled = false
|
||||
Networking.stop_webrtc()
|
||||
disconnect_button.disabled = true
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
multiplayer.connected_to_server.connect(_on_connected_to_server)
|
||||
multiplayer.server_disconnected.connect(_on_server_disconnected)
|
||||
multiplayer.connection_failed.connect(_on_connection_failed)
|
||||
|
||||
func _on_connection_failed() -> void:
|
||||
session_info.text = "Connection failed"
|
||||
connect_button.disabled = false
|
||||
lobby_code.editable = true
|
||||
disconnect_from_host()
|
||||
|
||||
func _on_connected_to_server() -> void:
|
||||
session_info.text = "Connected. Waiting for host to start"
|
||||
disconnect_button.disabled = false
|
||||
|
||||
func _on_server_disconnected() -> void:
|
||||
session_info.text = "Server closed the connection"
|
||||
lobby_code.editable = true
|
||||
connect_button.disabled = false
|
||||
disconnect_from_host()
|
||||
|
||||
func _update_button_states() -> void:
|
||||
connect_button.disabled = lobby_code.text.is_empty()
|
||||
|
||||
func _on_lobby_code_text_changed(new_text):
|
||||
_update_button_states()
|
||||
|
||||
|
||||
func _on_connect_button_pressed():
|
||||
connect_to_host()
|
||||
|
||||
func _on_disconnect_button_pressed():
|
||||
disconnect_from_host()
|
||||
37
UI/Lobby/WebRTCLobby.gd
Normal file
37
UI/Lobby/WebRTCLobby.gd
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
extends Control
|
||||
|
||||
@onready var margin_container = $PanelContainer/MarginContainer
|
||||
@onready var web_rtc_host = $PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost
|
||||
@onready var web_rtc_join = $PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin
|
||||
@onready var selection_menu = $PanelContainer/SelectionMenu
|
||||
|
||||
func _on_host_pressed():
|
||||
selection_menu.visible = false
|
||||
margin_container.visible = true
|
||||
web_rtc_host.show_panel()
|
||||
|
||||
|
||||
func _on_join_pressed():
|
||||
selection_menu.visible = false
|
||||
margin_container.visible = true
|
||||
web_rtc_join.show_panel()
|
||||
|
||||
|
||||
func _on_backto_main_menu_pressed():
|
||||
get_tree().change_scene_to_file("res://UI/main_menu.tscn")
|
||||
|
||||
|
||||
func _on_back_button_pressed():
|
||||
selection_menu.visible = true
|
||||
margin_container.visible = false
|
||||
selection_menu.visible = true
|
||||
|
||||
web_rtc_host.hide_panel()
|
||||
web_rtc_join.hide_panel()
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func load_game() -> void:
|
||||
get_tree().change_scene_to_file("res://Game.tscn")
|
||||
|
||||
func _on_start_game_button_pressed():
|
||||
load_game.rpc()
|
||||
162
UI/Lobby/WebRTCLobby.tscn
Normal file
162
UI/Lobby/WebRTCLobby.tscn
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://d1yxx75f8ad2e"]
|
||||
|
||||
[ext_resource type="Script" path="res://UI/Lobby/WebRTCLobby.gd" id="1_d0ifh"]
|
||||
[ext_resource type="Script" path="res://UI/Lobby/WebRTCHost.gd" id="2_h54u8"]
|
||||
[ext_resource type="Script" path="res://UI/Lobby/WebRTCJoin.gd" id="3_yero6"]
|
||||
|
||||
[node name="WebRtcLobby" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_d0ifh")
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -135.0
|
||||
offset_top = -65.5
|
||||
offset_right = 135.0
|
||||
offset_bottom = 65.5
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="SelectionMenu" type="MarginContainer" parent="PanelContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 4
|
||||
size_flags_vertical = 4
|
||||
theme_override_constants/margin_left = 50
|
||||
theme_override_constants/margin_top = 50
|
||||
theme_override_constants/margin_right = 50
|
||||
theme_override_constants/margin_bottom = 50
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/SelectionMenu"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Host" type="Button" parent="PanelContainer/SelectionMenu/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Host"
|
||||
|
||||
[node name="Join" type="Button" parent="PanelContainer/SelectionMenu/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Join"
|
||||
|
||||
[node name="BacktoMainMenu" type="Button" parent="PanelContainer/SelectionMenu/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Back"
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="PanelContainer"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_left = 50
|
||||
theme_override_constants/margin_top = 50
|
||||
theme_override_constants/margin_right = 50
|
||||
theme_override_constants/margin_bottom = 50
|
||||
|
||||
[node name="VBoxContainer2" type="VBoxContainer" parent="PanelContainer/MarginContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 50
|
||||
|
||||
[node name="WebRTCHost" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
script = ExtResource("2_h54u8")
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost"]
|
||||
layout_mode = 2
|
||||
text = "Host"
|
||||
|
||||
[node name="HSeparator" type="HSeparator" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Lobby-Code:"
|
||||
|
||||
[node name="LobbyLabel" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "XXXXXX"
|
||||
|
||||
[node name="HSeparator2" type="HSeparator" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="SessionInfo" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Waiting for opponent...."
|
||||
|
||||
[node name="StartGameButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Start Game"
|
||||
|
||||
[node name="WebRTCJoin" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
script = ExtResource("3_yero6")
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin"]
|
||||
layout_mode = 2
|
||||
text = "Join"
|
||||
|
||||
[node name="HSeparator" type="HSeparator" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Lobby-Code"
|
||||
|
||||
[node name="LobbyCode" type="LineEdit" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_constants/minimum_character_width = 6
|
||||
alignment = 2
|
||||
max_length = 6
|
||||
|
||||
[node name="ConnectButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Connect"
|
||||
|
||||
[node name="HSeparator2" type="HSeparator" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="SessionInfo" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "#INFO"
|
||||
|
||||
[node name="DisconnectButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Disconnect"
|
||||
|
||||
[node name="BackButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer2"]
|
||||
layout_mode = 2
|
||||
text = "Back"
|
||||
|
||||
[connection signal="pressed" from="PanelContainer/SelectionMenu/VBoxContainer/Host" to="." method="_on_host_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/SelectionMenu/VBoxContainer/Join" to="." method="_on_join_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/SelectionMenu/VBoxContainer/BacktoMainMenu" to="." method="_on_backto_main_menu_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/WebRTCHost/VBoxContainer/StartGameButton" to="." method="_on_start_game_button_pressed"]
|
||||
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer/HBoxContainer/LobbyCode" to="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin" method="_on_lobby_code_text_changed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer/ConnectButton" to="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin" method="_on_connect_button_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin/VBoxContainer/DisconnectButton" to="PanelContainer/MarginContainer/VBoxContainer2/WebRTCJoin" method="_on_disconnect_button_pressed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer2/BackButton" to="." method="_on_back_button_pressed"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue