cirnogodot/Scripts/UI/WeaponAmmoCounter.cs

123 lines
3.5 KiB
C#
Raw Permalink Normal View History

2025-03-05 18:55:30 +01:00
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.UI;
2025-03-06 15:03:14 +01:00
public partial class WeaponAmmoCounter : Container
2025-03-05 18:55:30 +01:00
{
public LootItem Item { get; private set; }
[Export]
public TextureRect Icon { get; private set; }
[Export]
public TextureRect AmmoIcon { get; private set; }
// The actual ammo label
[Export] public Label TotalAmmoLabel { get; private set; }
// Item count label
[Export] public Label LoadedAmmoLabel { get; private set; }
2025-03-05 18:55:30 +01:00
public void Init(LootItem item)
{
Item = item;
// If it has icon show it
// if it has count show it
// if it has ammo show it
2025-03-05 18:55:30 +01:00
// What's the point of having count and ammo without icon?
if (!item.UiType.HasFlag(UiItemType.Icon))
2025-03-05 18:55:30 +01:00
{
Icon.Hide();
}
else
{
Icon.Texture = item.InventorySprite;
2025-03-05 18:55:30 +01:00
}
if (!item.UiType.HasFlag(UiItemType.Count))
2025-03-06 21:16:04 +01:00
{
LoadedAmmoLabel.Hide();
}
if (item.UiType.HasFlag(UiItemType.Ammo))
{
InventoryManager.Instance.LoadedAmmoChanged += OnInstanceOnLoadedAmmoChanged;
if (string.IsNullOrWhiteSpace(item.WeaponData?.AmmoKey))
{
TotalAmmoLabel.Hide();
AmmoIcon.Hide();
return;
}
if (InventoryManager.Instance.TryGetItem(item.WeaponData?.AmmoKey, out var ammoItem))
{
AmmoIcon.Texture = ammoItem.Item.InventorySprite;
}
UpdateAmmoCounter();
// Register this only if there's ammo
InventoryManager.Instance.TotalAmmoChanged += OnInstanceOnTotalAmmoChanged;
2025-03-06 21:16:04 +01:00
}
else
{
AmmoIcon.Hide();
TotalAmmoLabel.Hide();
// Here sync the item count if it has no ammo but has count
if (item.UiType.HasFlag(UiItemType.Count))
{
InventoryManager.Instance.ItemAdded += ItemAmountChanged;
InventoryManager.Instance.ItemRemoved += ItemAmountRemoved;
}
}
}
private void ItemAmountChanged(LootItem item, int currentAmount)
{
if (item.ItemKey == Item.ItemKey)
{
LoadedAmmoLabel.Text = currentAmount.ToString();
2025-03-06 21:16:04 +01:00
}
}
2025-03-06 21:16:04 +01:00
private void ItemAmountRemoved(string key, int currentAmount)
{
if (key == Item.ItemKey)
{
LoadedAmmoLabel.Text = currentAmount.ToString();
}
2025-03-06 21:16:04 +01:00
}
2025-03-06 21:24:02 +01:00
private void OnInstanceOnLoadedAmmoChanged(StringName weaponKey, int count)
{
if (weaponKey != Item.WeaponData.ItemKey) return;
LoadedAmmoLabel.Text = count.ToString();
}
2025-03-06 21:16:04 +01:00
private void OnInstanceOnTotalAmmoChanged(StringName ammoKey, int count)
{
if (ammoKey != Item.WeaponData.AmmoKey) return;
TotalAmmoLabel.Text = count.ToString();
}
public void Delete()
{
2025-03-06 21:24:02 +01:00
InventoryManager.Instance.LoadedAmmoChanged -= OnInstanceOnLoadedAmmoChanged;
2025-03-06 21:16:04 +01:00
InventoryManager.Instance.TotalAmmoChanged -= OnInstanceOnTotalAmmoChanged;
InventoryManager.Instance.ItemAdded -= ItemAmountChanged;
InventoryManager.Instance.ItemRemoved -= ItemAmountRemoved;
2025-03-06 21:16:04 +01:00
QueueFree();
2025-03-05 18:55:30 +01:00
}
private void UpdateAmmoCounter()
2025-03-05 18:55:30 +01:00
{
TotalAmmoLabel.Text = InventoryManager.Instance.GetItemCount(Item.WeaponData.AmmoKey).ToString();
}
}