2025-01-20 16:47:15 +01:00
|
|
|
using Godot;
|
|
|
|
|
using System;
|
2025-01-30 17:24:40 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2025-01-28 14:05:38 +01:00
|
|
|
using Cirno.Scripts;
|
2025-01-30 17:24:40 +01:00
|
|
|
using Cirno.Scripts.Resources;
|
2025-01-20 16:47:15 +01:00
|
|
|
|
|
|
|
|
public partial class InventoryManager : Node2D
|
|
|
|
|
{
|
2025-03-02 16:48:18 +01:00
|
|
|
public static InventoryManager Instance { get; private set; }
|
2025-03-24 16:56:35 +01:00
|
|
|
|
|
|
|
|
public ItemsDatabase ItemsDatabase { get; set; }
|
|
|
|
|
|
2025-01-20 16:47:15 +01:00
|
|
|
public bool RedKeycard { get; set; }
|
2025-02-28 11:17:28 +01:00
|
|
|
|
2025-03-13 13:29:13 +01:00
|
|
|
private Dictionary<string, ItemContainer> _itemsDict = new();
|
2025-02-28 11:17:28 +01:00
|
|
|
|
2025-02-25 18:42:11 +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);
|
2025-02-25 18:42:11 +01:00
|
|
|
|
|
|
|
|
[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-03-05 18:55:30 +01:00
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
|
public delegate void TotalAmmoChangedEventHandler(StringName ammoKey, int count);
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
|
public delegate void LoadedAmmoChangedEventHandler(StringName weaponKey, int count);
|
2025-03-02 16:48:18 +01:00
|
|
|
|
2025-01-20 16:47:15 +01:00
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
2025-03-02 16:48:18 +01:00
|
|
|
Instance = this;
|
2025-03-24 16:56:35 +01:00
|
|
|
GD.Print("Loading items");
|
|
|
|
|
Load(GlobalState.Session.Items);
|
2025-01-20 16:47:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-03-05 18:55:30 +01:00
|
|
|
public bool TryGetItem(string key, out ItemContainer item)
|
|
|
|
|
{
|
|
|
|
|
return _itemsDict.TryGetValue(key, out item);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 13:29:13 +01:00
|
|
|
public bool HasItems(IList<string> itemKeys)
|
2025-01-30 17:24:40 +01:00
|
|
|
{
|
2025-02-11 11:50:45 +01:00
|
|
|
return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0);
|
2025-01-30 17:24:40 +01:00
|
|
|
}
|
2025-02-28 11:17:28 +01:00
|
|
|
|
2025-02-11 11:50:45 +01:00
|
|
|
public int GetItemCount(string itemKey)
|
2025-01-30 17:24:40 +01:00
|
|
|
{
|
2025-02-11 11:50:45 +01:00
|
|
|
return _itemsDict.TryGetValue(itemKey, out var itm) ? itm.Count : 0;
|
2025-01-30 17:24:40 +01:00
|
|
|
}
|
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-03-06 21:16:04 +01:00
|
|
|
if (itm.Count < 0)
|
2025-02-11 11:50:45 +01:00
|
|
|
{
|
2025-03-06 21:16:04 +01:00
|
|
|
itm.Count = 0;
|
|
|
|
|
//_itemsDict.Remove(itemKey);
|
2025-02-11 11:50:45 +01:00
|
|
|
}
|
2025-02-28 11:17:28 +01:00
|
|
|
|
2025-03-05 18:55:30 +01:00
|
|
|
EmitSignal(SignalName.ItemRemoved, itemKey, itm.Count);
|
2025-02-10 17:29:14 +01:00
|
|
|
|
2025-03-05 18:55:30 +01:00
|
|
|
if (itm.Item.Item is ItemTypes.Ammo)
|
|
|
|
|
{
|
|
|
|
|
EmitSignal(SignalName.TotalAmmoChanged, itemKey, itm.Count);
|
|
|
|
|
}
|
|
|
|
|
|
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-03-05 18:55:30 +01:00
|
|
|
EmitSignal(SignalName.ItemAdded, item, item.Amount);
|
2025-02-10 17:29:14 +01:00
|
|
|
}
|
|
|
|
|
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-03-05 18:55:30 +01:00
|
|
|
EmitSignal(SignalName.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-03-06 21:16:04 +01:00
|
|
|
|
2025-03-13 13:29:13 +01:00
|
|
|
public bool UseItem(StringName itemKey)
|
2025-02-25 18:42:11 +01:00
|
|
|
{
|
|
|
|
|
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}");
|
2025-02-25 18:42:11 +01:00
|
|
|
|
|
|
|
|
switch (itm.Item.Item)
|
|
|
|
|
{
|
|
|
|
|
case ItemTypes.Medkit:
|
|
|
|
|
case ItemTypes.FrogBomb:
|
|
|
|
|
case ItemTypes.Bomb:
|
|
|
|
|
case ItemTypes.Mine:
|
|
|
|
|
case ItemTypes.Battery:
|
2025-02-28 11:17:28 +01:00
|
|
|
EmitSignal(SignalName.ItemUsed, itm.Item, itm.Count);
|
2025-03-12 16:31:53 +01:00
|
|
|
if (itm.Item.ConsumeOnUse)
|
|
|
|
|
{
|
|
|
|
|
RemoveItem(itm.Item.ItemKey, 1);
|
|
|
|
|
}
|
2025-02-25 18:42:11 +01:00
|
|
|
break;
|
|
|
|
|
case ItemTypes.Weapon:
|
|
|
|
|
// Equip weapon
|
|
|
|
|
EmitSignal(SignalName.WeaponEquip, itm.Item.ItemKey);
|
|
|
|
|
break;
|
2025-02-28 11:17:28 +01:00
|
|
|
|
2025-02-25 18:42:11 +01:00
|
|
|
}
|
2025-02-28 11:17:28 +01:00
|
|
|
|
2025-02-25 18:42:11 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2025-03-05 18:55:30 +01:00
|
|
|
|
2025-03-13 13:29:13 +01:00
|
|
|
public void NotifyLoadedAmmoChange(StringName weaponDataItemKey, int loadedAmmo)
|
2025-03-05 18:55:30 +01:00
|
|
|
{
|
|
|
|
|
EmitSignal(SignalName.LoadedAmmoChanged, weaponDataItemKey, loadedAmmo);
|
|
|
|
|
}
|
2025-03-24 16:56:35 +01:00
|
|
|
|
|
|
|
|
public Godot.Collections.Dictionary<string, int> Save()
|
|
|
|
|
{
|
|
|
|
|
Godot.Collections.Dictionary<string, int> dict = new();
|
|
|
|
|
foreach (var item in _itemsDict)
|
|
|
|
|
{
|
|
|
|
|
dict.Add(item.Key, item.Value.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Load(Godot.Collections.Dictionary<string, int> items)
|
|
|
|
|
{
|
|
|
|
|
ItemsDatabase = ResourceLoader.Load<ItemsDatabase>("uid://cdi84udi6gldt");
|
|
|
|
|
|
|
|
|
|
_itemsDict.Clear();
|
|
|
|
|
|
|
|
|
|
_itemsDict = items.ToDictionary(x => x.Key, x => new ItemContainer()
|
|
|
|
|
{
|
|
|
|
|
Item = ItemsDatabase.LootItems.FirstOrDefault(y => y.ItemKey == x.Key),
|
|
|
|
|
Count = x.Value
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
GD.Print($"Items after loading: {_itemsDict.Count}");
|
|
|
|
|
}
|
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; }
|
|
|
|
|
}
|