implement loading and saving raw data to save file resource

This commit is contained in:
betalars 2024-10-03 01:10:35 +02:00
parent 2972ee0ad6
commit efcc3bf694
2 changed files with 64 additions and 3 deletions

View File

@ -1,8 +1,69 @@
@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()]
@export var current_room: State.rooms = 0
@export var mementos_complete: int = 0
@export var board_state: PackedScene
@export var thumbnail: Texture2D = preload("res://import/interface-elements/empty_save_slot.png")
@export var board_state: Dictionary
@export var thumbnail: Image = Image.load_from_file("res://import/interface-elements/empty_save_slot.png")
@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()))

View File

@ -6,7 +6,7 @@ func _init(save: SaveGame, id: int) -> void:
add_child(base_container)
var texture:= TextureRect.new()
texture.texture = save.thumbnail
texture.texture = ImageTexture.create_from_image(save.thumbnail)
texture.size_flags_vertical = Control.SIZE_SHRINK_CENTER
base_container.add_child(texture)