State persistance between scenes

This commit is contained in:
Marco 2025-03-24 16:56:35 +01:00
commit 35254935e4
13 changed files with 142 additions and 27 deletions

View file

@ -8,8 +8,9 @@ using GTweensGodot.Extensions;
public partial class GlobalState : Node
{
public static GlobalState Instance { get; private set; }
public static SessionSettings Session => GlobalState.Instance.SessionSettings;
public Node CurrentScene { get; set; }
public string CurrentSceneId { get; private set; }
private ColorRect _fader { get; set; }
@ -71,12 +72,18 @@ public partial class GlobalState : Node
private void DeferredGotoScene(string path, MapStartDataResource startData = null)
{
// if (InventoryManager.Instance is not null)
// {
// SessionSettings.Items = InventoryManager.Instance.Save();
// }
// It is now safe to remove the current scene.
CurrentScene.Free();
// Load a new scene.
var nextScene = GD.Load<PackedScene>(path);
CurrentSceneId = nextScene.GetSceneUniqueId();
// Instance the new scene.
CurrentScene = nextScene.Instantiate();
@ -153,4 +160,25 @@ public partial class GlobalState : Node
_loadingPlaque?.Hide();
_fader.TweenModulateAlpha(0, 1f).PlayUnpausable();
}
public void SaveGame()
{
var items = InventoryManager.Instance.Save();
var serializedSavedata = new Godot.Collections.Dictionary<string, Variant>()
{
{ "Items", items },
{ "Level", CurrentSceneId }
};
var saveFile = FileAccess.Open("user://savegame.save", FileAccess.ModeFlags.Write);
var jsonString = Json.Stringify(serializedSavedata);
saveFile.StoreLine(jsonString);
}
public void LoadGame()
{
}
}