godot-webrtc-mplayer-testing/scripts/global/global.gd

81 lines
2.1 KiB
GDScript

extends Node
onready var signaller_url = "ws://localhost:8888"
# onready var signaller_url = "wss://webrtc-signaller.deno.dev:443"
onready var ice_servers = [
{ "urls": ["stun:[::1]:3478", "stun:stun.l.google.com:19302"] },
# { "urls": ["stun:stun.l.google.com:19302"] }, # just google
# { "urls": ["stun:[::1]:3478"] }, # just localhost
]
const SignallerClient = preload("signaller_client.gd")
const WebRTCNegotiator = preload("webrtc_negotiator.gd")
onready var signaller_client = SignallerClient.new(signaller_url)
onready var negotiator = WebRTCNegotiator.new(ice_servers, signaller_client)
onready var onetime_cmd_flags = {
"--multiplayer": false,
"--create-lobby": false,
"--join-first-available-lobby": false,
}
func check_onetime_flag(flag):
var result = onetime_cmd_flags["--%s" % flag]
onetime_cmd_flags["--%s" % flag] = false
return result
func _ready():
signaller_client.connect("connected", self, "_signaller_connected")
negotiator.signaller_client = signaller_client
negotiator.ice_servers = ice_servers
add_child(negotiator)
for flag in onetime_cmd_flags.keys():
if flag in OS.get_cmdline_args():
onetime_cmd_flags[flag] = true
func goto_scene(scene_resource_name):
var _result = get_tree().change_scene("res://screens/%s.tscn" % scene_resource_name)
func main_menu():
negotiator.close()
goto_scene("main_menu")
func fake_singleplayer():
print("Faking network peer for singleplayer...")
negotiator.multiplayer.initialize(1, true)
get_tree().network_peer = negotiator.multiplayer
var peer: WebRTCPeerConnection = WebRTCPeerConnection.new()
peer.initialize()
negotiator.multiplayer.add_peer(peer, 1)
func start_singleplayer_game():
fake_singleplayer()
negotiator.close()
goto_game()
func goto_game():
goto_scene("game")
func _signaller_connected():
goto_scene("lobby_browser")
func lobby_browser():
negotiator.close()
negotiator.connect_to_signaller()
func lobby():
goto_scene("multiplayer_lobby")
func quit():
negotiator.close()
get_tree().quit()
func key_shortcut(code):
var sc = ShortCut.new()
sc.shortcut = InputEventKey.new()
sc.shortcut.scancode = code
return sc