refactoring save_game
This commit is contained in:
@@ -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
|
||||
|
||||
24
core/game_state/save_game_data.gd
Normal file
24
core/game_state/save_game_data.gd
Normal file
@@ -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
|
||||
1
core/game_state/save_game_data.gd.uid
Normal file
1
core/game_state/save_game_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bxmvs842r82vl
|
||||
Reference in New Issue
Block a user