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; }
|
|
|
|
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
|
public Label TotalAmmoLabel { get; private set; }
|
|
|
|
|
|
[Export]
|
|
|
|
|
|
public Label LoadedAmmoLabel { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public void Init(LootItem item)
|
|
|
|
|
|
{
|
|
|
|
|
|
Item = item;
|
2025-03-06 17:05:45 +01:00
|
|
|
|
|
|
|
|
|
|
Icon.Texture = item.InventorySprite;
|
2025-03-05 18:55:30 +01:00
|
|
|
|
|
2025-03-06 21:24:02 +01:00
|
|
|
|
InventoryManager.Instance.LoadedAmmoChanged += OnInstanceOnLoadedAmmoChanged;
|
2025-03-05 18:55:30 +01:00
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(item.WeaponData.AmmoKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
TotalAmmoLabel.Hide();
|
|
|
|
|
|
AmmoIcon.Hide();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-06 21:16:04 +01:00
|
|
|
|
if (InventoryManager.Instance.TryGetItem(item.WeaponData.AmmoKey, out var ammoItem))
|
|
|
|
|
|
{
|
|
|
|
|
|
AmmoIcon.Texture = ammoItem.Item.InventorySprite;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
AmmoIcon.Hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
//AmmoIcon.Texture = InventoryManager.Instance.
|
|
|
|
|
|
|
2025-03-05 18:55:30 +01:00
|
|
|
|
UpdateCounter();
|
|
|
|
|
|
// Register this only if there's ammo
|
2025-03-06 21:16:04 +01:00
|
|
|
|
InventoryManager.Instance.TotalAmmoChanged += OnInstanceOnTotalAmmoChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
QueueFree();
|
2025-03-05 18:55:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateCounter()
|
|
|
|
|
|
{
|
|
|
|
|
|
TotalAmmoLabel.Text = InventoryManager.Instance.GetItemCount(Item.WeaponData.AmmoKey).ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|