42 lines
898 B
GDScript
42 lines
898 B
GDScript
extends Node
|
|
|
|
signal money_changed(new_money)
|
|
|
|
var _save_dir := "user://saves"
|
|
|
|
var data := {
|
|
"money": 1000,
|
|
"day": 1
|
|
}
|
|
|
|
func list_saves():
|
|
var dir := DirAccess.open("user://saves")
|
|
var saves = dir.get_files()
|
|
var res: Array[Save] = []
|
|
for save in saves:
|
|
res.append(Save.new())
|
|
func add_money(amount):
|
|
data.money += amount
|
|
emit_signal("money_changed", data.money)
|
|
|
|
var save_path = "user://save.bin"
|
|
|
|
func save_save():
|
|
var file = FileAccess.open(save_path, FileAccess.WRITE)
|
|
file.store_buffer(JSONPacker.encode(data))
|
|
|
|
func load_save(path: String):
|
|
if FileAccess.file_exists(path):
|
|
var file = FileAccess.open(path, FileAccess.READ)
|
|
data = JSONPacker.decode(file.get_buffer(file.get_length()))
|
|
return true
|
|
return false
|
|
|
|
func _init() -> void:
|
|
load_state()
|
|
|
|
func _notification(what):
|
|
if what == NOTIFICATION_WM_CLOSE_REQUEST:
|
|
save_state()
|
|
get_tree().quit() # default behavior
|