Ammo UI system

This commit is contained in:
Marco 2025-02-11 11:50:45 +01:00
commit 5eb7b578bc
22 changed files with 238 additions and 87 deletions

View file

@ -1,6 +1,10 @@
using Godot;
using System;
using System.Linq;
using System.Threading.Tasks;
using Cirno.Scripts;
using Cirno.Scripts.Resources;
using Godot.Collections;
public partial class GameManager : Node2D
{
@ -22,6 +26,10 @@ public partial class GameManager : Node2D
[Export]
public Marker2D PlayerSpawnMarker { get; set; }
[Export] public PackedScene WeaponTemplate { get; private set; }
[Export] public Array<LootItem> StartingEquipment { get; private set; }
private InventoryManager _inventoryManager { get; set; }
//private AlarmManager _alarmManager { get; set; }
@ -52,25 +60,15 @@ public partial class GameManager : Node2D
SpawnBulletsContainer();
if (PlayerSpawnMarker != null)
{
SpawnPlayer();
}
if (_inventoryManager != null && _hud != null)
{
_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
}
if (_player != null && _hud != null)
{
_player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth);
_player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable);
}
GameState = GameState.Playing;
_ = DelayPlayerSpawn();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -82,6 +80,25 @@ public partial class GameManager : Node2D
}
}
private async Task DelayPlayerSpawn()
{
await Task.Delay(500);
if (PlayerSpawnMarker != null)
{
SpawnPlayer();
SpawnWeapons();
}
if (_player != null && _hud != null)
{
_player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth);
_player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable);
}
}
public void SpawnPlayer()
{
if (_player != null) return;
@ -99,6 +116,35 @@ public partial class GameManager : Node2D
_cameraTarget.GlobalPosition = _player.Position;
}
}
private void SpawnWeapons()
{
if (!StartingEquipment.Any())
{
GD.Print("No items to spawn on Player");
return;
}
foreach (var startingItem in StartingEquipment)
{
switch (startingItem.Item)
{
case ItemTypes.Weapon:
if (WeaponTemplate == null)
{
GD.Print("Could not spawn weapon because template is null");
break;
}
var weapon = _player.CreateChild<Weapon>(WeaponTemplate);
weapon.WeaponData = startingItem.WeaponData;
_player.EquippedWeapon ??= weapon;
break;
}
_inventoryManager.AddItem(startingItem);
}
}
private void SpawnBulletsContainer()
{

View file

@ -2,6 +2,7 @@ using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts;
using Cirno.Scripts.Resources;
public partial class Hud : CanvasLayer
@ -82,12 +83,33 @@ public partial class Hud : CanvasLayer
{
Item = item,
Container = hbox,
//Amount = currentAmount
};
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();
// }
label.LabelSettings = new LabelSettings()
{
FontSize = 8
@ -116,24 +138,26 @@ public partial class Hud : CanvasLayer
// _itemsContainer.AddChild(texture);
}
public void RemoveInventoryItem(LootItem item, int currentAmount)
public void RemoveInventoryItem(string itemKey, int currentAmount)
{
if (_items.TryGetValue(item.ItemKey, out var itm))
if (_items.TryGetValue(itemKey, out var itm))
{
if (currentAmount <= 0)
{
itm.Container.QueueFree();
_items.Remove(item.ItemKey);
_items.Remove(itemKey);
}
else
{
itm.Label.Text = currentAmount.ToString();
if (itm.Item.UiType == UiItemType.IconText)
{
itm.Label.Text = currentAmount.ToString();
}
}
}
else
{
GD.Print($"Tried to remove item {item.ItemName} {item.ItemKey} but it was not found");
GD.Print($"Tried to remove item {itemKey} but it was not found");
}
// var containerItem = _items.FirstOrDefault(x => x.Item == item);
@ -152,5 +176,7 @@ public partial class Hud : CanvasLayer
public LootItem Item { get; set; }
public HBoxContainer Container { get; set; }
public Label Label { get; set; }
//public int Amount { get; set; }
}
}

View file

@ -9,8 +9,6 @@ public partial class InventoryManager : Node2D
{
public bool RedKeycard { get; set; }
private List<LootItem> _items = new List<LootItem>();
private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>();
public Dictionary<string, ItemContainer> Items => _itemsDict;
@ -19,7 +17,7 @@ public partial class InventoryManager : Node2D
public delegate void ItemAddedEventHandler(LootItem item, int currentAmount);
[Signal]
public delegate void ItemRemovedEventHandler(LootItem item, int currentAmount);
public delegate void ItemRemovedEventHandler(string itemKey, int currentAmount);
// Called when the node enters the scene tree for the first time.
public override void _Ready()
@ -30,38 +28,34 @@ public partial class InventoryManager : Node2D
public override void _Process(double delta)
{
}
public bool HasItems(List<ItemTypes> items)
public bool HasItems(List<string> itemKeys)
{
return items.Aggregate(false, (current, item) => current || HasItem(item));
return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0);
}
public bool HasItem(ItemTypes type)
public int GetItemCount(string itemKey)
{
return _items.Any(x => x.Item == type && x.Amount > 0);
return _itemsDict.TryGetValue(itemKey, out var itm) ? itm.Count : 0;
}
public int RemoveItem(LootItem item, int amount)
public int RemoveItem(string itemKey, int amount)
{
if (_itemsDict.ContainsKey(item.ItemKey))
if (!_itemsDict.TryGetValue(itemKey, out var itm)) return 0;
int removed = Math.Min(itm.Count, amount);
itm.Count -= amount;
if (itm.Count <= 0)
{
var itm = _itemsDict[item.ItemKey];
int removed = 0;
itm.Count -= amount;
if (itm.Count <= 0)
{
_itemsDict.Remove(item.ItemKey);
}
EmitSignal(nameof(ItemRemoved), item, item.Amount);
return itm.Count;
_itemsDict.Remove(itemKey);
}
EmitSignal(nameof(ItemRemoved), itemKey, itm.Count);
return removed;
return 0;
}
public bool AddItem(LootItem item)
@ -81,7 +75,7 @@ public partial class InventoryManager : Node2D
}
else
{
if (itm.Count < item.Amount)
if (itm.Count < item.Max)
{
itm.Count += item.Amount;
}
@ -96,7 +90,7 @@ public partial class InventoryManager : Node2D
return false;
}
}
EmitSignal(nameof(ItemAdded), item, itm.Count, item.Amount);
EmitSignal(nameof(ItemAdded), item, itm.Count);
}
return true;

View file

@ -7,8 +7,9 @@ public enum ItemTypes
KeycardGreen,
Ammo,
Medkit,
CrabBomb,
FrogBomb,
Bomb,
Mine,
Battery
Battery,
Weapon
}

View file

@ -1,7 +1,10 @@
using Godot;
using System;
using System.Diagnostics;
using System.Linq;
using Cirno.Scripts;
using Cirno.Scripts.Resources;
using Godot.Collections;
public partial class PlayerMovement : CharacterBody2D, IDestructible
{
@ -41,13 +44,15 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
[Export] public float MaxHealth = 32f;
[Export] public Weapon EquippedWeapon;
public Weapon EquippedWeapon { get; set; }
private float _currentHealth = 0f;
private bool _isDestroyed = false;
private GameManager _gameManager;
private InventoryManager _inventoryManager;
[Export] public Sprite2D HitboxSprite { get; set; }
@ -94,6 +99,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
_gameManager = this.GetGameManager(); //GetNode<GameManager>("/root/GameScene");
_inventoryManager = this.GetInventoryManager();
_gameManager.GameStateChange += GameManagerOnGameStateChange;
if (SelectorScene != null)
@ -102,7 +109,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
_selector.Visible = false;
}
}
private void GameManagerOnGameStateChange(GameState state)
{
switch (state)

View file

@ -8,6 +8,7 @@ public partial class LootItem : Resource
[Export] public string ItemName { get; set; }
[Export] public string ItemKey { get; set; }
[Export] public ItemTypes Item;
[Export] public WeaponResource WeaponData { get; set; }
[Export] public int Amount;
[Export] public int Max;
[Export] public bool PickupIfMaxed;

View file

@ -30,6 +30,7 @@ public partial class WeaponResource : Resource
#region Bullet spawn data
[Export] public string AmmoKey;
[Export] public float BulletSpeed = 100f;
[Export] public float BulletDamage = 1;
[Export] private float _rotationOffset = 0f;

View file

@ -30,6 +30,8 @@ public partial class Weapon : Node2D
private Node2D _bulletsContainer;
private GameManager _gameManager;
private InventoryManager _inventoryManager;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
@ -37,21 +39,31 @@ public partial class Weapon : Node2D
_muzzle = GetNode<Marker2D>("./Muzzle");
_cooldownTimer = GetNode<Timer>("./ShootTimer");
_gameManager = GetNode<GameManager>("/root/GameScene");
_gameManager = this.GetGameManager();
_inventoryManager = this.GetInventoryManager();
}
public void Reload()
{
_cooldownTimer.Start(WeaponData.ReloadTime);
if (WeaponData.InfiniteAmmo)
if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey))
{
LoadedAmmo = WeaponData.BulletCapacity;
}
else
{
// TODO: Calculate subtraction, etc
LoadedAmmo = WeaponData.BulletCapacity;
// if (_inventoryManager.GetItemCount(WeaponData.AmmoKey) <= 0) return;
var ammoToLoad = _inventoryManager.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity);
if (ammoToLoad > 0)
{
LoadedAmmo = ammoToLoad;
}
else
{
GD.Print("Out of ammo");
}
}
}