Files

72 lines
1.5 KiB
C#

using System;
using Godot;
public partial class InventoryDisplay : PanelContainer
{
private readonly PackedScene itemDisplayPrefab = ResourceLoader.LoadItemDisplay();
[Export] VBoxContainer itemList;
[Export] RichTextLabel inventorySpace;
public override void _Ready()
{
GameData.inventory.OnInventoryUpdate += OnInventoryUpdate;
}
public override void _ExitTree()
{
GameData.inventory.OnInventoryUpdate -= OnInventoryUpdate;
}
public override void _Notification(int id)
{
if (id == NotificationVisibilityChanged)
{
if (!Visible) return;
ReloadItems();
UpdateInventorySpace();
}
}
private void UpdateInventorySpace()
{
inventorySpace.Text = $"Used space: {GameData.inventory.items.Count}/{GameData.inventory.maxInventorySize * GameData.maxRobotCount}";
}
public void ReloadItems()
{
ClearItems();
foreach (Item item in GameData.inventory.items)
{
itemList.AddChild(CreateItemDisplay(item));
}
}
private void ClearItems()
{
foreach (Node node in itemList.GetChildren())
{
itemList.RemoveChild(node);
node.QueueFree();
}
}
private ItemDisplay CreateItemDisplay(Item item)
{
ItemDisplay display = itemDisplayPrefab.Instantiate<ItemDisplay>();
display.item = item;
display.text.Text = item.data.GetReadableName();
display.amount.Text = $"{item.currentAmount}/{item.data.StackSize}";
display.texture.Texture = ResourceLoader.LoadPath(item.data.Texture);
return display;
}
public void OnInventoryUpdate(object sender, EventArgs args)
{
ReloadItems();
UpdateInventorySpace();
}
}