Added basic inventory system

This commit is contained in:
cblech
2025-03-25 19:39:00 +01:00
parent 4f312209b3
commit 959ab68b4d
19 changed files with 298 additions and 1 deletions
@@ -0,0 +1,10 @@
namespace Babushka.scripts.CSharp.Common.Inventory;
public enum InventoryActionResult
{
Success,
DestinationDoesNotExists,
DestinationFull,
SourceDoesNotExists,
SourceIsEmpty
}
@@ -0,0 +1,85 @@
#nullable enable
using System;
using Godot;
using System.Collections.Generic;
namespace Babushka.scripts.CSharp.Common.Inventory;
public partial class InventoryInstance : Node
{
private List<InventorySlot> _slots = new();
public IReadOnlyList<InventorySlot> Slots => _slots;
[Signal]
public delegate void SlotAmountChangedEventHandler();
[Signal]
public delegate void InventoryContentsChangedEventHandler();
[Export]
public int SlotAmount
{
get => _slots.Count;
set
{
if (value < _slots.Count)
{
_slots.RemoveRange(value, _slots.Count - value);
}
else if (value > _slots.Count)
{
for (var i = _slots.Count; i < value; i++)
{
_slots.Add(new InventorySlot());
}
}
EmitSignal(SignalName.SlotAmountChanged);
}
}
public InventoryActionResult AddItem(ItemInstance newItem, int inventorySlot = -1)
{
if (inventorySlot < 0)
{
inventorySlot = _slots.FindIndex(slot => slot.IsEmpty());
}
if (inventorySlot < 0 || !_slots[inventorySlot].IsEmpty())
{
return InventoryActionResult.DestinationFull;
}
if (inventorySlot >= _slots.Count)
{
return InventoryActionResult.DestinationDoesNotExists;
}
_slots[inventorySlot].itemInstance = newItem;
EmitSignal(SignalName.InventoryContentsChanged);
return InventoryActionResult.Success;
}
public InventoryActionResult RemoveItem(int inventorySlot, out ItemInstance? itemInstance )
{
if (inventorySlot < 0 || inventorySlot >= _slots.Count)
{
itemInstance = null;
return InventoryActionResult.SourceDoesNotExists;
}
if (_slots[inventorySlot].IsEmpty())
{
itemInstance = null;
return InventoryActionResult.SourceIsEmpty;
}
itemInstance = _slots[inventorySlot].itemInstance;
_slots[inventorySlot].itemInstance = null;
EmitSignal(SignalName.InventoryContentsChanged);
return InventoryActionResult.Success;
}
public InventoryActionResult RemoveItem(int inventorySlot)
{
return RemoveItem(inventorySlot, out _);
}
}
@@ -0,0 +1 @@
uid://36ui8g5gu0lb
@@ -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);
}
}
@@ -0,0 +1 @@
uid://701bitgfepjc
@@ -0,0 +1,12 @@
#nullable enable
namespace Babushka.scripts.CSharp.Common.Inventory;
public class InventorySlot
{
public ItemInstance? itemInstance;
public bool IsEmpty()
{
return itemInstance == null;
}
}
@@ -0,0 +1,6 @@
using Godot;
using System;
public partial class InventoryUI : Control
{
}
@@ -0,0 +1 @@
uid://b7vlkecrn0t5c
@@ -0,0 +1,6 @@
namespace Babushka.scripts.CSharp.Common.Inventory;
public class ItemInstance
{
public ItemResource blueprint;
}
@@ -0,0 +1,18 @@
using Godot;
namespace Babushka.scripts.CSharp.Common.Inventory;
[GlobalClass]
public partial class ItemResource: Resource
{
[Export]
public string name;
[Export]
public Color color;
public ItemResource()
{
name = "";
color = Colors.Red;
}
}
@@ -0,0 +1 @@
uid://cbskymrxs6ksu
@@ -0,0 +1 @@
uid://csgctygxnj3bc