SupaLidlGame/UI/Inventory/InventoryGrid.cs

60 lines
1.3 KiB
C#
Raw Normal View History

2024-05-29 14:55:23 -07:00
using Godot;
using GodotUtilities;
2024-05-29 14:55:23 -07:00
using SupaLidlGame.Items;
namespace SupaLidlGame.UI.Inventory;
public partial class InventoryGrid : GridContainer
{
private SupaLidlGame.Items.Inventory _inventorySource;
[Export]
private PackedScene _slotScene;
2024-05-29 14:55:23 -07:00
public SupaLidlGame.Items.Inventory InventorySource
{
set
{
_inventorySource = value;
Redraw();
2024-05-29 14:55:23 -07:00
}
get => _inventorySource;
}
public void Redraw()
2024-05-29 14:55:23 -07:00
{
if (_inventorySource is null)
{
this.QueueFreeChildren();
}
2024-05-29 14:55:23 -07:00
var children = GetChildren();
2024-05-29 14:55:23 -07:00
for (int i = children.Count; i < _inventorySource.InventoryCapacity; i++)
{
AddChild(_slotScene.Instantiate<InventorySlot>());
}
for (int i = children.Count - 1; i >= _inventorySource.InventoryCapacity; i--)
{
children[i].QueueFree();
}
2024-05-29 14:55:23 -07:00
children = GetChildren();
for (int i = 0; i < children.Count; i++)
{
InventorySlot slot = children[i] as InventorySlot;
if (i >= _inventorySource.Items.Count)
{
slot.Item = null;
}
else if (slot.Item != _inventorySource.Items[i])
{
slot.Item = _inventorySource.Items[i];
}
}
2024-05-29 14:55:23 -07:00
}
}