cajphrase

Untitled

May 8th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. // An immutable reference to the item prefab that defines its properties.
  2. public interface IItemReference
  3. {
  4.     PrefabReference prefab { get; }
  5.     ItemType type { get; } // this could be an enum to make checking between equipment and stuff easier
  6.     int maxStackSize { get; }
  7. }
  8. // Represents what a player would think of as an Item in the game.
  9. // Its super important to never duplicate or erase one of these accidentally so the quantity would need to be tightly controlled.
  10. // I've called this a readonly struct instead of an interface to make this clear
  11. public readonly struct ItemStack
  12. {
  13.     public readonly IItemReference itemReference;
  14.     public readonly int quantity;
  15.  
  16.     // example of how to work with this. Note this just throws exceptions if you try to construct an invalid stack:
  17.     public ItemStack Add(ItemStack other)
  18.     {
  19.         if (itemReference != other.itemReference)
  20.             throw new Exception();
  21.         int newQuantity = quantity + other.quantity;
  22.         if (newQuantity <= 0 || newQuantity > itemReference.maxStackSize)
  23.             throw new IndexOutOfRangeException();
  24.         return new(itemReference, newQuantity);
  25.     }
  26. }
  27. // Separates the responsibility for managing restrictions from managing quantities
  28. public interface IItemSlot
  29. {
  30.     Restriction restriction { get; } //defined as we already have it
  31.     ItemStack contents { get; }
  32.  
  33.     bool IsAllowedInSlot(IItemReference itemType);
  34.     bool CanMove(IItemSlot to);
  35. }
  36. // Represents any collection of slots, inventory, equipment etc.
  37. // Would be stored within ComponentData.
  38. public interface IItemSlotCollection : ICollection<IItemSlot>
  39. {
  40.     // Non-resizable. Each container can have whatever number of slots but it can't be changed after creation.
  41.     int Count { get; }
  42.     // allow direct readwrite access into any slot in the collection
  43.     ref IItemSlot this[] { get; }
  44.  
  45.     bool ContainsAny(IItemReference itemType);
  46.     bool Contains(ItemStack itemStack);
  47.     bool CanAdd(ItemStack itemStack)
  48. }
  49.  
  50. // This is the entry point for access by UI and gameplay code.
  51. public static class ItemUtilily
  52. {
  53.     // This is the key - this does all the restriction and quantity checking, and swaps items if they don't stack.
  54.     // Used for a drag-drop in the UI.
  55.     public static void Move(ref IItemSlot from, ref IItemSlot to);
  56.     // Used for a quick-move in the UI.
  57.     public static void MoveOrSpawnToWorld(ref IItemSlot from, IItemSlotCollection to);
  58.     // Used when picking up items.
  59.     public static void AddOrSpawnToWorld(ItemStack item, IItemSlotCollection inventory);
  60. }
Add Comment
Please, Sign In to add comment