From 6af8540bac7f6349ab2af806818156240b0efb2b Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Sat, 5 Oct 2024 15:32:26 -0700 Subject: [PATCH] Implement saving and loading hotbar state --- Items/Inventory.cs | 22 +++++++++++++++++++++- State/Global/GlobalState.cs | 17 +++++++++++++++-- State/Global/Stats.cs | 3 +++ UI/Inventory/InventoryGrid.cs | 2 ++ UI/Inventory/InventoryMenu.cs | 2 +- UI/Inventory/InventorySlot.cs | 2 ++ Utils/ScenePath.cs | 7 ++++++- 7 files changed, 50 insertions(+), 5 deletions(-) diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 6bffa50..73b8a1a 100644 --- a/Items/Inventory.cs +++ b/Items/Inventory.cs @@ -11,6 +11,8 @@ public partial class Inventory : Node2D, IItemCollection [Export] public Array Hotbar { get; private set; } + public Array HotbarToItemIndexMap { get; set; } = new(); + [Export] public Array Items { get; set; } @@ -59,6 +61,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) @@ -137,7 +145,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 dd3de73..734b861 100644 --- a/State/Global/GlobalState.cs +++ b/State/Global/GlobalState.cs @@ -77,7 +77,18 @@ public partial class GlobalState : Node MapState = save.MapState; Stats = save.Stats; - World.Instance.CurrentPlayer.Inventory.Items = Stats.Items; + 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) @@ -86,6 +97,8 @@ public partial class GlobalState : Node save.MapState = MapState; save.Stats = Stats; - Stats.Items = World.Instance.CurrentPlayer.Inventory.Items; + 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 94b2ed4..993ef13 100644 --- a/State/Global/Stats.cs +++ b/State/Global/Stats.cs @@ -17,4 +17,7 @@ public partial class Stats : Resource [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; }