126 lines
3.4 KiB
GDScript3
126 lines
3.4 KiB
GDScript3
|
|
extends Node
|
||
|
|
|
||
|
|
signal noray_connected
|
||
|
|
signal player_ready(id : int)
|
||
|
|
const NORAY_ADDRESS = "tomfol.io"
|
||
|
|
const NORAY_PORT = 8890
|
||
|
|
|
||
|
|
var is_host = false
|
||
|
|
var external_oid = ""
|
||
|
|
var debug = false
|
||
|
|
|
||
|
|
var playerDict: Dictionary[int,PlayerCharacter]
|
||
|
|
var alivePlayerDict: Dictionary[int,PlayerCharacter]
|
||
|
|
var currentMapLogic: MapLogic
|
||
|
|
|
||
|
|
var thisPlayer: PlayerCharacter #Player Character playing on this machiene, this value is obviously different for every player
|
||
|
|
|
||
|
|
@onready var errordialog = $/root/Main/Dialogs/ErrorDialog
|
||
|
|
@onready var start_btn : Button = $/root/Main/Mainmenu/Menu/MarginContainer/VBoxContainer/HBoxContainer4/VBoxContainer3/Start
|
||
|
|
@onready var main : Main = $/root/Main
|
||
|
|
|
||
|
|
func _ready():
|
||
|
|
Noray.on_connect_to_host.connect(on_noray_connected)
|
||
|
|
Noray.on_connect_nat.connect(handle_nat_connection)
|
||
|
|
#Noray.on_connect_relay.connect(handle_relay_connection)
|
||
|
|
|
||
|
|
Noray.connect_to_host(NORAY_ADDRESS, NORAY_PORT)
|
||
|
|
player_ready.connect(peer_connected)
|
||
|
|
multiplayer.peer_disconnected.connect(peer_disconnected)
|
||
|
|
func getCurrentMapLogic() -> MapLogic:
|
||
|
|
return currentMapLogic
|
||
|
|
func setCurrentMapLogic(toMap: MapLogic) -> void:
|
||
|
|
currentMapLogic = toMap
|
||
|
|
|
||
|
|
func peer_connected(id: int) -> void:
|
||
|
|
if !get_node("/root/Main/Players").get_child_count(): return
|
||
|
|
|
||
|
|
playerDict.set(id,get_node("/root/Main/Players/" + str(id)))
|
||
|
|
alivePlayerDict.set(id,get_node("/root/Main/Players/" + str(id)))
|
||
|
|
if !playerDict.get(id): playerDict.erase(id)
|
||
|
|
if !alivePlayerDict.get(id): alivePlayerDict.erase(id)
|
||
|
|
|
||
|
|
func peer_disconnected(id: int) -> void:
|
||
|
|
playerDict.erase(id)
|
||
|
|
alivePlayerDict.erase(id)
|
||
|
|
|
||
|
|
|
||
|
|
func on_noray_connected():
|
||
|
|
print("Connected to Noray server")
|
||
|
|
|
||
|
|
Noray.register_host()
|
||
|
|
await Noray.on_pid
|
||
|
|
await Noray.register_remote()
|
||
|
|
start_btn.disabled = false
|
||
|
|
noray_connected.emit()
|
||
|
|
|
||
|
|
func host():
|
||
|
|
var peer = ENetMultiplayerPeer.new()
|
||
|
|
var error = OK
|
||
|
|
if debug:
|
||
|
|
error = peer.create_server(1069)
|
||
|
|
else:
|
||
|
|
error = peer.create_server(Noray.local_port)
|
||
|
|
if error:
|
||
|
|
errordialog.title = "Error"
|
||
|
|
errordialog.dialog_text = "Failed to create Server: " + str(error_string(error) + " (" + str(error) + ")")
|
||
|
|
errordialog.popup_centered()
|
||
|
|
errordialog.get_ok_button().grab_focus()
|
||
|
|
main.close_network()
|
||
|
|
else:
|
||
|
|
multiplayer.multiplayer_peer = peer
|
||
|
|
is_host = true
|
||
|
|
|
||
|
|
func join(oid: String):
|
||
|
|
if debug:
|
||
|
|
var peer = ENetMultiplayerPeer.new()
|
||
|
|
var error = peer.create_client("localhost", 1069)
|
||
|
|
print("Join: " + error_string(error) + " " + str(error))
|
||
|
|
multiplayer.multiplayer_peer = peer
|
||
|
|
return
|
||
|
|
Noray.connect_nat(oid)
|
||
|
|
external_oid = oid
|
||
|
|
|
||
|
|
func handle_nat_connection(address, port):
|
||
|
|
var err = await connect_to_server(address, port)
|
||
|
|
|
||
|
|
if err != OK && !is_host:
|
||
|
|
print("NAT failed, using relay")
|
||
|
|
err = Noray.connect_relay(external_oid)
|
||
|
|
|
||
|
|
return err
|
||
|
|
|
||
|
|
func handle_relay_connection(address, port):
|
||
|
|
return await connect_to_server(address, port)
|
||
|
|
|
||
|
|
func connect_to_server(address, port):
|
||
|
|
var err = OK
|
||
|
|
if !is_host:
|
||
|
|
var udp = PacketPeerUDP.new()
|
||
|
|
udp.bind(Noray.local_port)
|
||
|
|
udp.set_dest_address(address, port)
|
||
|
|
|
||
|
|
err = await PacketHandshake.over_packet_peer(udp)
|
||
|
|
udp.close()
|
||
|
|
|
||
|
|
if err != OK:
|
||
|
|
if err != ERR_BUSY:
|
||
|
|
print("Handshake failed")
|
||
|
|
return err
|
||
|
|
else:
|
||
|
|
print("Handshake success")
|
||
|
|
|
||
|
|
var peer = ENetMultiplayerPeer.new()
|
||
|
|
err = peer.create_client(address, port, 0, 0, 0, Noray.local_port)
|
||
|
|
|
||
|
|
if err != OK:
|
||
|
|
return err
|
||
|
|
|
||
|
|
multiplayer.multiplayer_peer = peer
|
||
|
|
|
||
|
|
return OK
|
||
|
|
else:
|
||
|
|
err = await PacketHandshake.over_enet(multiplayer.multiplayer_peer.host, address, port)
|
||
|
|
|
||
|
|
return err
|