mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-08 23:25:54 +00:00
UI Inventory management
This commit is contained in:
parent
f4f193af7a
commit
c9abac3dc1
20 changed files with 262 additions and 72 deletions
|
|
@ -11,11 +11,15 @@ public partial class InventoryManager : Node2D
|
|||
|
||||
private List<LootItem> _items = new List<LootItem>();
|
||||
|
||||
[Signal]
|
||||
public delegate void ItemAddedEventHandler(LootItem item);
|
||||
private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>();
|
||||
|
||||
public Dictionary<string, ItemContainer> Items => _itemsDict;
|
||||
|
||||
[Signal]
|
||||
public delegate void ItemRemovedEventHandler(LootItem item);
|
||||
public delegate void ItemAddedEventHandler(LootItem item, int currentAmount);
|
||||
|
||||
[Signal]
|
||||
public delegate void ItemRemovedEventHandler(LootItem item, int currentAmount);
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
|
|
@ -36,48 +40,66 @@ public partial class InventoryManager : Node2D
|
|||
{
|
||||
return _items.Any(x => x.Item == type && x.Amount > 0);
|
||||
}
|
||||
|
||||
public bool AddItem(ItemTypes type, int amount = 1)
|
||||
{
|
||||
var item = new LootItem() { Item = type, Amount = amount };
|
||||
_items.Add(item);
|
||||
GD.Print($"Added {type} x{amount}");
|
||||
EmitSignal(nameof(ItemAdded), item);
|
||||
// switch (type)
|
||||
// {
|
||||
// // case ItemTypes.KeycardRed:
|
||||
// // RedKeycard = true;
|
||||
// // GD.Print($"Red Keycard x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.KeycardBlue:
|
||||
// // GD.Print($"Blue Keycard x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.KeycardGreen:
|
||||
// // GD.Print($"Green Keycard x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Ammo:
|
||||
// // GD.Print($"Ammo x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Medkit:
|
||||
// // GD.Print($"Medkit x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.CrabBomb:
|
||||
// // GD.Print($"CrabBomb x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Bomb:
|
||||
// // GD.Print($"Bomb x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Mine:
|
||||
// // GD.Print($"Mine x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Battery:
|
||||
// // GD.Print($"Battery x{amount}");
|
||||
// // break;
|
||||
// // default:
|
||||
// // return false;
|
||||
// }
|
||||
|
||||
return true; // TODO: Return false if could not be added
|
||||
public int RemoveItem(LootItem item, int amount)
|
||||
{
|
||||
if (_itemsDict.ContainsKey(item.ItemKey))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public bool AddItem(LootItem item)
|
||||
{
|
||||
//var item = new LootItem() { Item = type, Amount = amount };
|
||||
|
||||
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
|
||||
{
|
||||
if (itm.Count < item.Amount)
|
||||
{
|
||||
itm.Count += item.Amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.PickupIfMaxed)
|
||||
{
|
||||
itm.Count = item.Max;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
EmitSignal(nameof(ItemAdded), item, itm.Count, item.Amount);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddRedKeycard()
|
||||
|
|
@ -90,3 +112,9 @@ public partial class InventoryManager : Node2D
|
|||
RedKeycard = false;
|
||||
}
|
||||
}
|
||||
|
||||
public class ItemContainer
|
||||
{
|
||||
public LootItem Item { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue