Visualization for ammo

This commit is contained in:
Marco 2025-03-05 18:55:30 +01:00
commit 07ab64a0bf
12 changed files with 252 additions and 102 deletions

View file

@ -81,8 +81,10 @@ public partial class GameManager : Node2D
if (_inventoryManager != null && _hud != null)
{
_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
//_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
//_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
_inventoryManager.WeaponEquip += key => _hud.EquipWeapon(key);
}
PlayerRespawned += OnPlayerRespawned;

View file

@ -16,6 +16,9 @@ public partial class Hud : CanvasLayer
[Export]
public PackedScene SelectorScene { get; set; }
[Export]
public PackedScene WeaponContainerTemplate { get; private set; }
[Export]
private Node2D _selector;
@ -45,7 +48,7 @@ public partial class Hud : CanvasLayer
[Export]
public Control DebugMenuHolder { get; set; }
private Dictionary<string, HudItem> _items = new();
private Dictionary<string, WeaponAmmoCounter> _items = new();
private PauseMenu _pauseMenu;
@ -128,63 +131,49 @@ public partial class Hud : CanvasLayer
if (!_items.TryGetValue(item.ItemKey, out var item1))
{
var hbox = new HBoxContainer();
_itemsContainer.AddChild(hbox);
var instance = new TextureRect();
instance.Texture = item.InventorySprite;
instance.StretchMode = TextureRect.StretchModeEnum.Keep;
// change transform size?
var instance = WeaponContainerTemplate.Instantiate<WeaponAmmoCounter>();
//var instance = item.HudItemScene.Instantiate<TextureRect>();
//_in.CallDeferred("add_child", instance);
hbox.AddChild(instance);
var hudItem = new HudItem()
{
Item = item,
Container = hbox,
//Amount = currentAmount
};
_itemsContainer.CallDeferred("add_child", instance);
if (item.UiType == UiItemType.IconText)
{
var label = new Label();
label.Text = currentAmount.ToString();
// if (item.Item == ItemTypes.Weapon && item.WeaponData != null)
// {
// // Show ammo instead of item count
// var ammoItem = _items.GetValueOrDefault(item.WeaponData.AmmoKey);
//
// if (ammoItem != null)
// {
// label.Text = "0";
// }
// else
// {
// label.Text = ammoItem.
// }
//
// }
// else
// {
// label.Text = currentAmount.ToString();
// }
// var hbox = new HBoxContainer();
// _itemsContainer.AddChild(hbox);
//
// var instance = new TextureRect();
// instance.Texture = item.InventorySprite;
// instance.StretchMode = TextureRect.StretchModeEnum.Keep;
// hbox.AddChild(instance);
label.LabelSettings = _labelSettings;
hbox.AddChild(label);
hudItem.Label = label;
}
// var hudItem = new HudItem()
// {
// Item = item,
// Container = hbox,
// //Amount = currentAmount
// };
_items.Add(item.ItemKey, hudItem);
// if (item.UiType == UiItemType.IconText)
// {
// var label = new Label();
// label.Text = currentAmount.ToString();
//
//
// label.LabelSettings = _labelSettings;
// hbox.AddChild(label);
//
// hudItem.Label = label;
// }
_items.Add(item.ItemKey, instance);
instance.Init(item);
}
else
{
if (item.UiType == UiItemType.IconText && item1.Label != null)
{
item1.Label.Text = currentAmount.ToString();
}
// nothing
// if (item.UiType == UiItemType.IconText && item1.Label != null)
// {
// item1.Label.Text = currentAmount.ToString();
// }
}
// TextureRect texture = new TextureRect();
@ -244,38 +233,38 @@ public partial class Hud : CanvasLayer
}
}
public void RemoveInventoryItem(string itemKey, int currentAmount)
{
if (_items.TryGetValue(itemKey, out var itm))
{
if (currentAmount <= 0)
{
itm.Container.QueueFree();
_items.Remove(itemKey);
}
else
{
if (itm.Item.UiType == UiItemType.IconText)
{
itm.Label.Text = currentAmount.ToString();
}
}
}
else
{
GD.Print($"Tried to remove 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 void RemoveInventoryItem(string itemKey, int currentAmount)
// {
// if (_items.TryGetValue(itemKey, out var itm))
// {
// if (currentAmount <= 0)
// {
// itm.Container.QueueFree();
// _items.Remove(itemKey);
// }
// else
// {
// if (itm.Item.UiType == UiItemType.IconText)
// {
// itm.Label.Text = currentAmount.ToString();
// }
// }
// }
// else
// {
// GD.Print($"Tried to remove 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();
//
// }
private void SpawnDebugMenu()
{
@ -309,4 +298,29 @@ public partial class Hud : CanvasLayer
//public int Amount { get; set; }
}
public void EquipWeapon(string itemKey)
{
if (_items.TryGetValue(itemKey, out var localItem))
{
// It's already in the hud, abort
GD.Print($"Item with key {itemKey} is already in hud, aborting.");
return;
}
if (!InventoryManager.Instance.TryGetItem(itemKey, out var item))
{
GD.Print($"Item with key {itemKey} not found in inventory when equipping in hud.");
return;
}
// Clear all items
foreach (var hudItem in _items)
{
hudItem.Value.QueueFree();
}
_items.Clear();
AddInventoryItem(item.Item, item.Count);
}
}

View file

@ -25,6 +25,12 @@ public partial class InventoryManager : Node2D
[Signal]
public delegate void ItemUsedEventHandler(LootItem itemKey, int totalCount);
[Signal]
public delegate void TotalAmmoChangedEventHandler(StringName ammoKey, int count);
[Signal]
public delegate void LoadedAmmoChangedEventHandler(StringName weaponKey, int count);
public override void _Ready()
{
@ -36,6 +42,11 @@ public partial class InventoryManager : Node2D
{
}
public bool TryGetItem(string key, out ItemContainer item)
{
return _itemsDict.TryGetValue(key, out item);
}
public bool HasItems(List<string> itemKeys)
{
return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0);
@ -59,8 +70,13 @@ public partial class InventoryManager : Node2D
_itemsDict.Remove(itemKey);
}
EmitSignal(nameof(ItemRemoved), itemKey, itm.Count);
EmitSignal(SignalName.ItemRemoved, itemKey, itm.Count);
if (itm.Item.Item is ItemTypes.Ammo)
{
EmitSignal(SignalName.TotalAmmoChanged, itemKey, itm.Count);
}
return removed;
}
@ -78,7 +94,7 @@ public partial class InventoryManager : Node2D
});
GD.Print($"Added new ({item.ItemKey}) {item.Item} x{item.Amount}");
EmitSignal(nameof(ItemAdded), item, item.Amount);
EmitSignal(SignalName.ItemAdded, item, item.Amount);
}
else
{
@ -97,7 +113,7 @@ public partial class InventoryManager : Node2D
return false;
}
}
EmitSignal(nameof(ItemAdded), item, itm.Count);
EmitSignal(SignalName.ItemAdded, item, itm.Count);
}
return true;
@ -141,6 +157,11 @@ public partial class InventoryManager : Node2D
return true;
}
public void NotifyLoadedAmmoChange(string weaponDataItemKey, int loadedAmmo)
{
EmitSignal(SignalName.LoadedAmmoChanged, weaponDataItemKey, loadedAmmo);
}
}
public class ItemContainer

View file

@ -0,0 +1,51 @@
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.UI;
public partial class WeaponAmmoCounter : Control
{
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;
InventoryManager.Instance.LoadedAmmoChanged += (weaponKey, count) =>
{
if (weaponKey != Item.WeaponData.ItemKey) return;
LoadedAmmoLabel.Text = count.ToString();
};
if (string.IsNullOrWhiteSpace(item.WeaponData.AmmoKey))
{
TotalAmmoLabel.Hide();
AmmoIcon.Hide();
return;
}
UpdateCounter();
// Register this only if there's ammo
InventoryManager.Instance.TotalAmmoChanged += (ammoKey, count) =>
{
if (ammoKey != Item.WeaponData.AmmoKey) return;
TotalAmmoLabel.Text = count.ToString();
};
}
private void UpdateCounter()
{
TotalAmmoLabel.Text = InventoryManager.Instance.GetItemCount(Item.WeaponData.AmmoKey).ToString();
}
}

View file

@ -0,0 +1 @@
uid://chqjrv7wqk6ej

View file

@ -19,7 +19,16 @@ public partial class Weapon : Node2D
public int Ammo { get; set; } = 0;
public int LoadedAmmo { get; private set; }
private int _loadedAmmo;
public int LoadedAmmo
{
get => _loadedAmmo;
private set
{
_loadedAmmo = value;
_inventoryManager?.NotifyLoadedAmmoChange(WeaponData?.ItemKey, _loadedAmmo);
}
}
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
@ -61,7 +70,7 @@ public partial class Weapon : Node2D
{
// if (_inventoryManager.GetItemCount(WeaponData.AmmoKey) <= 0) return;
var ammoToLoad = _inventoryManager.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity);
if (ammoToLoad > 0)
{
LoadedAmmo = ammoToLoad;
@ -127,7 +136,13 @@ public partial class Weapon : Node2D
}
LoadedAmmo -= 1;
//_inventoryManager.NotifyLoadedAmmoChange(WeaponData.ItemKey, LoadedAmmo);
// if (!string.IsNullOrWhiteSpace(WeaponData?.AmmoKey))
// {
// // Notify hud to decrease weapon
//
// }
_cooldownTimer.Start(WeaponData.RateOfFire);
_cooldownTimer.Start(WeaponData?.RateOfFire ?? 0);
}
}