summaryrefslogtreecommitdiff
path: root/scripts/MainScene.gd
blob: c15ed1cdac4b54140a647310ebe5da139bce8776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
extends Control

onready var GC = $GCClient
onready var MM = $MainMenu

enum { MENU, INGAME }
var game_state : int = MENU

onready var game_scene_base : PackedScene = preload("res://pages/GameTable.tscn")
var game_scene = null

func _ready():
	if OS.get_name() == "HTML5": # running on web
		var lobby_id = JavaScript.eval("new URLSearchParams(document.location.search).get('lobby_id')")
		var gc_url = JavaScript.eval("new URLSearchParams(document.location.search).get('gc_url')")
		var password = JavaScript.eval("new URLSearchParams(document.location.search).get('pw')")
		if gc_url:
			Globals.update_gc_url(gc_url)
		if lobby_id:
			var pw : String = password if password else ""
			MM.join_menu_button_pressed(lobby_id, pw)
	
	GC.connect("join_request_success", self, "on_join_request_success")
	GC.connect("host_success", self, "on_host_success")
	GC.connect("host_failure", self, "on_host_failure")
	GC.connect("join_success", self, "on_join_success")
	GC.connect("join_failure", self, "on_join_failure")
	GC.connect("gc_connection_failed", self, "on_gc_connection_failure")

	MM.connect("game_host_request", GC, "host_lobby")
	MM.connect("game_join_request", GC, "join_lobby")

func _process(_delta):
	if GC.state > 1 and (game_state == MENU):
		GC.receive()

func to_main_menu():
	if game_state == MENU: return
	if game_scene:
		remove_child(game_scene)
		game_scene.queue_free()
	GC.reset_gc_client()
	GC.close_socket()
	MM.visible = true
	game_state = MENU

func to_game(args = null):
	if game_state == INGAME: return
	MM.close_popup()
	MM.visible = false
	game_scene = game_scene_base.instance()
	game_scene.set_up(GC)
	if args:
		game_scene.set_up_args(args)
	add_child(game_scene)
	game_state = INGAME

# GC FUNCTIONS

func on_gc_connection_failure():
	MM.open_popup("Error", "Could not connect to game coordinator.")

func on_join_request_success():
	MM.close_popup()
	MM.open_popup("Connected", "Waiting for acknowledgment from host...")

func on_host_success():
	to_game()

func on_host_failure(reason):
	MM.open_popup("Error", "Could not host game: %s" % reason)

func on_join_success(join_args):
	to_game(join_args)

func on_join_failure(reason):
	MM.open_popup("Error", "Could not join game: %s" % reason)