-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.gd
250 lines (210 loc) · 6.52 KB
/
GameState.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
extends Node
signal satisfaction_update(text)
signal asteroid_unlock(asteroid)
enum States {
TITLE,
TRANSITIONING,
PAUSED,
PLAYING
}
enum JourneyScore {
SPEEDY,
FAST,
TIMELY,
TARDY,
SLUGGISH
}
const DEFAULT_SATISFACTION = 60.0
const UNLOCK_THRESHOLD = 5
var settings = {}
var current_state = States.TITLE
var current_asteroid : Node2D
var pickups = {}
var dropoffs = {}
var asteroids = {}
var asteroid_satisfaction = {}
var journeys : Array
var unlocks : Dictionary
var unlock_order : Array
var asteroids_done : Array
const SAVE_GAME_PATH = 'user://savegame.json'
func save_game_state():
var save = {
# Don't store the actual node
current_asteroid_name = current_asteroid.name,
asteroid_satisfaction = asteroid_satisfaction,
journeys = journeys,
unlocks = unlocks,
unlock_order = unlock_order,
asteroids_done = asteroids_done,
play_time = root().play_time()
}
var save_game = File.new()
save_game.open(SAVE_GAME_PATH, File.WRITE)
save_game.store_string(to_json(save))
save_game.close()
func load_game_state():
var save_game = File.new()
save_game.open(SAVE_GAME_PATH, File.READ)
var state = parse_json(save_game.get_as_text())
save_game.close()
# Don't store the actual node
self.current_asteroid = asteroid_node(state.current_asteroid_name)
self.asteroid_satisfaction = state.asteroid_satisfaction
self.journeys = state.journeys
self.unlocks = state.unlocks
self.unlock_order = state.unlock_order
self.asteroids_done = state.asteroids_done
for asteroid in asteroids_done:
asteroid_node(asteroid + 'Asteroid').lift_barrier()
return state
func has_game_save():
return File.new().file_exists(SAVE_GAME_PATH)
func initialize(start_asteroid = 'HomeAsteroid'):
build_system_data()
self.current_asteroid = asteroid_node(start_asteroid)
if not asteroid_satisfaction.empty():
for k in asteroid_satisfaction.keys():
self.asteroid_satisfaction[k] = DEFAULT_SATISFACTION
else:
self.asteroid_satisfaction = {}
self.current_state = States.PLAYING
self.journeys = []
self.unlocks = {
Services = false,
Goods = false,
Study = false,
Home = false,
}
self.unlock_order = ['Home', 'Services', 'Study', 'Goods']
self.asteroids_done = []
# Builds up info about the system that other parts of the game use.
func build_system_data():
if not pickups.empty():
return
for kid in root().get_children():
if not(kid is Asteroid):
continue
var pickups = []
for point in kid.get_node('Pickups').get_children():
pickups.append(point)
var dropoffs = []
for point in kid.get_node('Dropoffs').get_children():
dropoffs.append(point)
self.pickups[kid.name] = pickups
self.dropoffs[kid.name] = dropoffs
self.asteroids[kid.name] = kid
self.asteroid_satisfaction[kid.name] = DEFAULT_SATISFACTION
const CONFIG_PATH = 'user://gamestate.cfg'
func load_settings():
var config = ConfigFile.new()
var res = config.load(CONFIG_PATH)
if res != OK:
# Shouldn't happen. Could happen. Meh.
settings = {
seen_intro = false,
music = true,
sfx = true,
fastest_time = 31449601
}
else:
settings.seen_intro = config.get_value('settings', 'seen_intro', false)
settings.music = config.get_value('settings', 'music', true)
settings.sfx = config.get_value('settings', 'sfx', true)
settings.fastest_time = config.get_value('settings', 'fastest_time', 31449601)
func save_setting_value(k, v):
var config = ConfigFile.new()
for sk in settings.keys():
config.set_value('settings', sk, settings[sk])
config.set_value('settings', k, v)
settings[k] = v
config.save(CONFIG_PATH)
func set_seen_intro(seen_state):
save_setting_value('seen_intro', seen_state)
func set_music(mute_state):
save_setting_value('music', mute_state)
func set_sfx(mute_state):
save_setting_value('sfx', mute_state)
func save_completion_time(new_time):
if new_time < settings.fastest_time:
save_setting_value('fastest_time', new_time)
func intro_acknowledged():
set_seen_intro(true)
func set_current_asteroid(new_asteroid):
current_asteroid = asteroid_node("%sAsteroid" % new_asteroid)
func root():
return get_node('/root/Root')
func asteroid_node(name):
return asteroids[name]
func on_new_dropoff(dropoff: DropoffPoint, _travel_time: float, journey_score: int):
var asteroid = dropoff.get_asteroid()
var prev_satisfaction = asteroid_satisfaction[asteroid.name]
var ratio : float
match journey_score:
JourneyScore.SPEEDY:
ratio = 1.075
JourneyScore.FAST:
ratio = 1.05
JourneyScore.TIMELY:
ratio = 1.025
JourneyScore.TARDY:
ratio = 1.0
JourneyScore.SLUGGISH:
ratio = 0.95
asteroid_satisfaction[asteroid.name] = clamp(prev_satisfaction * ratio, 0, 100)
var local_satisfaction = asteroid_satisfaction[asteroid.name]
# Don't emit any signals when satisfaction is full.
if prev_satisfaction != 100 and local_satisfaction != 100:
if prev_satisfaction == local_satisfaction:
emit_signal('satisfaction_update', 'No change to satisfaction levels ._.')
else:
var aname = asteroid.name.replace('Asteroid', '')
var direction = 'increased' if prev_satisfaction < local_satisfaction else 'decreased'
var local_delta = local_satisfaction - prev_satisfaction
emit_signal(
'satisfaction_update',
'%s Satisfaction [i]%s[/i] by [b]%.2f[/b] to [b]%.2f[/b]%%' % [aname, direction, local_delta, local_satisfaction]
)
journeys.push_front({score = journey_score, asteroid = asteroid.name.replace('Asteroid', '')})
handle_unlocks()
save_game_state()
func on_asteroid_change(_from, _to):
call_deferred('save_game_state')
func good_journey_count():
if unlock_order.empty():
return 0
var unlock_target = unlock_order[0]
var good_journeys = 0
for js in journeys:
if js.score <= JourneyScore.TIMELY and js.asteroid == unlock_target:
good_journeys += 1
return clamp(good_journeys, 0, UNLOCK_THRESHOLD)
func unlocking_now():
if unlock_order.empty():
return false
return good_journey_count() >= UNLOCK_THRESHOLD
func handle_unlocks():
if unlocking_now():
unlock_order.pop_front()
if not unlocks.Services:
unlocks.Services = true
asteroids_done.append('Home')
emit_signal('asteroid_unlock', 'Services')
journeys = []
elif not unlocks.Study:
unlocks.Study = true
asteroids_done.append('Services')
emit_signal('asteroid_unlock', 'Study')
journeys = []
elif not unlocks.Goods:
unlocks.Goods = true
asteroids_done.append('Study')
emit_signal('asteroid_unlock', 'Goods')
journeys = []
elif not unlocks.Home:
unlocks.Home = true
asteroids_done.append('Goods')
emit_signal('asteroid_unlock', 'Home')
journeys = []
func unlocked_asteroids():
return asteroids_done