2024-05-29 14:55:23 -07:00
|
|
|
using Godot;
|
2024-05-30 14:16:22 -07:00
|
|
|
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;
|
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
[Export]
|
|
|
|
private PackedScene _slotScene;
|
|
|
|
|
2024-05-29 14:55:23 -07:00
|
|
|
public SupaLidlGame.Items.Inventory InventorySource
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_inventorySource = value;
|
2024-05-30 14:16:22 -07:00
|
|
|
Redraw();
|
2024-05-29 14:55:23 -07:00
|
|
|
}
|
|
|
|
get => _inventorySource;
|
|
|
|
}
|
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
public void Redraw()
|
2024-05-29 14:55:23 -07:00
|
|
|
{
|
2024-05-30 14:16:22 -07:00
|
|
|
if (_inventorySource is null)
|
|
|
|
{
|
|
|
|
this.QueueFreeChildren();
|
|
|
|
}
|
2024-05-29 14:55:23 -07:00
|
|
|
|
2024-05-30 14:16:22 -07:00
|
|
|
var children = GetChildren();
|
2024-05-29 14:55:23 -07:00
|
|
|
|
2024-05-30 14:16:22 -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
|
|
|
|
2024-05-30 14:16:22 -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
|
|
|
}
|
|
|
|
}
|