-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.gd
257 lines (221 loc) · 8.24 KB
/
GameManager.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
extends Node2D
class_name GameManager
@onready var spawn_area: Area2D = $"Box Spawn"
@onready var customer_spawn_area = $"Door Spawn"
@onready var box_spawn_timer = $"Timers/Box Spawn Timer"
@onready var customer_spawn_timer = $"Timers/Customer Spawn Timer"
@onready var clock_timer = $"Timers/Clock Timer"
@onready var standing_areas = [
$"Standing Areas/1",
$"Standing Areas/2",
$"Standing Areas/3",
$"Standing Areas/4",
$"Standing Areas/5"
]
var customers_waiting = []
var box_scene = preload("res://box.tscn")
var customer_scene = preload("res://customer.tscn")
var boxes_to_spawn = []
var write_ups: int = 0
@onready var manifest_container = $"GUI/Manifest/Contents/Vertical Container"
var manifest_entry = preload("res://manifest_entry.tscn")
var customers_around = 0
var time_left = 120 #in seconds
@onready var day_text = $"GUI/Day Label"
@onready var time_text = $"GUI/Timer Label"
@onready var black_screen = $"GUI/Black Screen"
@onready var continue_text = $"GUI/Black Screen/Continue Text"
@onready var lose_text = $"GUI/Black Screen/Game Over Text"
@onready var win_menu_button: Button = $"GUI/Black Screen/HBoxContainer/Win Main Menu"
@onready var win_next_level_button: Button = $"GUI/Black Screen/HBoxContainer/Win Next Level"
@onready var solo_menu_button: Button = $"GUI/Black Screen/Solo Main Menu"
@onready var fade_screen: ColorRect = $"GUI/Fade Screen"
@onready var review = $"GUI/Black Screen/Review"
@onready var profile_picture: TextureRect = $"GUI/Black Screen/Review/Profile Picture"
@onready var stars_label: Label = $"GUI/Black Screen/Review/Stars"
@onready var review_label: Label = $"GUI/Black Screen/Review/Review Text"
@onready var username_label: Label = $"GUI/Black Screen/Review/Username"
@onready var music: AudioStreamPlayer = $Music
var fade_tween: Tween
enum GameState {
GAME_START,
GAME_PLAYING,
GAME_OVER,
GAME_CONTINUE,
GAME_WIN
}
var game_state: GameState = GameState.GAME_START: set = set_game_state
signal on_game_state_changed(new_state)
var can_spawn_box = true
func _ready():
randomize()
music.volume_db = -80
var music_tween: Tween = get_tree().create_tween()
music_tween.tween_property(music,"volume_db", -35, 4.0)
generate_boxes(Global.levels[Global.level - 1].packages)
time_left = Global.levels[Global.level - 1].seconds
day_text.text = "Day " + str(Global.levels[Global.level - 1].level_name)
make_manifest()
box_spawn_timer.connect("timeout", Callable(self, "spawn_box"))
box_spawn_timer.start()
customer_spawn_timer.connect("timeout", Callable(self, "spawn_customer"))
customer_spawn_timer.start()
_update_time()
clock_timer.connect("timeout", Callable(self, "_on_clock_changed"))
clock_timer.start()
game_state = GameState.GAME_PLAYING
win_menu_button.connect("pressed",Callable(self,"_go_to_scene").bind("res://main_menu.tscn"))
solo_menu_button.connect("pressed",Callable(self,"_go_to_scene").bind("res://main_menu.tscn"))
win_next_level_button.connect("pressed",Callable(self,"_next_level"))
spawn_area.connect("body_exited",Callable(self,"_on_box_clear_conveyer"))
fade_screen.modulate = Color.BLACK
var tween: Tween = get_tree().create_tween()
tween.tween_property(fade_screen,"modulate",Color(0,0,0,0),1.0)
func generate_boxes(amount: int):
for n in amount:
var box: BoxData = BoxData.new(
Global.box_colors.keys().pick_random(),
Global.box_sprites.keys().pick_random(),
Global.get_serial_number(8)
)
boxes_to_spawn.append(box)
func make_manifest():
for box in boxes_to_spawn:
var entry: ManifestEntry = manifest_entry.instantiate()
entry.set_box(box)
manifest_container.add_child(entry)
pass
func spawn_box():
if(!can_spawn_box):
return
can_spawn_box = false
var box_to_spawn = boxes_to_spawn[0]
boxes_to_spawn.erase(box_to_spawn)
var box = box_scene.instantiate()
box.position = spawn_area.position
add_child(box)
box.data = box_to_spawn
if(boxes_to_spawn.size() == 0):
box_spawn_timer.stop()
func get_clear_drop_area():
if(customers_waiting.size() >= standing_areas.size()):
return null
return standing_areas[customers_waiting.size()]
func spawn_customer():
if(Global.available_boxes.size() == 0):
return
var drop_area = get_clear_drop_area()
if(!drop_area):
return
var customer = customer_scene.instantiate()
customers_waiting.append(customer)
customer.connect("on_customer_got_correct_package", Callable(self, "_on_customer_got_correct_package").bind(customer))
customer.connect("on_customer_got_incorrect_package", Callable(self, "_on_customer_got_incorrect_package").bind(customer))
customer.connect("on_customer_leave", Callable(self,"_on_customer_leave"))
add_child(customer)
customer.position = customer_spawn_area.position
customer.move_to(drop_area.position)
customers_around += 1
func _on_customer_got_correct_package(customer: Customer):
move_customers_up(customer)
func _on_customer_got_incorrect_package(customer: Customer):
move_customers_up(customer)
write_ups += 1
if(write_ups > 3):
_game_over("You were fired for being written up too many times.")
func move_customers_up(customer_to_remove: Customer):
print("moving")
customers_waiting.erase(customer_to_remove)
for other_customer in customers_waiting:
var should_be_at = standing_areas[customers_waiting.find(other_customer)]
if(other_customer.position != should_be_at.position):
other_customer.move_to(should_be_at.position)
func _on_customer_leave():
customers_around -= 1
if(Global.available_boxes.size() == 0 && customers_around == 0):
_win()
func set_game_state(value: GameState):
game_state = value
emit_signal("on_game_state_changed", game_state)
func _on_clock_changed():
time_left -= 1
_update_time()
func _update_time():
var seconds = fmod(time_left, 60.0)
var minutes = int(time_left / 60.0) % 60
var formatted_string: String = "%01d:%02d" % [minutes, seconds]
time_text.text = formatted_string + " until overtime"
if(time_left <= 0):
_game_over("You were fired for getting 23 seconds of overtime.")
func _game_over(reason: String):
clock_timer.stop()
game_state = GameState.GAME_OVER
solo_menu_button.show()
black_screen.show()
black_screen.modulate = Color(0,0,0,0)
continue_text.hide()
lose_text.show()
lose_text.clear()
lose_text.append_text("[center]")
lose_text.push_color(Color("#FF0000"))
lose_text.add_text(reason)
lose_text.pop()
lose_text.pop()
var fade_tween: Tween = get_tree().create_tween()
fade_tween.tween_property(black_screen,"modulate",Color.WHITE,1.0)
_show_review()
func _win():
clock_timer.stop()
if(Global.level >= Global.levels.size()):
game_state = GameState.GAME_WIN
solo_menu_button.show()
continue_text.clear()
continue_text.append_text("[center]")
continue_text.push_color(Color("#FFEDFA"))
continue_text.add_text("You've earned a pizza party, good work! Thank you for playing.")
continue_text.pop()
continue_text.pop()
else:
game_state = GameState.GAME_CONTINUE
win_menu_button.show()
win_next_level_button.show()
black_screen.show()
black_screen.modulate = Color(0,0,0,0)
var fade_tween: Tween = get_tree().create_tween()
fade_tween.tween_property(black_screen,"modulate",Color.WHITE,1.0)
_show_review()
func _go_to_scene(scene):
Global.cleanup()
if(fade_tween):
return
fade_tween = get_tree().create_tween()
fade_tween.tween_property(fade_screen,"modulate",Color.BLACK,1.0)
var music_tween: Tween = get_tree().create_tween()
music_tween.tween_property(music,"volume_db", -80, 1.0)
await fade_tween.finished
get_tree().change_scene_to_file(scene)
func _next_level():
Global.level = min(Global.level + 1, Global.levels.size())
_go_to_scene("res://main_game.tscn")
func _on_box_clear_conveyer(_body: Node2D):
print("passed")
can_spawn_box = true
func _show_review():
#make review
review.show()
var random_customer: CustomerData = Global.customer_list.pick_random()
var stars_text: String = ""
var review_text: String = ""
if(game_state == GameState.GAME_WIN):
stars_text = "★★★★★"
review_text = random_customer.five_star
elif(game_state == GameState.GAME_CONTINUE):
stars_text = "★★★☆☆"
review_text = random_customer.three_star
else:
stars_text = "★☆☆☆☆"
review_text = random_customer.one_star
profile_picture.texture = random_customer.portait
stars_label.text = stars_text
review_label.text = review_text
username_label.text = random_customer.username