diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 8c5f876..825c5bf 100644 --- a/Items/Inventory.cs +++ b/Items/Inventory.cs @@ -11,10 +11,11 @@ public partial class Inventory : Node2D, IItemCollection [Export] public Array Hotbar { get; private set; } - [Export] - public Array Items { get; private set; } + public Array HotbarToItemIndexMap { get; set; } = new(); [Export] + public Array Items { get; set; } + public Dictionary InventoryMap { get; set; } [Signal] @@ -62,6 +63,12 @@ public partial class Inventory : Node2D, IItemCollection public bool IsUsingItem => SelectedItem?.IsUsing ?? false; + public Inventory() + { + HotbarToItemIndexMap.Resize(HotbarCapacity); + HotbarToItemIndexMap.Fill(-1); + } + public override void _Ready() { if (Hotbar is null) @@ -155,7 +162,19 @@ public partial class Inventory : Node2D, IItemCollection return item; } - public Item SetHotbarIndexToItem(int index, ItemMetadata metadata) + public Item SetHotbarIndexToItemIndex(int hotbarIndex, int itemIndex) + { + HotbarToItemIndexMap[hotbarIndex] = itemIndex; + + if (itemIndex >= 0) + { + return SetHotbarIndexToItem(hotbarIndex, Items[itemIndex]); + } + + return null; + } + + private Item SetHotbarIndexToItem(int index, ItemMetadata metadata) { var oldItem = Hotbar[index]; Item newItem = null; diff --git a/State/Global/GlobalState.cs b/State/Global/GlobalState.cs index 8c56b17..734b861 100644 --- a/State/Global/GlobalState.cs +++ b/State/Global/GlobalState.cs @@ -76,6 +76,19 @@ public partial class GlobalState : Node Progression = save.Progression; MapState = save.MapState; Stats = save.Stats; + + var inventory = World.Instance.CurrentPlayer.Inventory; + inventory.Items = Stats.Items; + + for (int i = 0; i < Stats.HotbarToItemIndexMap.Count; i++) + { + int itemIndex = Stats.HotbarToItemIndexMap[i]; + + if (itemIndex >= 0) + { + inventory.SetHotbarIndexToItemIndex(i, itemIndex); + } + } } public void ExportToSave(Save save) @@ -83,5 +96,9 @@ public partial class GlobalState : Node save.Progression = Progression; save.MapState = MapState; save.Stats = Stats; + + var inventory = World.Instance.CurrentPlayer.Inventory; + Stats.Items = inventory.Items; + Stats.HotbarToItemIndexMap = inventory.HotbarToItemIndexMap; } } diff --git a/State/Global/Stats.cs b/State/Global/Stats.cs index a78c222..993ef13 100644 --- a/State/Global/Stats.cs +++ b/State/Global/Stats.cs @@ -1,4 +1,5 @@ using Godot; +using Godot.Collections; namespace SupaLidlGame.State.Global; @@ -13,4 +14,10 @@ public partial class Stats : Resource [Export] public int DeathCount { get; set; } = 0; + + [Export] + public Array Items { get; set; } = new(); + + [Export] + public Array HotbarToItemIndexMap { get; set; } = new(); } diff --git a/UI/Inventory/InventoryGrid.cs b/UI/Inventory/InventoryGrid.cs index fc33d54..7589919 100644 --- a/UI/Inventory/InventoryGrid.cs +++ b/UI/Inventory/InventoryGrid.cs @@ -72,6 +72,7 @@ public partial class InventoryGrid : GridContainer ItemMetadata item = items.Current; slot.Item = item; + slot.Index = i; } // make remaining slots display empty @@ -80,6 +81,7 @@ public partial class InventoryGrid : GridContainer InventorySlot slot = children[i] as InventorySlot; slot.Item = null; + slot.Index = i; } } diff --git a/UI/Inventory/InventoryMenu.cs b/UI/Inventory/InventoryMenu.cs index 1a27612..304f247 100644 --- a/UI/Inventory/InventoryMenu.cs +++ b/UI/Inventory/InventoryMenu.cs @@ -76,6 +76,6 @@ public partial class InventoryMenu : BaseMenu, IModal { int slot = button.GetMeta("slot").AsInt32(); GD.Print("Equipping item at slot " + slot); - Source.SetHotbarIndexToItem(slot, _selected.Item); + Source.SetHotbarIndexToItemIndex(slot, _selected.Index); } } diff --git a/UI/Inventory/InventorySlot.cs b/UI/Inventory/InventorySlot.cs index 8d9bda6..e29e13a 100644 --- a/UI/Inventory/InventorySlot.cs +++ b/UI/Inventory/InventorySlot.cs @@ -33,6 +33,8 @@ public partial class InventorySlot : Button private static Texture2D _placeholderTexture; + public int Index { get; set; } + private Items.ItemMetadata _item; public Items.ItemMetadata Item diff --git a/Utils/ScenePath.cs b/Utils/ScenePath.cs index 5e2a236..7cc938c 100644 --- a/Utils/ScenePath.cs +++ b/Utils/ScenePath.cs @@ -21,7 +21,12 @@ public partial class ScenePath : ResourcePath // add scene to loaded to not have to reload scene when called again var scene = base.Load(); - _loaded.Add(Path, scene); + + if (useCached) + { + _loaded.Add(Path, scene); + } + return scene; }