-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersistent.gd
317 lines (242 loc) · 6.88 KB
/
persistent.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
extends Node
onready var player = get_node("/root/base/player")
onready var base = get_node("/root/base")
var firstload = true
var endgames = []
var timer:float = 0 # timer in seconds
var savenum = 0 setget update_savepath
var savepath = "user://saves%d.save" % savenum
var thumbnailpath = "user://thumbnail.save"
var thumbnails = {0: {}, 1: {}, 2: {}} # how do i actually delete the save. a
var damagesource = ""
var id_keep = {
"firekey": false,
"airkey": false,
"waterkey": false,
"earthkey": false,
'outoffirstroom': false
} setget update_keeps
var carrying = [] setget sort_inv
signal endgame
var coords = Vector2(0, 0)
var places = {
Vector2(0, 0) :[],
Vector2(1, 0): ['cookie'],
Vector2(1, 1): ['wooden stick'],
Vector2(0, 1): ['cookie'],
Vector2(0, -1): ['wooden stick'],
Vector2(-1, -1): ['wooden stick'],
Vector2(-1, 0): ['healing drop'],
Vector2(-1, 1):['cookie'],
Vector2(1, -1):['healing drop']}
var beenplaces = [Vector2()]
var genbosses = {
Vector2(10, -10): {'name': 'poinkydirtie', 'alive': true},
Vector2(10, 10): {'name': 'swooshymooshy', 'alive': true},
Vector2(-10, 10): {'name': 'foofeefoofee', 'alive': true},
Vector2(-10, -10): {'name': 'puffpuffiepuff', 'alive': true}}
var seenbosses = []
var health = 5 setget update_health
var energy = 5 setget update_energy
var damage = 0
var max_health = 5 setget update_maxhealth
var defeated = false
var coward = 0
var isCoward = false
var weapon = "" setget set_weapon
# WHEN ADDING A NEW VARIABLE:
# 1. create variable
# 2. allow it to be reset. if needed
# 3. allow it to be saved
# 4. check that it can be loaded
func create_save(num):
update_savepath(num)
reset()
func delete_save(num):
"""
1. clear thumbnail
2. delete file
3. ?? profit.
"""
thumbnails[num] = {}
var dir = Directory.new()
dir.remove("user://saves%d.save" % num)
save_thumbnail(false)
func update_savepath(new):
savenum = new
savepath = "user://saves%d.save" % savenum
func reset():
endgames = []
id_keep = {
"firekey": false,
"airkey": false,
"waterkey": false,
"earthkey": false,
'outoffirstroom': false
}
carrying = []
coords = Vector2(0, 0)
places = {
Vector2(0, 0) :[],
Vector2(1, 0): ['cookie'],
Vector2(1, 1): ['wooden stick'],
Vector2(0, 1): ['cookie'],
Vector2(0, -1): ['wooden stick'],
Vector2(-1, -1): ['wooden stick'],
Vector2(-1, 0): ['healing drop'],
Vector2(-1, 1):['cookie'],
Vector2(1, -1):['healing drop']}
beenplaces = [Vector2()]
genbosses = {
Vector2(10, -10): {'name': 'poinkydirtie', 'alive': true},
Vector2(10, 10): {'name': 'swooshymooshy', 'alive': true},
Vector2(-10, 10): {'name': 'foofeefoofee', 'alive': true},
Vector2(-10, -10): {'name': 'puffpuffiepuff', 'alive': true}}
seenbosses = []
health = 5
energy = 5
damage = 0
max_health = 5
defeated = false
weapon = ""
damagesource = ""
timer = 0
save_game()
func update_health(newval):
var oldhealth = health
health = newval
base.bars.update_bars()
if health <= 0:
base.die(damagesource)
base.low_health.return_to_normal()
elif health <= min(0.2 * max_health, 5):
base.low_health.low_health()
else:
base.low_health.return_to_normal()
func update_energy(newval):
energy = newval
base.bars.update_bars()
func update_maxhealth(newval):
max_health = newval
base.bars.update_bars()
if health <= min(0.2 * max_health, 5):
base.low_health.low_health()
else:
base.low_health.return_to_normal()
func set_weapon(newval):
weapon = newval
player.update_weapon()
base.update_weapon()
func items_custcomp(a, b):
return Data.itemsorder.find( a ) < Data.itemsorder.find( b )
func sort_inv(newval):
if !(weapon in newval) && weapon != "":
set_weapon("")
newval.sort_custom(self, "items_custcomp")
carrying = newval
func _ready():
sort_inv(carrying)
load_thumbnails()
func update_keeps(new_keep):
id_keep = new_keep
if id_keep["firekey"] && id_keep["airkey"] && id_keep["waterkey"] && id_keep["earthkey"] && !defeated:
defeated = true
emit_signal("endgame")
func load_thumbnails():
var save = File.new()
if not save.file_exists(thumbnailpath):
yield(get_tree(), "idle_frame")
return {0: {}, 1: {}, 2: {}} # Error! We don't have a save to load.
# Load the file line by line and process that dictionary to restore
# the object it represents.
save.open(thumbnailpath, File.READ)
# Get the saved dictionary from the next line in the save file
var vals = parse_json(save.get_line())
for i in vals.keys():
"""
Store the keys as variables
- 0weapon
- 0health
- 0energy
- 0time (seconds played)
"""
thumbnails[int(i.left(1))][i.right(1)] = vals[i]
return thumbnails
func save_thumbnail(update_curgame = true):
var save = File.new()
save.open(thumbnailpath, File.WRITE)
if update_curgame:
thumbnails[savenum]["weapon"] = weapon
thumbnails[savenum]["health"] = health
thumbnails[savenum]["max_health"] = max_health
thumbnails[savenum]["energy"] = energy
thumbnails[savenum]["timer"] = timer
thumbnails[savenum]["percentage"] = Functions.get_room_percent()
var vals = {}
for i in thumbnails.keys():
for j in thumbnails[i].keys():
vals[str(i) + j] = thumbnails[i][j]
save.store_line(to_json(vals))
save.close()
func save_game():
save_thumbnail()
#prepares the file
var saves = File.new()
saves.open(savepath, File.WRITE)
#vars to save
var vals = {
"endgames": endgames,
"coords": coords,
"places": places,
"beenplaces": beenplaces,
"genbosses": genbosses,
"seenbosses": seenbosses,
"carrying": carrying,
"health": health,
"energy": energy,
"damage": damage,
"max_health": max_health,
"defeated": defeated,
"coward": coward,
"isCoward": isCoward,
"weapon": weapon,
'id_keep': id_keep,
'firstload': firstload,
'timer': timer
}
saves.store_line(to_json(vals))
print("game saved!")
saves.close()
#emit_signal("finish_save")
func load_game():
var veckeys = ["genbosses", "places"]
var vecarr = ["beenplaces"]
var vecsingle = ["coords"]
var save_game = File.new()
if not save_game.file_exists(savepath):
yield(get_tree(), "idle_frame")
return # Error! We don't have a save to load.
# Load the file line by line and process that dictionary to restore
# the object it represents.
save_game.open(savepath, File.READ)
# Get the saved dictionary from the next line in the save file
var vals = parse_json(save_game.get_line())
for i in vals.keys():
if i in veckeys:
set(i, {})
for key in vals[i].keys():
get(i)[str2var("Vector2" + key)] = vals[i][key]
elif i in vecarr:
set(i, [])
for item in vals[i]:
get(i).append(str2var("Vector2" + item))
elif i in vecsingle:
set(i, str2var("Vector2" + vals[i]))
else:
set(i, vals[i])
save_game.close()
print("loaded!")
#emit_signal("finish_load")
sort_inv(carrying)
yield(get_tree(), "idle_frame")
# OH WAHT I ALREADY DID SAVING AND LOADING. OK I GUESS