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

@ -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);
}
}