diff --git a/src/dev-util/savegame.gd b/src/dev-util/savegame.gd index ac2f6ad..cf4c5d4 100644 --- a/src/dev-util/savegame.gd +++ b/src/dev-util/savegame.gd @@ -2,12 +2,11 @@ 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 unique_save_name: String = "frame_of_mind_%s_%s" % [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: Dictionary = {} -var thumbnail: Image = Image.load_from_file("res://import/interface-elements/empty_save_slot.png") +@export var thumbnail: Texture = preload("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 @@ -19,12 +18,14 @@ func _validate_property(property: Dictionary): func _init(filepath = "") -> void: + if filepath == "": - filepath = "usr://saves/%s" % unique_save_name + filepath = "%s/%s" % [State.user_saves_path, unique_save_name] read_save_file() func read_save_file(): + if FileAccess.file_exists("%s.json:" % filepath): @@ -33,12 +34,12 @@ func read_save_file(): 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) + var tmp_img: Image + if FileAccess.file_exists("%s/thumbnails/%s.png" % [State.user_saves_path, filepath]): + tmp_img = Image.load_from_file("%s/thumbnails/%s.png" % [State.user_saves_path, 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 @@ -51,13 +52,12 @@ func read_save_file(): if is_save_file_valid: for key in parsed.keys(): set(key, parsed[key]) - - thumbnail = tmp_img - + + if tmp_img != null: + thumbnail = ImageTexture.create_from_image(tmp_img) 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, @@ -65,14 +65,13 @@ func _get_save_dict() -> Dictionary: "last_saved": last_saved } - func save_to_file(current_screen: Texture): - last_saved + last_saved = Time.get_datetime_dict_from_system() - thumbnail = current_screen.get_image() + var thumbnail_image: Image = current_screen.get_image() current_screen.resize(384, 261, Image.INTERPOLATE_LANCZOS) - thumbnail.save_png("%s.png" % filepath) + thumbnail_image.save_png("%s/thumbnails/%s.png" % [State.user_saves_path, filepath]) var file = FileAccess.open("%s.json:" % filepath, FileAccess.WRITE) file.store_string(JSON.stringify(_get_save_dict())) diff --git a/src/logic-scenes/main menu/save_game_display.gd b/src/logic-scenes/main menu/save_game_display.gd index 91f5007..85ba426 100644 --- a/src/logic-scenes/main menu/save_game_display.gd +++ b/src/logic-scenes/main menu/save_game_display.gd @@ -6,7 +6,7 @@ func _init(save: SaveGame, id: int) -> void: add_child(base_container) var texture:= TextureRect.new() - texture.texture = ImageTexture.create_from_image(save.thumbnail) + texture.texture = save.thumbnail texture.size_flags_vertical = Control.SIZE_SHRINK_CENTER base_container.add_child(texture) diff --git a/src/logic-scenes/main menu/save_game_list.gd b/src/logic-scenes/main menu/save_game_list.gd index 59d7024..8a22d60 100644 --- a/src/logic-scenes/main menu/save_game_list.gd +++ b/src/logic-scenes/main menu/save_game_list.gd @@ -9,27 +9,41 @@ var has_stage: bool = false: visible = stage save_buttons[0].grab_focus() -@export var saves: Array[SaveGame] = [SaveGame.new(), SaveGame.new(), SaveGame.new()] +@export var saves: Array[SaveGame] var save_buttons: Array[SaveGameDisplay] @export var update_display: bool: set(value): - display() + load_games() +var scroll_container: ScrollContainer var create_new_game: bool = false func _ready() -> void: - display() + load_games() -func display(): - for child in save_buttons: - child.free() +func load_games(): + var save_game_dir := DirAccess.open(State.user_saves_path) + var filepaths: PackedStringArray = save_game_dir.get_files() + for path in filepaths: + if path.ends_with(".json"): + saves.append(SaveGame.new(path)) + + saves.append(SaveGame.new()) + + #purging the current state save_buttons = [] + if scroll_container != null: + scroll_container.free() + + scroll_container.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + add_child(scroll_container) + for i in range(saves.size()): var new_button := SaveGameDisplay.new(saves[i], i+1) + scroll_container.add_child(new_button) save_buttons.append(new_button) - add_child(new_button) new_button.pressed.connect(func(): _on_game_picked(i)) func _on_game_picked(id: int): @@ -44,4 +58,14 @@ func _on_game_picked(id: int): func pick_slot(create_new_game: bool): State.take_stage(self) - create_new_game = true + self.create_new_game = create_new_game + +func get_most_recent_save() -> SaveGame: + var most_recent_time := 0.0 + var most_recent_index:int = 0 + for i in range(saves.size()): + if Time.get_unix_time_from_datetime_dict(saves[i].last_saved) > most_recent_time and not saves[i].current_room == 0: + most_recent_index = i + most_recent_time = Time.get_unix_time_from_datetime_dict(saves[i].last_saved) + + return saves[most_recent_index] if most_recent_time > 0 else SaveGame.new(0)