diff --git a/core/game_state/game_state.gd b/core/game_state/game_state.gd index 2e1be90..ffe6322 100644 --- a/core/game_state/game_state.gd +++ b/core/game_state/game_state.gd @@ -2,7 +2,7 @@ extends Node const SAVE_PATH: String = "user://savegame.json" -var save_data: Dictionary = {} +var save_data: SaveGameData = SaveGameData.new() var is_loaded: bool = false func _ready() -> void: @@ -10,22 +10,12 @@ func _ready() -> void: func save_game() -> void: var save_file = FileAccess.open(SAVE_PATH, FileAccess.WRITE) - var save_nodes = get_tree().get_nodes_in_group("Saveable") - var new_save_data: Dictionary = {} - - for node in save_nodes: - if not node.has_method("get_save_data"): - continue - - var node_data = node.get_save_data() - var node_path = str(node.get_path()) - new_save_data[node_path] = node_data - - var json_string = JSON.stringify(new_save_data) + var json_string = JSON.stringify(save_data.to_dictionary()) save_file.store_line(json_string) func load_game() -> void: if not FileAccess.file_exists(SAVE_PATH): + save_data = SaveGameData.new() is_loaded = true return @@ -35,7 +25,8 @@ func load_game() -> void: var json = JSON.new() var parse_result = json.parse(json_string) - if parse_result == OK: - save_data = json.data + save_data = SaveGameData.new() + if parse_result == OK and json.data is Dictionary: + save_data = SaveGameData.from_dictionary(json.data) is_loaded = true diff --git a/core/game_state/save_game_data.gd b/core/game_state/save_game_data.gd new file mode 100644 index 0000000..96f2492 --- /dev/null +++ b/core/game_state/save_game_data.gd @@ -0,0 +1,24 @@ +class_name SaveGameData +extends Resource + +var unlocked_collectibles_ids: Array[String] = [] +var saved_photos: Array[String] = [] + +func to_dictionary() -> Dictionary: + return { + "unlocked_collectibles_ids": unlocked_collectibles_ids, + "saved_photos": saved_photos, + } + +static func from_dictionary(data: Dictionary) -> SaveGameData: + var save_game_data := SaveGameData.new() + + if data.has("unlocked_collectibles_ids"): + for collectible_id in data["unlocked_collectibles_ids"]: + save_game_data.unlocked_collectibles_ids.append(str(collectible_id)) + + if data.has("saved_photos"): + for photo_path in data["saved_photos"]: + save_game_data.saved_photos.append(str(photo_path)) + + return save_game_data diff --git a/core/game_state/save_game_data.gd.uid b/core/game_state/save_game_data.gd.uid new file mode 100644 index 0000000..b994a29 --- /dev/null +++ b/core/game_state/save_game_data.gd.uid @@ -0,0 +1 @@ +uid://bxmvs842r82vl diff --git a/core/photo_mode/collectible_gallery.gd b/core/photo_mode/collectible_gallery.gd index c289cc3..cc1d16a 100644 --- a/core/photo_mode/collectible_gallery.gd +++ b/core/photo_mode/collectible_gallery.gd @@ -1,7 +1,8 @@ extends Control +@export var collectible_ui_scene: PackedScene + @onready var grid_container: GridContainer = $%CollectibleGrid -@onready var collectible_ui_scene = preload("res://core/photo_mode/collectible_ui.tscn") func _ready() -> void: CollectionManager.on_collectible_unlocked.connect(_unlock_collectible) diff --git a/core/photo_mode/collectible_gallery.tscn b/core/photo_mode/collectible_gallery.tscn index 7767815..847029d 100644 --- a/core/photo_mode/collectible_gallery.tscn +++ b/core/photo_mode/collectible_gallery.tscn @@ -1,6 +1,7 @@ [gd_scene format=3 uid="uid://bvw086glfpcba"] [ext_resource type="Script" uid="uid://dq3qtcrdnikl7" path="res://core/photo_mode/collectible_gallery.gd" id="1_67tug"] +[ext_resource type="PackedScene" uid="uid://dp7dvfauh5rpx" path="res://core/photo_mode/collectible_ui.tscn" id="2_or234"] [node name="CollectibleGallery" type="Control" unique_id=354419843] layout_mode = 3 @@ -12,6 +13,7 @@ anchor_bottom = 0.5 grow_horizontal = 2 grow_vertical = 2 script = ExtResource("1_67tug") +collectible_ui_scene = ExtResource("2_or234") [node name="PanelContainer" type="PanelContainer" parent="." unique_id=640179265] layout_mode = 1 diff --git a/core/photo_mode/collection_manager.gd b/core/photo_mode/collection_manager.gd index 3bfdbd5..db75a83 100644 --- a/core/photo_mode/collection_manager.gd +++ b/core/photo_mode/collection_manager.gd @@ -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() diff --git a/core/photo_mode/photo_gallery.gd b/core/photo_mode/photo_gallery.gd index 6914248..9ffd1e4 100644 --- a/core/photo_mode/photo_gallery.gd +++ b/core/photo_mode/photo_gallery.gd @@ -1,8 +1,8 @@ extends Control -@onready var grid_container: GridContainer = $%PhotoGrid +@export var photo_scene: PackedScene -@onready var photo_scene = preload("res://core/photo_mode/photo.tscn") +@onready var grid_container: GridContainer = $%PhotoGrid func _ready() -> void: CollectionManager.on_photo_saved.connect(_create_photo_thumbnail) diff --git a/core/photo_mode/photo_gallery.tscn b/core/photo_mode/photo_gallery.tscn index b93d767..48057df 100644 --- a/core/photo_mode/photo_gallery.tscn +++ b/core/photo_mode/photo_gallery.tscn @@ -1,6 +1,7 @@ [gd_scene format=3 uid="uid://b6r787sik5yil"] [ext_resource type="Script" uid="uid://k4iqrlkbjppl" path="res://core/photo_mode/photo_gallery.gd" id="1_bp5uf"] +[ext_resource type="PackedScene" uid="uid://cvus62qkop3qi" path="res://core/photo_mode/photo.tscn" id="2_45wok"] [node name="PhotoGallery" type="Control" unique_id=354419843] layout_mode = 3 @@ -12,6 +13,7 @@ anchor_bottom = 0.5 grow_horizontal = 2 grow_vertical = 2 script = ExtResource("1_bp5uf") +photo_scene = ExtResource("2_45wok") [node name="PanelContainer" type="PanelContainer" parent="." unique_id=640179265] layout_mode = 1