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,6 +8,9 @@ using Cirno.Scripts.Resources;
public partial class InventoryManager : Node2D
{
public static InventoryManager Instance { get; private set; }
public ItemsDatabase ItemsDatabase { get; set; }
public bool RedKeycard { get; set; }
private Dictionary<string, ItemContainer> _itemsDict = new();
@ -35,6 +38,8 @@ public partial class InventoryManager : Node2D
public override void _Ready()
{
Instance = this;
GD.Print("Loading items");
Load(GlobalState.Session.Items);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -154,6 +159,32 @@ public partial class InventoryManager : Node2D
{
EmitSignal(SignalName.LoadedAmmoChanged, weaponDataItemKey, loadedAmmo);
}
public Godot.Collections.Dictionary<string, int> Save()
{
Godot.Collections.Dictionary<string, int> dict = new();
foreach (var item in _itemsDict)
{
dict.Add(item.Key, item.Value.Count);
}
return dict;
}
public void Load(Godot.Collections.Dictionary<string, int> items)
{
ItemsDatabase = ResourceLoader.Load<ItemsDatabase>("uid://cdi84udi6gldt");
_itemsDict.Clear();
_itemsDict = items.ToDictionary(x => x.Key, x => new ItemContainer()
{
Item = ItemsDatabase.LootItems.FirstOrDefault(y => y.ItemKey == x.Key),
Count = x.Value
});
GD.Print($"Items after loading: {_itemsDict.Count}");
}
}
public class ItemContainer