Files

71 lines
2.1 KiB
C#
Raw Permalink Normal View History

2025-03-25 19:39:00 +01:00
#nullable enable
using Godot;
namespace Babushka.scripts.CSharp.Common.Inventory;
public partial class InventoryManager : Node
{
2025-04-07 02:10:59 +02:00
public static InventoryManager Instance { get; private set; }
2025-04-17 16:10:12 +02:00
2025-03-25 19:39:00 +01:00
public InventoryInstance playerInventory;
public override void _EnterTree()
{
Instance = this;
}
public override void _Ready()
{
playerInventory = new InventoryInstance();
playerInventory.SlotAmount = 30;
}
public InventoryActionResult CreateItem(
ItemResource itemBlueprint,
InventoryInstance inventory,
2025-04-17 16:10:12 +02:00
int amount = 1,
2025-03-25 19:39:00 +01:00
int inventorySlot = -1)
{
2025-04-17 16:10:12 +02:00
var newItem = new ItemInstance { blueprint = itemBlueprint, amount = amount };
return inventorySlot < 0
? inventory.AddItem(newItem)
: inventory.AddItemToSlot(newItem, inventorySlot);
2025-03-25 19:39:00 +01:00
}
public InventoryActionResult MoveItem(
InventoryInstance sourceInventory,
int sourceSlot,
InventoryInstance destinationInventory,
int destinationSlot)
{
var remResult = sourceInventory.RemoveItem(sourceSlot, out var item);
if (remResult != InventoryActionResult.Success) return remResult;
2025-04-17 16:10:12 +02:00
var addResult = destinationInventory.AddItemToSlot(item!, destinationSlot);
if (addResult == InventoryActionResult.Success) return InventoryActionResult.Success;
2025-03-25 19:39:00 +01:00
2025-04-17 16:10:12 +02:00
// if adding in the destination failed, re-add the item into the source
sourceInventory.AddItemToSlot(item!, sourceSlot); // can not fail ... in theory
2025-03-25 19:39:00 +01:00
return addResult;
}
public InventoryActionResult RemoveItem(
InventoryInstance inventory,
int inventorySlot,
out ItemInstance? itemInstance)
{
return inventory.RemoveItem(inventorySlot, out itemInstance);
}
public InventoryActionResult RemoveItem(
InventoryInstance inventory,
int inventorySlot)
{
return inventory.RemoveItem(inventorySlot);
}
2025-04-17 16:10:12 +02:00
2025-04-14 16:49:29 +02:00
public InventoryActionResult CollectItem(ItemInstance itemInstance)
{
return playerInventory.AddItem(itemInstance);
}
2025-04-17 16:10:12 +02:00
}