WIP: chaning save slot from 3 fixed slots to dynamic system

This commit is contained in:
betalars 2024-10-07 11:23:19 +02:00
parent 7f01336587
commit fefadff5dd
3 changed files with 48 additions and 25 deletions

View File

@ -2,12 +2,11 @@
class_name SaveGame extends Resource class_name SaveGame extends Resource
@export var filepath: String @export var filepath: String
@export var save_slot_id: int @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 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 current_room: State.rooms = 0
@export var mementos_complete: int = 0 @export var mementos_complete: int = 0
@export var board_state: Dictionary = {} @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 last_saved: Dictionary = Time.get_datetime_dict_from_system()
@export var is_save_file_valid: bool = false @export var is_save_file_valid: bool = false
@ -19,12 +18,14 @@ func _validate_property(property: Dictionary):
func _init(filepath = "") -> void: func _init(filepath = "") -> void:
if filepath == "": if filepath == "":
filepath = "usr://saves/%s" % unique_save_name filepath = "%s/%s" % [State.user_saves_path, unique_save_name]
read_save_file() read_save_file()
func read_save_file(): func read_save_file():
if FileAccess.file_exists("%s.json:" % filepath): if FileAccess.file_exists("%s.json:" % filepath):
@ -33,12 +34,12 @@ func read_save_file():
file.close() file.close()
var parsed: Dictionary = JSON.parse_string(raw_json) var parsed: Dictionary = JSON.parse_string(raw_json)
var tmp_img var tmp_img: Image
if FileAccess.file_exists("%s.png" % filepath): if FileAccess.file_exists("%s/thumbnails/%s.png" % [State.user_saves_path, filepath]):
tmp_img = Image.load_from_file("%s.png" % filepath) tmp_img = Image.load_from_file("%s/thumbnails/%s.png" % [State.user_saves_path, filepath])
is_save_file_valid = ( is_save_file_valid = (
save_slot_id is int and
unique_save_name is String and unique_save_name is String and
current_room is int and current_room is int and
mementos_complete is int and mementos_complete is int and
@ -51,13 +52,12 @@ func read_save_file():
if is_save_file_valid: if is_save_file_valid:
for key in parsed.keys(): for key in parsed.keys():
set(key, parsed[key]) set(key, parsed[key])
thumbnail = tmp_img if tmp_img != null:
thumbnail = ImageTexture.create_from_image(tmp_img)
func _get_save_dict() -> Dictionary: func _get_save_dict() -> Dictionary:
return { return {
"save_slot_id": save_slot_id,
"unique_save_name": unique_save_name, "unique_save_name": unique_save_name,
"current_room": current_room, "current_room": current_room,
"mementos_complete": mementos_complete, "mementos_complete": mementos_complete,
@ -65,14 +65,13 @@ func _get_save_dict() -> Dictionary:
"last_saved": last_saved "last_saved": last_saved
} }
func save_to_file(current_screen: Texture): 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) 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) var file = FileAccess.open("%s.json:" % filepath, FileAccess.WRITE)
file.store_string(JSON.stringify(_get_save_dict())) 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) add_child(base_container)
var texture:= TextureRect.new() var texture:= TextureRect.new()
texture.texture = ImageTexture.create_from_image(save.thumbnail) texture.texture = save.thumbnail
texture.size_flags_vertical = Control.SIZE_SHRINK_CENTER texture.size_flags_vertical = Control.SIZE_SHRINK_CENTER
base_container.add_child(texture) base_container.add_child(texture)

View File

@ -9,27 +9,41 @@ var has_stage: bool = false:
visible = stage visible = stage
save_buttons[0].grab_focus() 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] var save_buttons: Array[SaveGameDisplay]
@export var update_display: bool: @export var update_display: bool:
set(value): set(value):
display() load_games()
var scroll_container: ScrollContainer
var create_new_game: bool = false var create_new_game: bool = false
func _ready() -> void: func _ready() -> void:
display() load_games()
func display(): func load_games():
for child in save_buttons: var save_game_dir := DirAccess.open(State.user_saves_path)
child.free() 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 = [] 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()): for i in range(saves.size()):
var new_button := SaveGameDisplay.new(saves[i], i+1) var new_button := SaveGameDisplay.new(saves[i], i+1)
scroll_container.add_child(new_button)
save_buttons.append(new_button) save_buttons.append(new_button)
add_child(new_button)
new_button.pressed.connect(func(): _on_game_picked(i)) new_button.pressed.connect(func(): _on_game_picked(i))
func _on_game_picked(id: int): func _on_game_picked(id: int):
@ -44,4 +58,14 @@ func _on_game_picked(id: int):
func pick_slot(create_new_game: bool): func pick_slot(create_new_game: bool):
State.take_stage(self) 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)