-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameState.gd
302 lines (248 loc) · 6.83 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
extends Node
# number of seconds between turns
const UPDATE_FREQ = 1.0
const SAVE_FREQ = 60.0 # limit autosave frequency so we don't wear out drives
class GameActions:
const Eat = "eat"
const Bathe = "bathe"
const Play = "game"
const Medicine = "medicine"
var timers = {
GameActions.Eat: 0,
GameActions.Bathe: 0,
GameActions.Play: 0,
GameActions.Medicine: 0,
"update": now(),
} : set = _update_timers
var save_sync = 0
var death_timer = 0
var stats = {
"hungry": 100.0,
"weight": 60.0,
"boredom": 0.0,
"dirty": 0.0,
"happy": 100.0,
"sick": 0.0,
"tired": 0.0,
"honey": 0.0,
"is_asleep": false,
"age": 0.0,
} : set = _update_stats
var lights_on = true
var unlocks = {}: set = _update_unlocks
# holds whatever random additional data some self managed systems have
var extra = {}
var TWITCH_ENABLED = false
signal stats_changed(stats)
signal timers_changed(timers)
func _ready():
if FileAccess.file_exists("user://pet.save"):
var save_game = FileAccess.open("user://pet.save", FileAccess.READ)
# save files are a single line json
var test_json_conv = JSON.new()
test_json_conv.parse(save_game.get_line())
var data = test_json_conv.get_data()
save_game.close()
self.stats = data.get("stats", {})
self.timers = data.get("timers", {})
self.unlocks = data.get("unlocks", {})
self.extra = data.get("extra", {})
await AppSkin.loaded()
await get_tree().process_frame
if get_tree().current_scene.is_in_group("scene"):
SceneManager.change_scene(get_tree().current_scene.scene_file_path)
func now() -> float:
return Time.get_unix_time_from_system()
func can_act(action, unlockable = null):
if not (action in timers):
return true
if stats.is_asleep:
return false
if unlockable and not unlocks.get(unlockable, false):
return false
var next_allowed: float = timers.get(action, 999999999.0)
return now() > next_allowed
func _update_unlocks(change):
if change.is_empty():
unlocks.clear()
else:
unlocks.merge(change, true)
func _update_stats(change):
stats = {
"hungry": clamp(change.get("hungry", stats.hungry), 0.0, 250.0),
"weight": clamp(change.get("weight", stats.weight), 0.0, 100.0),
"boredom": clamp(change.get("boredom", stats.boredom), 0.0, 100.0),
"dirty": clamp(change.get("dirty", stats.dirty), 0.0, 100.0),
"tired": clamp(change.get("tired", stats.tired), 0.0, 100.0),
"sick": clamp(change.get("sick", stats.sick), 0.0, 100.0),
"honey": clamp(change.get("honey", stats.honey), 0.0, 99999.0),
"age": max(0.0, change.get("age", stats.age)),
"is_asleep": change.get("is_asleep", stats.is_asleep),
}
emit_signal("stats_changed", stats)
func _update_timers(change):
timers = {
GameState.GameActions.Eat: change.get(GameState.GameActions.Eat, timers.eat),
GameState.GameActions.Bathe: change.get(GameState.GameActions.Bathe, timers.bathe),
GameState.GameActions.Play: change.get(GameState.GameActions.Play, timers.game),
GameState.GameActions.Medicine: change.get(GameState.GameActions.Medicine, timers.medicine),
"update": change.get("update", timers.update),
}
emit_signal("timers_changed", timers)
func is_overfed():
return stats.weight >= 80
func save_data():
if now() < save_sync:
return
var save_game = FileAccess.open("user://pet.save", FileAccess.WRITE)
save_game.store_line(
JSON.stringify({
"stats": stats,
"timers": timers,
"unlocks": unlocks,
"extra": extra,
})
)
save_game.close()
save_sync = now() + SAVE_FREQ
func execute_turn():
var change = stats.duplicate()
if TWITCH_ENABLED and stats.is_asleep:
change.hungry -= 0.05
change.dirty += 0.0
change.boredom += 0.0
change.tired += 0.0
elif TWITCH_ENABLED:
change.hungry -= 0.28
change.dirty += 0.13
change.boredom += 0.03
change.tired += 0.04
elif stats.is_asleep:
change.hungry -= 0.05
change.dirty += 0.0
change.boredom += 0.0
change.tired += 0.0
else:
change.hungry -= 0.15
change.dirty += 0.08
change.boredom += 0.1
change.tired += 0.03
# increase weight when overfed
if stats.hungry > 150.0:
change.weight += 1.0
# lose weight when starving
elif stats.hungry < 40:
change.weight -= 0.05
# return to healthy weight when moderately full
elif stats.hungry > 80 and stats.weight < 40.0:
change.weight += 0.5
elif stats.hungry > 60 and stats.weight < 25.0:
change.weight += 0.5
# cover tiredness and sickness by sleeping
if stats.is_asleep:
change.tired -= 0.4
change.sick -= 0.2
# wake up if lights are on
if lights_on:
change.is_asleep = false
else:
# get sick from not being clean
if stats.dirty > 30.0:
change.sick += 0.2
elif stats.dirty > 60.0:
change.sick += 0.4
elif stats.dirty < 20.0:
change.sick -= 0.2
if stats.sick > 50.0:
change.tired += 0.1
# go to bed when lights off and tired
if stats.tired > 45 and not lights_on:
change.is_asleep = true
if stats.sick > 30 and not lights_on:
change.is_asleep = true
var score = honey_score()
change.honey += score
change.age += UPDATE_FREQ
# punish the fox for not producing honey
if score <= 1.0 and death_timer <= 0:
death_timer = now() + 500
elif score > 1.0:
death_timer = 0
self.stats = change
self.timers = {
"update": now() + UPDATE_FREQ
}
func honey_score():
# calculate honey gain based on overall health/happiness
var happy = 10.0
# the fox likes to be stinky
# but not too stinky
if stats.dirty > 70.0:
happy -= 2.0
elif stats.dirty > 30.0:
happy += 1.0
# produce less honey when unhealthy weight
if stats.weight < 20.0:
happy -= 2.0
elif stats.weight < 40.0:
happy -= 0.5
elif stats.weight > 80.0:
happy -= 1.0
# produce significantly less when sick
if stats.sick > 75.0:
happy -= 6.0
# not happy if lights are on while tired
if stats.tired > 70 and lights_on:
happy -= 3.0
# scared of dark
elif stats.tired < 20 and not lights_on:
happy -= 0.5
# very happy when well rested
elif stats.tired < 20:
happy += 2.0
# happy when sleeping
if stats.is_asleep:
happy += 0.5
# keep fed
if stats.hungry < 30:
happy -= 3.0
elif stats.hungry > 80:
happy += 1.0
if stats.boredom > 80:
happy -= 4.0
elif stats.boredom > 40:
happy -= 2.0
return clamp(happy, 0.0, 15.0)
func is_dead():
return death_timer > 0 and now() > death_timer
func reset(reset_timers = true, reset_stats = true, hard = false):
if reset_timers:
timers = {
GameState.GameActions.Eat: 0,
GameState.GameActions.Bathe: 0,
GameState.GameActions.Play: 0,
GameState.GameActions.Medicine: 0,
"update": now() + UPDATE_FREQ,
}
death_timer = 0
if reset_stats:
stats = {
"hungry": 100.0,
"weight": 60.0,
"boredom": 0.0,
"dirty": 0.0,
"happy": 100.0,
"sick": 0.0,
"tired": 0.0,
"honey": 0.0,
"is_asleep": false,
"age": 0.0,
}
if hard:
unlocks = {}
func _process(_delta):
# autosave
save_data()
func _input(event):
if event.is_action_pressed("free_money"):
stats.honey += 10000