-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.gd
107 lines (89 loc) · 2.31 KB
/
global.gd
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
extends Node
const INTRO := preload("res://scenes/gamestates/intro.tscn")
const GAME := preload("res://scenes/gamestates/game.tscn")
const ENDING := preload("res://scenes/gamestates/ending.tscn")
const POPUP := preload("res://scenes/gamestates/popup_menu.tscn")
const MENU := preload("res://scenes/gamestates/menu.tscn")
const SCENE_TRANSITIONER := preload("res://scenes/gamestates/scene_transitioner.tscn")
const GRAVITY := 30.0
const GAME_SEQUENCE = [
INTRO,
GAME,
ENDING,
]
enum STATE {
Menu,
Intro,
StartGame,
Ingame,
Won,
Died,
Pause,
BackToMenu,
RestartRoom,
Exiting,
Transitioning,
}
signal restart_room
var state: int = STATE.Menu
var gravity := GRAVITY
func _ready():
set_pause_mode(Node.PAUSE_MODE_PROCESS)
OS.min_window_size = OS.window_size
func _process(_delta):
if Input.is_action_just_pressed("toggle_fullscreen"):
OS.set_window_fullscreen(!OS.is_window_fullscreen())
func set_state(var new):
if new == state:
return
state = new
match state:
STATE.Intro:
get_tree().paused = true
transition_to(INTRO)
STATE.BackToMenu:
get_tree().paused = true
transition_to(MENU)
STATE.StartGame:
get_tree().paused = true
transition_to(GAME)
STATE.Won:
get_tree().paused = true
transition_to(ENDING)
STATE.Ingame:
get_tree().paused = false
STATE.Pause:
get_tree().paused = true
get_tree().get_root().add_child(POPUP.instance())
STATE.RestartRoom, STATE.Died:
emit_signal("restart_room")
set_state(STATE.Ingame)
STATE.Exiting:
get_tree().quit()
# Either PackedScene or path to scene.
func transition_to(var target_scene):
if state == STATE.Transitioning:
return
set_state(STATE.Transitioning)
var transitioner := SCENE_TRANSITIONER.instance()
transitioner.target_scene = target_scene
transitioner.connect("finished", self, "_on_transition_finished")
get_tree().get_root().add_child(transitioner)
func _on_transition_finished():
get_tree().paused = false
set_state(STATE.Ingame)
func play_sound(var stream: AudioStream, var pos = null):
var asp: Node
if pos is Vector2:
asp = AudioStreamPlayer2D.new()
asp.position = pos
elif pos is Vector3:
asp = AudioStreamPlayer3D.new()
asp.position = pos
else:
asp = AudioStreamPlayer.new()
asp.stream = stream
asp.bus = "Sound"
asp.connect("finished", asp, "queue_free")
add_child(asp)
asp.play()