mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 14:55:54 +00:00
Separated ammo and notifications from hud
This commit is contained in:
parent
d644cece7f
commit
46c433e5f7
39 changed files with 258 additions and 86 deletions
|
|
@ -5,6 +5,7 @@ using System.Linq;
|
|||
using Cirno.Scripts;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.UI;
|
||||
using Cirno.Scripts.Utils;
|
||||
|
||||
public partial class Hud : CanvasLayer
|
||||
{
|
||||
|
|
@ -44,7 +45,10 @@ public partial class Hud : CanvasLayer
|
|||
[Export] private Container _fairyTerminatedPanel;
|
||||
|
||||
[Export] private Container _hudInfoPanel;
|
||||
|
||||
[Export] public Container NotificationsContainer { get; private set; }
|
||||
|
||||
[Export] public Container WeaponContainer { get; private set; }
|
||||
|
||||
[ExportGroup("Pause Menu")] [Export]
|
||||
public Control PauseMenuContainer;
|
||||
[ExportGroup("Pause Menu")] [Export]
|
||||
|
|
@ -57,12 +61,16 @@ public partial class Hud : CanvasLayer
|
|||
[ExportGroup("Debug Menu")]
|
||||
[Export]
|
||||
public Control DebugMenuHolder { get; set; }
|
||||
|
||||
|
||||
|
||||
private Dictionary<string, WeaponAmmoCounter> _items = new();
|
||||
|
||||
private PauseMenu _pauseMenu;
|
||||
|
||||
private bool _playerDead = false; // useless
|
||||
|
||||
private WeaponAmmoCounter _weapon;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
|
|
@ -78,9 +86,20 @@ public partial class Hud : CanvasLayer
|
|||
{
|
||||
var instance = ItemNotificationTemplate.Instantiate<ItemNotification>();
|
||||
|
||||
_itemsContainer.CallDeferred("add_child", instance);
|
||||
NotificationsContainer.CallDeferred("add_child", instance);
|
||||
|
||||
instance.Init(item, currentamount, ItemsNotificationTimeout);
|
||||
|
||||
if (item.UiType.HasAnyFlag(UiItemType.Ammo | UiItemType.Energy))
|
||||
{
|
||||
AddWeapon(item);
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.UiType.HasAnyFlag(UiItemType.Icon | UiItemType.Count))
|
||||
{
|
||||
AddInventoryItem(item, currentamount);
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowMessage(string text)
|
||||
|
|
@ -165,9 +184,22 @@ public partial class Hud : CanvasLayer
|
|||
//_selector.Position = _selector.tolo
|
||||
}
|
||||
|
||||
private void AddWeapon(LootItem item)
|
||||
{
|
||||
_weapon?.Delete();
|
||||
|
||||
var instance = WeaponContainerTemplate.Instantiate<WeaponAmmoCounter>();
|
||||
|
||||
WeaponContainer.CallDeferred("add_child", instance);
|
||||
|
||||
instance.Init(item);
|
||||
|
||||
_weapon = instance;
|
||||
}
|
||||
|
||||
public void AddInventoryItem(LootItem item, int currentAmount)
|
||||
{
|
||||
if (item.UiType == UiItemType.NoUI) return;
|
||||
if (item.UiType == 0) return;
|
||||
|
||||
if (!_items.TryGetValue(item.ItemKey, out var item1))
|
||||
{
|
||||
|
|
@ -355,13 +387,14 @@ public partial class Hud : CanvasLayer
|
|||
}
|
||||
|
||||
// Clear all items
|
||||
foreach (var hudItem in _items)
|
||||
{
|
||||
hudItem.Value.Delete();
|
||||
}
|
||||
_items.Clear();
|
||||
// foreach (var hudItem in _items)
|
||||
// {
|
||||
// hudItem.Value.Delete();
|
||||
// }
|
||||
// _items.Clear();
|
||||
|
||||
AddInventoryItem(item.Item, item.Count);
|
||||
AddWeapon(item.Item);
|
||||
//AddInventoryItem(item.Item, item.Count);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@ public partial class LootItem : Resource
|
|||
[Export] public int Max;
|
||||
[Export] public bool PickupIfMaxed;
|
||||
[Export] public bool ConsumeOnUse;
|
||||
[Export] public UiItemType UiType;
|
||||
|
||||
[Export(PropertyHint.Flags, "Icon,Count,Ammo,Energy")]
|
||||
public UiItemType UiType { get; set; } = 0;
|
||||
[Export] public bool Selectable;
|
||||
[Export] public bool AutoPickup { get; private set; } = false;
|
||||
[Export] public Texture2D InventorySprite;
|
||||
|
|
@ -42,11 +44,4 @@ public partial class LootItem : Resource
|
|||
|
||||
return spawnedItem;
|
||||
}
|
||||
}
|
||||
|
||||
public enum UiItemType
|
||||
{
|
||||
NoUI,
|
||||
Icon,
|
||||
IconText
|
||||
}
|
||||
13
Scripts/Resources/UiItemType.cs
Normal file
13
Scripts/Resources/UiItemType.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
|
||||
namespace Cirno.Scripts.Resources;
|
||||
|
||||
[Flags]
|
||||
public enum UiItemType
|
||||
{
|
||||
Icon = 1 << 1,
|
||||
Count = 1 << 2,
|
||||
Ammo = 1 << 3,
|
||||
Energy = 1 << 4,
|
||||
|
||||
}
|
||||
1
Scripts/Resources/UiItemType.cs.uid
Normal file
1
Scripts/Resources/UiItemType.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://daol2o7sgpqio
|
||||
|
|
@ -13,39 +13,86 @@ public partial class WeaponAmmoCounter : Container
|
|||
[Export]
|
||||
public TextureRect AmmoIcon { get; private set; }
|
||||
|
||||
[Export]
|
||||
public Label TotalAmmoLabel { get; private set; }
|
||||
[Export]
|
||||
public Label LoadedAmmoLabel { get; private set; }
|
||||
// The actual ammo label
|
||||
[Export] public Label TotalAmmoLabel { get; private set; }
|
||||
|
||||
// Item count label
|
||||
[Export] public Label LoadedAmmoLabel { get; private set; }
|
||||
|
||||
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
|
||||
|
||||
// What's the point of having count and ammo without icon?
|
||||
|
||||
Icon.Texture = item.InventorySprite;
|
||||
|
||||
InventoryManager.Instance.LoadedAmmoChanged += OnInstanceOnLoadedAmmoChanged;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(item.WeaponData.AmmoKey))
|
||||
if (!item.UiType.HasFlag(UiItemType.Icon))
|
||||
{
|
||||
TotalAmmoLabel.Hide();
|
||||
AmmoIcon.Hide();
|
||||
return;
|
||||
Icon.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
Icon.Texture = item.InventorySprite;
|
||||
}
|
||||
|
||||
if (InventoryManager.Instance.TryGetItem(item.WeaponData.AmmoKey, out var ammoItem))
|
||||
if (!item.UiType.HasFlag(UiItemType.Count))
|
||||
{
|
||||
AmmoIcon.Texture = ammoItem.Item.InventorySprite;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
//AmmoIcon.Texture = InventoryManager.Instance.
|
||||
|
||||
}
|
||||
|
||||
UpdateCounter();
|
||||
// Register this only if there's ammo
|
||||
InventoryManager.Instance.TotalAmmoChanged += OnInstanceOnTotalAmmoChanged;
|
||||
private void ItemAmountChanged(LootItem item, int currentAmount)
|
||||
{
|
||||
if (item.ItemKey == Item.ItemKey)
|
||||
{
|
||||
LoadedAmmoLabel.Text = currentAmount.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private void ItemAmountRemoved(string key, int currentAmount)
|
||||
{
|
||||
if (key == Item.ItemKey)
|
||||
{
|
||||
LoadedAmmoLabel.Text = currentAmount.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInstanceOnLoadedAmmoChanged(StringName weaponKey, int count)
|
||||
|
|
@ -64,10 +111,12 @@ public partial class WeaponAmmoCounter : Container
|
|||
{
|
||||
InventoryManager.Instance.LoadedAmmoChanged -= OnInstanceOnLoadedAmmoChanged;
|
||||
InventoryManager.Instance.TotalAmmoChanged -= OnInstanceOnTotalAmmoChanged;
|
||||
InventoryManager.Instance.ItemAdded -= ItemAmountChanged;
|
||||
InventoryManager.Instance.ItemRemoved -= ItemAmountRemoved;
|
||||
QueueFree();
|
||||
}
|
||||
|
||||
private void UpdateCounter()
|
||||
private void UpdateAmmoCounter()
|
||||
{
|
||||
TotalAmmoLabel.Text = InventoryManager.Instance.GetItemCount(Item.WeaponData.AmmoKey).ToString();
|
||||
}
|
||||
|
|
|
|||
48
Scripts/Utils/EnumsFlagsExtensions.cs
Normal file
48
Scripts/Utils/EnumsFlagsExtensions.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
using System;
|
||||
|
||||
namespace Cirno.Scripts.Utils;
|
||||
|
||||
public static class EnumFlagsExtensions
|
||||
{
|
||||
public static bool HasAllFlags<T>(this T value, T flags) where T : Enum
|
||||
{
|
||||
var valueInt = Convert.ToUInt64(value);
|
||||
var flagsInt = Convert.ToUInt64(flags);
|
||||
return (valueInt & flagsInt) == flagsInt;
|
||||
}
|
||||
|
||||
public static bool HasAnyFlag<T>(this T value, T flags) where T : Enum
|
||||
{
|
||||
var valueInt = Convert.ToUInt64(value);
|
||||
var flagsInt = Convert.ToUInt64(flags);
|
||||
return (valueInt & flagsInt) != 0;
|
||||
}
|
||||
|
||||
public static bool HasNoFlags<T>(this T value, T flags) where T : Enum
|
||||
{
|
||||
var valueInt = Convert.ToUInt64(value);
|
||||
var flagsInt = Convert.ToUInt64(flags);
|
||||
return (valueInt & flagsInt) == 0;
|
||||
}
|
||||
|
||||
public static T AddFlags<T>(this T value, T flags) where T : Enum
|
||||
{
|
||||
var valueInt = Convert.ToUInt64(value);
|
||||
var flagsInt = Convert.ToUInt64(flags);
|
||||
return (T)Enum.ToObject(typeof(T), valueInt | flagsInt);
|
||||
}
|
||||
|
||||
public static T RemoveFlags<T>(this T value, T flags) where T : Enum
|
||||
{
|
||||
var valueInt = Convert.ToUInt64(value);
|
||||
var flagsInt = Convert.ToUInt64(flags);
|
||||
return (T)Enum.ToObject(typeof(T), valueInt & ~flagsInt);
|
||||
}
|
||||
|
||||
public static T ToggleFlags<T>(this T value, T flags) where T : Enum
|
||||
{
|
||||
var valueInt = Convert.ToUInt64(value);
|
||||
var flagsInt = Convert.ToUInt64(flags);
|
||||
return (T)Enum.ToObject(typeof(T), valueInt ^ flagsInt);
|
||||
}
|
||||
}
|
||||
1
Scripts/Utils/EnumsFlagsExtensions.cs.uid
Normal file
1
Scripts/Utils/EnumsFlagsExtensions.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://d11mwshvtc327
|
||||
Loading…
Add table
Add a link
Reference in a new issue