-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_game.gd
139 lines (114 loc) · 4.09 KB
/
main_game.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
extends Node
var state = StateMachine.new()
var MAIN_MENU = state.add("main_menu")
var CHARACTER_SELECT = state.add("character_select")
var WITHIN_RUN = state.add("within_run")
var PROGRESS = state.add("progress")
var ABANDON_RUN = state.add("abandon_run")
var main_menu_scene = preload("res://main_menu.tscn")
var character_selection_scene = preload("res://character_selection.tscn")
var game_run_scene = preload("res://game_run.tscn")
var character_ids = [
Enum.CharacterId.WARRIOR,
Enum.CharacterId.WIZARD,
]
var run_type: RunDef.RunType
var game_run: GameRun
var characters: Array[Character]
var loaded_save_state: SaveState
var save_state_within_run = false
func _ready():
state.connect_signals(self)
load_game_state()
state.change_state(MAIN_MENU)
func clear_children():
for node in get_children():
node.queue_free()
func _on_main_menu_entered():
var main_menu = main_menu_scene.instantiate()
add_child(main_menu)
main_menu.set_new_run(not save_state_within_run)
main_menu.run_type_selected.connect(select_character)
main_menu.continue_run_selected.connect(continue_run)
main_menu.abandon_run_selected.connect(abandon_run)
func _on_main_menu_exited():
clear_children()
func _on_character_select_entered():
characters.clear()
var character_selection = character_selection_scene.instantiate() as CharacterSelection
for character_id in character_ids:
var character = CharacterLoader.create(character_id)
character_selection.characters.push_back(character)
add_child(character_selection)
character_selection.character_selected.connect(start_run)
func _on_character_select_exited():
clear_children()
func _on_within_run_entered():
game_run = game_run_scene.instantiate()
add_child(game_run)
if save_state_within_run:
game_run.load_save_state(loaded_save_state.run_state)
else:
StatsManager.add_level(Enum.StatsLevel.GAME_RUN)
game_run.set_run_type(run_type)
game_run.set_starting_characters(characters)
game_run.start()
game_run.run_finished.connect(finish_run)
func _on_within_run_exited():
save_state_within_run = false
clear_children()
func _on_progress_entered():
# Load a nice view of progress when ready.
# Character unlocks are based on certain achievements
# (e.g., receive 1000 damage, obtain 500 block, etc)
# Display progress towards those, new unlocks, etc.
progress.call_deferred()
func progress():
StatsManager.remove_level(Enum.StatsLevel.GAME_RUN)
save_game_state()
state.change_state(MAIN_MENU)
func _on_progress_exited():
pass
func select_character(run_type: RunDef.RunType):
self.run_type = run_type
if run_type == RunDef.RunType.REGULAR_PARTY or run_type == RunDef.RunType.TEST_CAMP:
characters.clear()
characters.push_back(CharacterLoader.create(Enum.CharacterId.WARRIOR))
characters.push_back(CharacterLoader.create(Enum.CharacterId.WIZARD))
state.change_state(WITHIN_RUN)
return
state.change_state(CHARACTER_SELECT)
func start_run(character: Character):
characters.push_back(character)
state.change_state(WITHIN_RUN)
func finish_run():
state.change_state(PROGRESS)
func continue_run():
state.change_state(WITHIN_RUN)
func abandon_run():
state.change_state(ABANDON_RUN)
func _on_abandon_run_entered():
# Do something better here, like showing progress. Probably can be
# merged with PROGRESS state.
save_state_within_run = false
StatsManager.stats.trim_to_level(Enum.StatsLevel.GAME_RUN)
# Show progress, then.
StatsManager.stats.trim_to_level(Enum.StatsLevel.OVERALL)
save_game_state()
state.change_state.call_deferred(MAIN_MENU)
func _on_abandon_run_exited():
pass
func load_game_state():
if not FileAccess.file_exists("user://stuffie_save.tres"):
return
loaded_save_state = load("user://stuffie_save.tres") as SaveState
StatsManager.stats = loaded_save_state.stats
if loaded_save_state.main_game_state == WITHIN_RUN.name:
save_state_within_run = true
func save_game_state():
var save_state = SaveState.new()
save_state.stats = StatsManager.stats
save_state.main_game_state = state.current_state_name()
if state.is_state(WITHIN_RUN):
save_state.run_state = game_run.get_save_state()
ResourceSaver.save(save_state, "user://stuffie_save.tres")