-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreManager.gd
76 lines (64 loc) · 1.77 KB
/
ScoreManager.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
extends Node
const Utils = preload('res://Utils.gd')
const Level = preload('res://game/Level.gd')
const SAVE_FILE = 'user://bestscore.save'
var score = 0 setget set_score
func set_score(value):
if(value > 0):
score = int(value)
else:
score = 0
func add_score(value):
if(value > 0):
score = int(score + value)
func save_score():
# Find out level number
var level = get_node(Utils.name_from_root('Level'))
assert(level)
var scores = []
# # Load existing score, if any
var result = load_score()
if result['ok']:
scores = result['scores']
else:
for i in range(Level.N_LEVELS):
scores.append(0)
# Overwrite this level's score if better than previous
if score > scores[level.level_num - 1]:
scores[level.level_num - 1] = score
# Save it
save_score_data(scores)
func save_score_data(scores):
assert(scores.size() == Level.N_LEVELS)
var savefile = File.new()
savefile.open(SAVE_FILE, File.WRITE)
var scorestr = ""
for s in scores:
scorestr += str(s) + ','
# Chop trailing comma
scorestr = scorestr.substr(0, scorestr.length() - 1)
savefile.store_string(scorestr)
savefile.close()
# Tries to load the save data. Returns {'ok': true, 'scores': [int] } on
# success, or { 'ok': false } on failure (invalid data or missing file)
func load_score():
var savefile = File.new()
if !savefile.file_exists(SAVE_FILE):
return { 'ok': false }
savefile.open(SAVE_FILE, File.READ)
var raw = savefile.get_as_text()
# Data is saved like: score1,score2,score3,score4,score5
var scorestr = raw.split(',')
if scorestr.size() != Level.N_LEVELS:
# Data is invalid
savefile.close()
return { 'ok': false }
var scores = []
for s in scorestr:
scores.append(int(s))
assert(scores.size() == Level.N_LEVELS)
savefile.close()
return {
'ok': true,
'scores': scores
}