Advertisement
Nicklaj

ElementStack

Feb 28th, 2025
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4.  
  5. [System.Serializable]
  6. public class ElementStack
  7. {
  8.     /// <summary>
  9.     /// Note about the stack system: while it is called stack, the under-the-hood implementation is actually a queue list.
  10.     /// This is because the entity is limited to a certain amount of elements in their reserve, and when there are too many
  11.     /// elements and dequeue allows to easily remove the oldest element.
  12.     /// </summary>
  13.     [field: SerializeField] public List<StackItem> CurrentElementStack { get; private set; }
  14.     [field: SerializeField] public int MaxStackSize { get; set; }
  15.  
  16.     [SerializeField] private StackItem[] _debugElementStack;
  17.  
  18.     public ElementStack(int maxStackSize)
  19.     {
  20.         CurrentElementStack = new List<StackItem>();
  21.         this.MaxStackSize = maxStackSize;
  22.     }
  23.  
  24.     /// <summary>
  25.     /// Adds specified element to the stack. If the stack is already full, the oldest element is removed.
  26.     /// </summary>
  27.     /// <param name="elementType">Type of the element to add to the stack.</param>
  28.     public void AddElementToStack(ElementType elementType)
  29.     {
  30.         if (CurrentElementStack.Count == MaxStackSize)
  31.             CurrentElementStack.Pop();
  32.         CurrentElementStack.Add(new StackItem(elementType, 1));
  33.         _debugElementStack = CurrentElementStack.ToArray();
  34.     }
  35.  
  36.     /// <summary>
  37.     /// Returns the element at the specified index in the stack.
  38.     /// </summary>
  39.     /// <param name="index">The index of the stack to check</param>
  40.     /// <returns>
  41.     /// The return is a tuple that contains, as first attribute whether an Item was found at the specified index;
  42.     /// The second parameter instead is the actual StackItem.
  43.     /// </returns>
  44.     public (bool IsAvailable, StackItem Item) GetElementInStack(int index)
  45.     {
  46.         if (index >= CurrentElementStack.Count)
  47.             return (false, new StackItem());
  48.         return (true, CurrentElementStack.ToArray()[index]);
  49.     }
  50.  
  51.     /// <summary>
  52.     /// Checks whether an element of a specific type is contained in the stack.
  53.     /// </summary>
  54.     /// <param name="type">Type of the element</param>
  55.     /// <returns>Returns true if the stack contains at least one item of the specified type.</returns>
  56.     public bool ContainsElementOfType(ElementType type)
  57.     {
  58.         return CurrentElementStack.Where(item => item.ElementType == type).Count() > 0;
  59.     }
  60.  
  61.     /// <summary>
  62.     /// Returns the first element of the specified type in the stack.
  63.     /// </summary>
  64.     /// <param name="type">Type of the element</param>
  65.     /// <returns></returns>
  66.     public (bool IsAvailable, StackItem Item) GetFirstElementOfType(ElementType type)
  67.     {
  68.         if (!ContainsElementOfType(type))
  69.             return (false, new StackItem());
  70.         return (true, CurrentElementStack.Where(item => item.ElementType == type).FirstOrDefault());
  71.     }
  72. }
  73.  
  74. /// <summary>
  75. /// Item that is stored in the ElementStack.
  76. /// </summary>
  77. [System.Serializable]
  78. public struct StackItem
  79. {
  80.     /// <summary>
  81.     /// Type of the element.
  82.     /// </summary>
  83.     public ElementType ElementType;
  84.  
  85.     /// <summary>
  86.     /// Quantity of the element. This is supposed to be between 0 and 1, allowing for partial usage down the road.
  87.     /// Eg. Perk points that allow you to use half an element to cast the spell, allowing you to cast it twice with the same amount of elements.
  88.     /// </summary>
  89.     public float Quantity;
  90.  
  91.     public StackItem(ElementType elementType, float quantity)
  92.     {
  93.         ElementType = elementType;
  94.         Quantity = quantity;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement