UI Inventory management

This commit is contained in:
Marco 2025-02-10 17:29:14 +01:00
commit c9abac3dc1
20 changed files with 262 additions and 72 deletions

View file

@ -1,5 +1,7 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Resources;
public partial class Hud : CanvasLayer
@ -18,6 +20,8 @@ public partial class Hud : CanvasLayer
[Export] private Label _healthLabel;
[Export] private Container _itemsContainer;
private Dictionary<string, HudItem> _items = new();
public override void _Ready()
{
// Assuming the HUD has a Label node named "HealthLabel"
@ -61,16 +65,92 @@ public partial class Hud : CanvasLayer
//_selector.Position = _selector.tolo
}
public void AddInventoryItem(LootItem item)
public void AddInventoryItem(LootItem item, int currentAmount)
{
TextureRect texture = new TextureRect();
texture.Texture = item.InventorySprite;
if (item.UiType == UiItemType.NoUI) return;
_itemsContainer.AddChild(texture);
if (!_items.TryGetValue(item.ItemKey, out var item1))
{
var hbox = new HBoxContainer();
_itemsContainer.AddChild(hbox);
var instance = item.HudItemScene.Instantiate<TextureRect>();
//_in.CallDeferred("add_child", instance);
hbox.AddChild(instance);
var hudItem = new HudItem()
{
Item = item,
Container = hbox,
};
if (item.UiType == UiItemType.IconText)
{
var label = new Label();
label.Text = currentAmount.ToString();
label.LabelSettings = new LabelSettings()
{
FontSize = 8
};
hbox.AddChild(label);
hudItem.Label = label;
}
_items.Add(item.ItemKey, hudItem);
}
else
{
if (item.UiType == UiItemType.IconText && item1.Label != null)
{
item1.Label.Text = currentAmount.ToString();
}
}
// TextureRect texture = new TextureRect();
// texture.Texture = item.InventorySprite;
//
// texture.ExpandMode = TextureRect.ExpandModeEnum.KeepSize;
// texture.StretchMode = TextureRect.StretchModeEnum.Keep;
//
// _itemsContainer.AddChild(texture);
}
public void RemoveInventoryItem(LootItem item)
public void RemoveInventoryItem(LootItem item, int currentAmount)
{
if (_items.TryGetValue(item.ItemKey, out var itm))
{
if (currentAmount <= 0)
{
itm.Container.QueueFree();
_items.Remove(item.ItemKey);
}
else
{
itm.Label.Text = currentAmount.ToString();
}
}
else
{
GD.Print($"Tried to remove item {item.ItemName} {item.ItemKey} but it was not found");
}
// var containerItem = _items.FirstOrDefault(x => x.Item == item);
// if (containerItem == null)
// {
// GD.Print($"Tried to remove item {item.ItemName} but it was not found");
// return;
// }
//
// containerItem.Container.QueueFree();
}
public class HudItem
{
public LootItem Item { get; set; }
public HBoxContainer Container { get; set; }
public Label Label { get; set; }
}
}