Save System Mk.III (and start of new difficulty system)
This commit is contained in:
parent
746b9813e2
commit
4958c420d8
8 changed files with 203 additions and 82 deletions
|
@ -1,44 +1,127 @@
|
|||
extends Node
|
||||
|
||||
var file
|
||||
## contains the save data for a single level
|
||||
class LevelSaveData:
|
||||
var save_id: String
|
||||
|
||||
var score_any: int = 0
|
||||
var score_100: int = 0
|
||||
|
||||
var time_any: float = INF
|
||||
var time_100: float = INF
|
||||
|
||||
var keys_collected: int = 0
|
||||
var shards_collected: Array
|
||||
|
||||
func _init(id: String) -> void:
|
||||
save_id = id
|
||||
shards_collected = []
|
||||
shards_collected.resize(8)
|
||||
for i in 8:
|
||||
shards_collected[i] = false
|
||||
|
||||
## save the level data to the given ConfigFile
|
||||
func save_to_file(file: ConfigFile) -> void:
|
||||
# set scores
|
||||
file.set_value(save_id, "score_any", score_any)
|
||||
file.set_value(save_id, "score_100", score_100)
|
||||
# set times
|
||||
file.set_value(save_id, "time_any", time_any)
|
||||
file.set_value(save_id, "time_100", time_100)
|
||||
# set collected keys
|
||||
file.set_value(save_id, "keys", keys_collected)
|
||||
# set collected shards
|
||||
for i in 8:
|
||||
file.set_value(save_id, "shard_%d" % i, shards_collected[i])
|
||||
|
||||
## loads level data from the given ConfigFile
|
||||
func load_from_file(file: ConfigFile) -> void:
|
||||
# get scores
|
||||
score_any = file.get_value(save_id, "score_any", 0)
|
||||
score_100 = file.get_value(save_id, "score_100", 0)
|
||||
# get times
|
||||
time_any = file.get_value(save_id, "time_any", INF)
|
||||
time_100 = file.get_value(save_id, "time_100", INF)
|
||||
# get collected keys
|
||||
keys_collected = file.get_value(save_id, "keys", keys_collected)
|
||||
# get collected shards
|
||||
for i in 8:
|
||||
shards_collected[i] = file.get_value(save_id, "shard_%d" % i, false)
|
||||
|
||||
func load_file(file_number):
|
||||
var new_file = ConfigFile.new()
|
||||
new_file.load("user://file%d.pr" % file_number)
|
||||
file = new_file
|
||||
class SaveFile:
|
||||
# path of file to load/save from
|
||||
var file_path: String
|
||||
# name of the save file
|
||||
var name: String = ""
|
||||
# difficulty chosen for the file
|
||||
var difficulty: int = Game.Difficulty.SPICY
|
||||
# total play time
|
||||
var play_time: float = 0.0
|
||||
# dictionary of levels' save data
|
||||
var levels: Dictionary
|
||||
|
||||
func _init(path: String) -> void:
|
||||
file_path = path
|
||||
levels = {}
|
||||
for level in LevelData.levels:
|
||||
levels[level.save_id] = LevelSaveData.new(level.save_id)
|
||||
|
||||
# return total number of shards
|
||||
func get_total_shards() -> int:
|
||||
var total = 0
|
||||
for level in levels.values():
|
||||
total += level.shards_collected.count(true)
|
||||
return total
|
||||
|
||||
# return total number of keys
|
||||
func get_total_keys() -> int:
|
||||
var total = 0
|
||||
for level in levels.values():
|
||||
total += level.keys_collected
|
||||
return total
|
||||
|
||||
func load_from_file() -> void:
|
||||
var file = ConfigFile.new()
|
||||
file.load(file_path)
|
||||
# load name
|
||||
name = file.get_value("options", "name", "SG")
|
||||
# load difficulty
|
||||
difficulty = file.get_value("options", "difficulty", Game.Difficulty.SPICY)
|
||||
# load playtime
|
||||
play_time = file.get_value("options", "play_time", 0.0)
|
||||
# TODO: load bought palettes
|
||||
# TODO: load selected palette
|
||||
# load level save data
|
||||
for level_save in levels.values():
|
||||
level_save.load_from_file(file)
|
||||
|
||||
func save_to_file() -> void:
|
||||
var file = ConfigFile.new()
|
||||
# save name
|
||||
file.set_value("options", "name", name)
|
||||
# save difficulty
|
||||
file.set_value("options", "difficulty", difficulty)
|
||||
# save playtime
|
||||
file.set_value("options", "play_time", play_time)
|
||||
# TODO: save bought palettes
|
||||
# TODO: save selected palette
|
||||
# save level data
|
||||
for level in levels.values():
|
||||
level.save_to_file(file)
|
||||
file.save(file_path)
|
||||
|
||||
func get_level_data(save_id):
|
||||
var shards = []
|
||||
shards.resize(8)
|
||||
for i in 8:
|
||||
shards[i] = file.get_value(save_id, "shard_%d" % i, false)
|
||||
return {
|
||||
# any% score and time
|
||||
score_any = file.get_value(save_id, "score_any", 0),
|
||||
time_any = file.get_value(save_id, "time_any", INF),
|
||||
# 100% score and time
|
||||
score_100 = file.get_value(save_id, "score_100", 0),
|
||||
time_100 = file.get_value(save_id, "time_100", INF),
|
||||
shards = shards
|
||||
}
|
||||
var current_file: SaveFile = null
|
||||
|
||||
func load_options():
|
||||
Game.use_lives = file.get_value("options","uselives",Game.use_lives)
|
||||
Game.is_easy_mode = file.get_value("game","easy",Game.is_easy_mode)
|
||||
func _ready() -> void:
|
||||
# TODO: make load last played file
|
||||
current_file = load_file("user://file1.pr")
|
||||
Game.difficulty = current_file.difficulty
|
||||
|
||||
func set_shard_collected(save_id, index, collected = true):
|
||||
file.set_value(save_id, "shard_%d" % index, collected)
|
||||
func _on_letter_chosen(glyph: String):
|
||||
if current_file != null:
|
||||
current_file.name += glyph
|
||||
|
||||
func set_score(save_id, score, is_100 = false):
|
||||
file.set_value(save_id, "score_100" if is_100 else "score_any", int(score))
|
||||
|
||||
func set_options():
|
||||
file.set_value("options","uselives",Game.use_lives)
|
||||
file.set_value("game","easy",Game.is_easy_mode)
|
||||
save_file(Game.current_file)
|
||||
|
||||
func set_time(save_id, time, is_100 = false):
|
||||
file.set_value(save_id, "time_100" if is_100 else "time_any", float(time))
|
||||
|
||||
func save_file(file_number):
|
||||
file.save("user://file%d.pr" % file_number)
|
||||
func load_file(path: String) -> SaveFile:
|
||||
var file = SaveFile.new(path)
|
||||
file.load_from_file()
|
||||
return file
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue