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
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue