mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Implemented vending machine
This commit is contained in:
parent
d78daf4e18
commit
e5ffb0cf94
32 changed files with 3074 additions and 1992 deletions
|
|
@ -8,7 +8,7 @@ using Cirno.Scripts.Resources;
|
|||
public partial class InventoryManager : Node
|
||||
{
|
||||
public static InventoryManager Instance { get; private set; }
|
||||
|
||||
|
||||
public ItemsDatabase ItemsDatabase { get; set; }
|
||||
|
||||
private Dictionary<string, ItemContainer> _itemsDict = new();
|
||||
|
|
@ -25,7 +25,7 @@ public partial class InventoryManager : Node
|
|||
/// Use when equipping a weapon from the inventory screen
|
||||
/// </summary>
|
||||
[Signal] public delegate void WeaponEquipEventHandler(string itemKey);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Use when updating the weapon shown on the hud
|
||||
/// </summary>
|
||||
|
|
@ -36,10 +36,10 @@ public partial class InventoryManager : Node
|
|||
|
||||
[Signal]
|
||||
public delegate void TotalAmmoChangedEventHandler(StringName ammoKey, int count);
|
||||
|
||||
|
||||
[Signal]
|
||||
public delegate void LoadedAmmoChangedEventHandler(StringName weaponKey, int count);
|
||||
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
|
|
@ -97,14 +97,14 @@ public partial class InventoryManager : Node
|
|||
{
|
||||
EmitSignal(SignalName.TotalAmmoChanged, itemKey, itm.Count);
|
||||
}
|
||||
|
||||
|
||||
return removed;
|
||||
|
||||
}
|
||||
|
||||
public bool CanAddItem(string itemKey)
|
||||
{
|
||||
|
||||
|
||||
var found = TryGetItem(itemKey, out var itm);
|
||||
if (found)
|
||||
{
|
||||
|
|
@ -130,41 +130,55 @@ public partial class InventoryManager : Node
|
|||
public bool AddItem(LootItem item)
|
||||
{
|
||||
//var item = new LootItem() { Item = type, Amount = amount };
|
||||
var maxCount = item.Max <= 0 ? int.MaxValue : item.Max;
|
||||
|
||||
if (!_itemsDict.TryGetValue(item.ItemKey, out var itm))
|
||||
{
|
||||
var startingCount = Math.Clamp(item.Amount, 0, maxCount);
|
||||
if (startingCount <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_itemsDict.Add(item.ItemKey, new ItemContainer()
|
||||
{
|
||||
Item = item,
|
||||
Count = item.Amount,
|
||||
Count = startingCount,
|
||||
});
|
||||
GD.Print($"Added new ({item.ItemKey}) {item.Item} x{item.Amount}");
|
||||
GD.Print($"Added new ({item.ItemKey}) {item.Item} x{startingCount}");
|
||||
|
||||
EmitSignal(SignalName.ItemAdded, item, item.Amount);
|
||||
EmitSignal(SignalName.ItemAdded, item, startingCount);
|
||||
|
||||
if (item.Item is ItemTypes.Ammo)
|
||||
{
|
||||
EmitSignal(SignalName.TotalAmmoChanged, item.ItemKey, startingCount);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (itm.Count < item.Max)
|
||||
if (itm.Count >= maxCount)
|
||||
{
|
||||
itm.Count += item.Amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.PickupIfMaxed)
|
||||
{
|
||||
itm.Count = item.Max;
|
||||
}
|
||||
else
|
||||
if (!item.PickupIfMaxed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
itm.Count = Math.Clamp(itm.Count + item.Amount, 0, maxCount);
|
||||
}
|
||||
|
||||
EmitSignal(SignalName.ItemAdded, item, itm.Count);
|
||||
|
||||
if (item.Item is ItemTypes.Ammo)
|
||||
{
|
||||
EmitSignal(SignalName.TotalAmmoChanged, item.ItemKey, itm.Count);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public bool UseItem(StringName itemKey)
|
||||
{
|
||||
if (!_itemsDict.TryGetValue(itemKey, out var itm)) return false;
|
||||
|
|
@ -214,15 +228,15 @@ public partial class InventoryManager : Node
|
|||
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),
|
||||
Item = ItemsDatabase.LootItems.FirstOrDefault(y => y.ItemKey == x.Key),
|
||||
Count = x.Value
|
||||
});
|
||||
|
||||
|
||||
GD.Print($"Items after loading: {_itemsDict.Count}");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue