Added basic inventory system
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#nullable enable
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class InventoryManager : Node
|
||||
{
|
||||
public InventoryManager Instance { get; private set; }
|
||||
|
||||
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,
|
||||
int inventorySlot = -1)
|
||||
{
|
||||
var newItem = new ItemInstance { blueprint = itemBlueprint };
|
||||
var addResult = inventory.AddItem(newItem, inventorySlot);
|
||||
return addResult;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
var addResult = destinationInventory.AddItem(item!, destinationSlot);
|
||||
if(addResult == InventoryActionResult.Success) return InventoryActionResult.Success;
|
||||
|
||||
sourceInventory.AddItem(item!, sourceSlot); // can not fail ... in theory
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user