cirnogodot/Scripts/InventoryManager.cs

114 lines
2.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-10 17:29:14 +01:00
private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>();
public Dictionary<string, ItemContainer> Items => _itemsDict;
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-01-31 13:03:38 +01:00
[Signal]
2025-02-11 11:50:45 +01:00
public delegate void ItemRemovedEventHandler(string itemKey, int currentAmount);
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-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-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-10 17:29:14 +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-10 17:29:14 +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-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}");
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;
}
else
{
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
}
return true;
2025-01-28 14:05:38 +01:00
}
2025-01-20 16:47:15 +01:00
public void AddRedKeycard()
{
RedKeycard = true;
}
public void RemoveRedKeycard()
{
RedKeycard = false;
}
}
2025-02-10 17:29:14 +01:00
public class ItemContainer
{
public LootItem Item { get; set; }
public int Count { get; set; }
}