changes UI to a global singleton

This commit is contained in:
Luca 2024-09-26 19:59:52 +02:00
parent aa4afc4c4b
commit e3aec75404
16 changed files with 371 additions and 119 deletions

View file

@ -1,21 +0,0 @@
extends Node
# SIGNALS
signal inventory_changed
# The players current monsters in the inventory are saved here
var monsters: Array
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
setup_new_inventory()
func setup_new_inventory() -> void:
var data = MonsterData.new()
data.set_data("debuggy", {}, 5)
print("After setting it in inventory: ", data.display_name)
monsters.append(data)
# debug timeout to wait for the debug UI
await get_tree().create_timer(2.0).timeout
inventory_changed.emit()

View file

@ -4,9 +4,6 @@ extends Node
const SAVE_GAME_PATH := "user://savegame.save"
const VERSION: int = 1
# List all the nodes and resouces to save
var save_data: Resource = SaveData.new()
func save() -> void:
var save_file = FileAccess.open(SAVE_GAME_PATH, FileAccess.WRITE)
@ -19,25 +16,25 @@ func save() -> void:
}
var meta_json_string = JSON.stringify(meta_data)
save_file.store_line(meta_json_string)
# Save the player node's position
var player = get_tree().get_root().find_child("Player", true, false)
var player_node_data = player.save()
var player_json = JSON.stringify(player_node_data)
save_file.store_line(player_json)
# for now, only save the savedata resource from above
if !save_data.has_method("save"):
print("Resource '%s' is missing a save() function, skipped" %save_data.resource_name)
return
# Call the node's / resource's save function
var node_data = save_data.call("save")
# Serialize the JSON string
var json_string = JSON.stringify(node_data)
# Store the save dictionary as a new line in the save file
# Save all other stuff from the SaveData
var save_data = SaveData.call("save")
var json_string = JSON.stringify(save_data)
save_file.store_line(json_string)
func load() -> void:
if not FileAccess.file_exists(SAVE_GAME_PATH):
print("No save file exists.")
return
# NEXT STEP: How to load correctly and in which order?
# first the current world, then the player and their position and then the inventory?
# Load the save file line by line and process the dictionary to restore
# the object it represents
@ -57,17 +54,17 @@ func load() -> void:
# Get the data from the JSON object
var node_data = json.get_data()
if node_data.has("meta"):
print("Meta data ignored.")
# handle the player data (name, money, etc.)
if node_data.has("player_data"):
print("save_game.gd: ", node_data)
# Firstly, we need to create the object and add it to the tree and set its position.
#var new_object = load(node_data["filename"]).instantiate()
#get_node(node_data["parent"]).add_child(new_object)
#new_object.position = Vector2(node_data["pos_x"], node_data["pos_y"])
var world_to_load = load(node_data["player_data"].world)
var instance = world_to_load.instantiate()
instance.add_child(load("res://scenes/player/player.tscn").instantiate())
var level_container = get_tree().get_root().find_child("CurrentLevel", true, false)
level_container.add_child(instance)
# Now we set the remaining variables.
#for i in node_data.keys():
# if i == "filename" or i == "parent" or i == "pos_x" or i == "pos_y":
# continue
# new_object.set(i, node_data[i])
func savegame_exists() -> bool:
return FileAccess.file_exists(SAVE_GAME_PATH)