Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // An immutable reference to the item prefab that defines its properties.
- public interface IItemReference
- {
- PrefabReference prefab { get; }
- ItemType type { get; } // this could be an enum to make checking between equipment and stuff easier
- int maxStackSize { get; }
- }
- // Represents what a player would think of as an Item in the game.
- // Its super important to never duplicate or erase one of these accidentally so the quantity would need to be tightly controlled.
- // I've called this a readonly struct instead of an interface to make this clear
- public readonly struct ItemStack
- {
- public readonly IItemReference itemReference;
- public readonly int quantity;
- // example of how to work with this. Note this just throws exceptions if you try to construct an invalid stack:
- public ItemStack Add(ItemStack other)
- {
- if (itemReference != other.itemReference)
- throw new Exception();
- int newQuantity = quantity + other.quantity;
- if (newQuantity <= 0 || newQuantity > itemReference.maxStackSize)
- throw new IndexOutOfRangeException();
- return new(itemReference, newQuantity);
- }
- }
- // Separates the responsibility for managing restrictions from managing quantities
- public interface IItemSlot
- {
- Restriction restriction { get; } //defined as we already have it
- ItemStack contents { get; }
- bool IsAllowedInSlot(IItemReference itemType);
- bool CanMove(IItemSlot to);
- }
- // Represents any collection of slots, inventory, equipment etc.
- // Would be stored within ComponentData.
- public interface IItemSlotCollection : ICollection<IItemSlot>
- {
- // Non-resizable. Each container can have whatever number of slots but it can't be changed after creation.
- int Count { get; }
- // allow direct readwrite access into any slot in the collection
- ref IItemSlot this[] { get; }
- bool ContainsAny(IItemReference itemType);
- bool Contains(ItemStack itemStack);
- bool CanAdd(ItemStack itemStack)
- }
- // This is the entry point for access by UI and gameplay code.
- public static class ItemUtilily
- {
- // This is the key - this does all the restriction and quantity checking, and swaps items if they don't stack.
- // Used for a drag-drop in the UI.
- public static void Move(ref IItemSlot from, ref IItemSlot to);
- // Used for a quick-move in the UI.
- public static void MoveOrSpawnToWorld(ref IItemSlot from, IItemSlotCollection to);
- // Used when picking up items.
- public static void AddOrSpawnToWorld(ItemStack item, IItemSlotCollection inventory);
- }
Add Comment
Please, Sign In to add comment