Cleaned up project with better structure.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using Godot;
|
||||
|
||||
public partial class InventoryDisplay : PanelContainer
|
||||
{
|
||||
private 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}";
|
||||
}
|
||||
|
||||
public void ReloadItems()
|
||||
{
|
||||
foreach (Node node in itemList.GetChildren())
|
||||
{
|
||||
itemList.RemoveChild(node);
|
||||
node.QueueFree();
|
||||
}
|
||||
|
||||
ItemDisplay display;
|
||||
|
||||
foreach (Item item in GameData.inventory.items)
|
||||
{
|
||||
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);
|
||||
itemList.AddChild(display);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnInventoryUpdate(object sender, EventArgs args)
|
||||
{
|
||||
ReloadItems();
|
||||
UpdateInventorySpace();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://com0u7nqag6pp
|
||||
@@ -0,0 +1,9 @@
|
||||
using Godot;
|
||||
|
||||
public partial class ItemDisplay : PanelContainer
|
||||
{
|
||||
[Export] public TextureRect texture;
|
||||
[Export] public RichTextLabel text;
|
||||
[Export] public RichTextLabel amount;
|
||||
public Item item;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://qdjn5oqn6p5d
|
||||
Reference in New Issue
Block a user