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

81 lines
2.1 KiB
GDScript3
Raw Normal View History

2021-11-15 15:43:33 -06:00
extends Node
2021-12-08 23:39:56 -06:00
onready var signaller_url = "ws://localhost:8888"
# onready var signaller_url = "wss://webrtc-signaller.deno.dev:443"
2021-11-15 15:43:33 -06:00
2021-12-08 23:39:56 -06:00
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
]
2021-11-15 15:43:33 -06:00
2021-12-08 23:39:56 -06:00
const SignallerClient = preload("signaller_client.gd")
const WebRTCNegotiator = preload("webrtc_negotiator.gd")
2021-12-06 21:56:37 -06:00
2021-12-08 23:39:56 -06:00
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
2021-11-15 15:43:33 -06:00
2021-12-08 23:39:56 -06:00
func _ready():
2021-12-09 21:10:17 -06:00
signaller_client.connect("connected", self, "_signaller_connected")
2021-12-08 23:39:56 -06:00
negotiator.signaller_client = signaller_client
negotiator.ice_servers = ice_servers
add_child(negotiator)
2021-12-06 21:56:37 -06:00
2021-12-08 23:39:56 -06:00
for flag in onetime_cmd_flags.keys():
if flag in OS.get_cmdline_args():
onetime_cmd_flags[flag] = true
2021-12-06 21:56:37 -06:00
2021-11-17 08:18:12 -06:00
func goto_scene(scene_resource_name):
2021-12-07 16:43:37 -06:00
var _result = get_tree().change_scene("res://screens/%s.tscn" % scene_resource_name)
2021-11-15 16:25:13 -06:00
2021-11-17 08:18:12 -06:00
func main_menu():
2021-12-08 23:39:56 -06:00
negotiator.close()
2021-12-07 16:43:37 -06:00
goto_scene("main_menu")
2021-11-15 16:25:13 -06:00
2021-12-08 23:39:56 -06:00
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)
2021-11-17 08:18:12 -06:00
func start_singleplayer_game():
2021-12-08 23:39:56 -06:00
fake_singleplayer()
negotiator.close()
goto_game()
func goto_game():
2021-11-17 08:18:12 -06:00
goto_scene("game")
2021-11-15 15:43:33 -06:00
2021-11-17 16:24:00 -06:00
func _signaller_connected():
2021-12-07 16:43:37 -06:00
goto_scene("lobby_browser")
2021-11-17 16:24:00 -06:00
2021-11-17 13:57:45 -06:00
func lobby_browser():
2021-12-08 23:39:56 -06:00
negotiator.close()
negotiator.connect_to_signaller()
2021-11-15 16:25:13 -06:00
2021-11-17 13:57:45 -06:00
func lobby():
2021-12-07 16:43:37 -06:00
goto_scene("multiplayer_lobby")
2021-11-17 13:57:45 -06:00
2021-11-17 08:18:12 -06:00
func quit():
2021-12-08 23:39:56 -06:00
negotiator.close()
2021-11-17 08:18:12 -06:00
get_tree().quit()
2021-12-09 21:10:17 -06:00
func key_shortcut(code):
var sc = ShortCut.new()
sc.shortcut = InputEventKey.new()
sc.shortcut.scancode = code
return sc