127 lines
3.1 KiB
GDScript3
127 lines
3.1 KiB
GDScript3
|
|
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()
|