Implement saving and loading hotbar state

wip
John Montagu, the 4th Earl of Sandvich 2024-10-05 15:32:26 -07:00
parent 48dcee72b4
commit 6af8540bac
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
7 changed files with 50 additions and 5 deletions

View File

@ -11,6 +11,8 @@ public partial class Inventory : Node2D, IItemCollection<ItemMetadata>
[Export]
public Array<Item> Hotbar { get; private set; }
public Array<int> HotbarToItemIndexMap { get; set; } = new();
[Export]
public Array<ItemMetadata> Items { get; set; }
@ -59,6 +61,12 @@ public partial class Inventory : Node2D, IItemCollection<ItemMetadata>
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<ItemMetadata>
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;

View File

@ -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;
}
}

View File

@ -17,4 +17,7 @@ public partial class Stats : Resource
[Export]
public Array<Items.ItemMetadata> Items { get; set; } = new();
[Export]
public Array<int> HotbarToItemIndexMap { get; set; } = new();
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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<PackedScene>();
if (useCached)
{
_loaded.Add(Path, scene);
}
return scene;
}