cirnogodot/Scripts/UI/WeaponAmmoCounter.cs
2025-03-06 21:24:02 +01:00

74 lines
No EOL
2 KiB
C#

using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.UI;
public partial class WeaponAmmoCounter : Container
{
public LootItem Item { get; private set; }
[Export]
public TextureRect Icon { get; private set; }
[Export]
public TextureRect AmmoIcon { get; private set; }
[Export]
public Label TotalAmmoLabel { get; private set; }
[Export]
public Label LoadedAmmoLabel { get; private set; }
public void Init(LootItem item)
{
Item = item;
Icon.Texture = item.InventorySprite;
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;
}
else
{
AmmoIcon.Hide();
}
//AmmoIcon.Texture = InventoryManager.Instance.
UpdateCounter();
// Register this only if there's ammo
InventoryManager.Instance.TotalAmmoChanged += OnInstanceOnTotalAmmoChanged;
}
private void OnInstanceOnLoadedAmmoChanged(StringName weaponKey, int count)
{
if (weaponKey != Item.WeaponData.ItemKey) return;
LoadedAmmoLabel.Text = count.ToString();
}
private void OnInstanceOnTotalAmmoChanged(StringName ammoKey, int count)
{
if (ammoKey != Item.WeaponData.AmmoKey) return;
TotalAmmoLabel.Text = count.ToString();
}
public void Delete()
{
InventoryManager.Instance.LoadedAmmoChanged -= OnInstanceOnLoadedAmmoChanged;
InventoryManager.Instance.TotalAmmoChanged -= OnInstanceOnTotalAmmoChanged;
QueueFree();
}
private void UpdateCounter()
{
TotalAmmoLabel.Text = InventoryManager.Instance.GetItemCount(Item.WeaponData.AmmoKey).ToString();
}
}