cirnogodot/Scripts/InventoryManager.cs

149 lines
3 KiB
C#
Raw Normal View History

2025-01-20 16:47:15 +01:00
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
2025-01-28 14:05:38 +01:00
using Cirno.Scripts;
using Cirno.Scripts.Resources;
2025-01-20 16:47:15 +01:00
public partial class InventoryManager : Node2D
{
public bool RedKeycard { get; set; }
2025-02-28 11:17:28 +01:00
2025-02-10 17:29:14 +01:00
private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>();
2025-02-28 11:17:28 +01:00
public List<ItemContainer> Items => _itemsDict.Where(x => x.Value.Count > 0).Select(x => x.Value).ToList();
2025-02-28 11:17:28 +01:00
2025-01-31 13:03:38 +01:00
[Signal]
2025-02-10 17:29:14 +01:00
public delegate void ItemAddedEventHandler(LootItem item, int currentAmount);
2025-02-28 11:17:28 +01:00
2025-01-31 13:03:38 +01:00
[Signal]
2025-02-11 11:50:45 +01:00
public delegate void ItemRemovedEventHandler(string itemKey, int currentAmount);
[Signal]
public delegate void WeaponEquipEventHandler(string itemKey);
2025-02-28 11:17:28 +01:00
[Signal]
public delegate void ItemUsedEventHandler(LootItem itemKey, int totalCount);
2025-01-20 16:47:15 +01:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
2025-02-28 11:17:28 +01:00
2025-02-11 11:50:45 +01:00
public bool HasItems(List<string> itemKeys)
{
2025-02-11 11:50:45 +01:00
return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0);
}
2025-02-28 11:17:28 +01:00
2025-02-11 11:50:45 +01:00
public int GetItemCount(string itemKey)
{
2025-02-11 11:50:45 +01:00
return _itemsDict.TryGetValue(itemKey, out var itm) ? itm.Count : 0;
}
2025-02-10 17:29:14 +01:00
2025-02-11 11:50:45 +01:00
public int RemoveItem(string itemKey, int amount)
2025-02-10 17:29:14 +01:00
{
2025-02-11 11:50:45 +01:00
if (!_itemsDict.TryGetValue(itemKey, out var itm)) return 0;
2025-02-10 17:29:14 +01:00
2025-02-11 11:50:45 +01:00
int removed = Math.Min(itm.Count, amount);
2025-02-28 11:17:28 +01:00
2025-02-11 11:50:45 +01:00
itm.Count -= amount;
2025-02-10 17:29:14 +01:00
2025-02-11 11:50:45 +01:00
if (itm.Count <= 0)
{
_itemsDict.Remove(itemKey);
}
2025-02-28 11:17:28 +01:00
2025-02-11 11:50:45 +01:00
EmitSignal(nameof(ItemRemoved), itemKey, itm.Count);
2025-02-10 17:29:14 +01:00
2025-02-11 11:50:45 +01:00
return removed;
2025-02-10 17:29:14 +01:00
}
2025-02-28 11:17:28 +01:00
2025-02-10 17:29:14 +01:00
public bool AddItem(LootItem item)
2025-01-28 14:05:38 +01:00
{
2025-02-10 17:29:14 +01:00
//var item = new LootItem() { Item = type, Amount = amount };
2025-01-28 14:05:38 +01:00
2025-02-10 17:29:14 +01:00
if (!_itemsDict.TryGetValue(item.ItemKey, out var itm))
{
_itemsDict.Add(item.ItemKey, new ItemContainer()
{
Item = item,
Count = item.Amount,
});
GD.Print($"Added new ({item.ItemKey}) {item.Item} x{item.Amount}");
2025-02-28 11:17:28 +01:00
2025-02-10 17:29:14 +01:00
EmitSignal(nameof(ItemAdded), item, item.Amount);
}
else
{
2025-02-11 11:50:45 +01:00
if (itm.Count < item.Max)
2025-02-10 17:29:14 +01:00
{
itm.Count += item.Amount;
}
2025-02-28 11:17:28 +01:00
else
2025-02-10 17:29:14 +01:00
{
if (item.PickupIfMaxed)
{
itm.Count = item.Max;
}
else
{
return false;
}
}
2025-02-11 11:50:45 +01:00
EmitSignal(nameof(ItemAdded), item, itm.Count);
2025-02-10 17:29:14 +01:00
}
2025-02-28 11:17:28 +01:00
2025-02-10 17:29:14 +01:00
return true;
2025-01-28 14:05:38 +01:00
}
2025-02-28 11:17:28 +01:00
2025-01-20 16:47:15 +01:00
public void AddRedKeycard()
{
RedKeycard = true;
}
public void RemoveRedKeycard()
{
RedKeycard = false;
}
public bool UseItem(string itemKey)
{
if (!_itemsDict.TryGetValue(itemKey, out var itm)) return false;
if (itm.Count == 0) return false;
2025-02-28 11:17:28 +01:00
GD.Print($"Used item in manager {itemKey}");
switch (itm.Item.Item)
{
case ItemTypes.Medkit:
2025-02-28 11:17:28 +01:00
// Heal
case ItemTypes.FrogBomb:
2025-02-28 11:17:28 +01:00
// Use Bomb
case ItemTypes.Bomb:
2025-02-28 11:17:28 +01:00
// Bomb
case ItemTypes.Mine:
case ItemTypes.Battery:
2025-02-28 11:17:28 +01:00
EmitSignal(SignalName.ItemUsed, itm.Item, itm.Count);
break;
case ItemTypes.Weapon:
// Equip weapon
EmitSignal(SignalName.WeaponEquip, itm.Item.ItemKey);
break;
2025-02-28 11:17:28 +01:00
}
2025-02-28 11:17:28 +01:00
return true;
}
2025-01-20 16:47:15 +01:00
}
2025-02-10 17:29:14 +01:00
public class ItemContainer
{
public LootItem Item { get; set; }
public int Count { get; set; }
}