refactoring save_game

This commit is contained in:
2026-04-16 12:30:40 +02:00
parent e02bd1819f
commit 2508ac96fa
8 changed files with 50 additions and 39 deletions

View File

@@ -9,7 +9,6 @@ var unlocked_collectibles_ids: Array[String] = []
var saved_photos: Array[String] = []
func _ready() -> void:
add_to_group("Saveable")
load_save_data()
func unlock_collectible(collectible_id: String) -> void:
@@ -17,6 +16,7 @@ func unlock_collectible(collectible_id: String) -> void:
var new_collectible = get_collectible_by_id(collectible_id)
if new_collectible:
unlocked_collectibles_ids.append(collectible_id)
sync_save_data()
on_collectible_unlocked.emit(collectible_id)
func get_collectible_by_id(id: String) -> CollectibleResource:
@@ -43,28 +43,18 @@ func save_photo_to_disk_async() -> void:
image.save_png(file_path)
saved_photos.append(file_path)
sync_save_data()
on_photo_saved.emit(file_path)
func get_save_data() -> Dictionary:
return {
"unlocked_collectibles_ids": unlocked_collectibles_ids,
"saved_photos": saved_photos
}
func sync_save_data() -> void:
GameState.save_data.unlocked_collectibles_ids = unlocked_collectibles_ids.duplicate()
GameState.save_data.saved_photos = saved_photos.duplicate()
func load_save_data() -> void:
var my_path = str(get_path())
if GameState.save_data.has(my_path):
var data = GameState.save_data[my_path]
if data.has("unlocked_collectibles_ids"):
var loaded_ids = data["unlocked_collectibles_ids"]
unlocked_collectibles_ids.clear()
for id in loaded_ids:
unlocked_collectibles_ids.append(str(id))
if data.has("saved_photos"):
var loaded_photos = data["saved_photos"]
saved_photos.clear()
for photo in loaded_photos:
saved_photos.append(str(photo))
else:
unlocked_collectibles_ids = GameState.save_data.unlocked_collectibles_ids.duplicate()
saved_photos = GameState.save_data.saved_photos.duplicate()
if unlocked_collectibles_ids.is_empty() and saved_photos.is_empty():
unlocked_collectibles_ids.append("cane")
sync_save_data()