frame-of-mind/src/dev-util/savegame.gd

70 lines
2.1 KiB
GDScript3
Raw Normal View History

2024-10-01 23:26:54 +00:00
@tool
class_name SaveGame extends Resource
@export var filepath: String
@export var save_slot_id: int
@export var unique_save_name: String = "frame_of_mind_save_slot_%d_%s_%s" % [save_slot_id, Time.get_date_string_from_system(), Time.get_time_string_from_system()]
2024-10-01 23:26:54 +00:00
@export var current_room: State.rooms = 0
@export var mementos_complete: int = 0
@export var board_state: Dictionary
@export var thumbnail: Image = Image.load_from_file("res://import/interface-elements/empty_save_slot.png")
2024-10-01 23:26:54 +00:00
@export var last_saved: Dictionary = Time.get_datetime_dict_from_system()
@export var is_save_file_valid: bool = false
func _init(filepath = "") -> void:
if filepath == "":
filepath = "usr://saves/%s" % unique_save_name
read_save_file()
func _get_save_dict() -> Dictionary:
return {
"save_slot_id": save_slot_id,
"unique_save_name": unique_save_name,
"current_room": current_room,
"mementos_complete": mementos_complete,
"board_state": board_state,
"last_saved": last_saved
}
func read_save_file():
if FileAccess.file_exists("%s.json:" % filepath):
var file = FileAccess.open("%s.json:" % filepath, FileAccess.READ)
var raw_json = FileAccess.get_file_as_string("%s.json:" % filepath)
file.close()
var parsed: Dictionary = JSON.parse_string(raw_json)
var tmp_img
if FileAccess.file_exists("%s.png" % filepath):
tmp_img = Image.load_from_file("%s.png" % filepath)
is_save_file_valid = (
save_slot_id is int and
unique_save_name is String and
current_room is int and
mementos_complete is int and
board_state is Dictionary and
last_saved is Dictionary and
Time.get_unix_time_from_datetime_dict(last_saved) != 0 and
tmp_img is Image
)
if is_save_file_valid:
for key in parsed.keys():
set(key, parsed[key])
thumbnail = tmp_img
func save_file(current_screen: ImageTexture):
thumbnail = current_screen.get_image()
current_screen.resize(384, 261, Image.INTERPOLATE_LANCZOS)
thumbnail.save_png("%s.png" % filepath)
var file = FileAccess.open("%s.json:" % filepath, FileAccess.WRITE)
file.store_string(JSON.stringify(_get_save_dict()))