Advertisement
Nicklaj

Untitled

Feb 27th, 2025
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. [System.Serializable]
  5. public class ElementStack
  6. {
  7.     /// <summary>
  8.     /// Note about the stack system: while it is called stack, the under-the-hood implementation is actually a queue.
  9.     /// This is because the entity is limited to a certain amount of elements in their reserve, and when there are too many
  10.     /// elements, the queue allows to easily remove the oldest element and insert the newest.
  11.     /// </summary>
  12.     [SerializeField] public Queue<StackItem> CurrentElementStack { get; private set; }
  13.     [SerializeField] public int MaxStackSize { get; set; }
  14.  
  15.     ElementStack(int maxStackSize)
  16.     {
  17.         CurrentElementStack = new Queue<StackItem>();
  18.         this.MaxStackSize = maxStackSize;
  19.     }
  20.  
  21.     /// <summary>
  22.     /// Adds specified element to the stack. If the stack is already full, the oldest element is removed.
  23.     /// </summary>
  24.     /// <param name="elementType">Type of the element to add to the stack.</param>
  25.     public void AddElementToStack(ElementType elementType)
  26.     {
  27.         if (CurrentElementStack.Count == MaxStackSize)
  28.             CurrentElementStack.Dequeue();
  29.         CurrentElementStack.Enqueue(new StackItem(elementType, 1));
  30.     }
  31.  
  32.     /// <summary>
  33.     /// Returns the element at the specified index in the stack.
  34.     /// </summary>
  35.     /// <param name="index">The index of the stack to check</param>
  36.     /// <returns>
  37.     /// The return is a tuple that contains, as first attribute whether an Item was found at the specified index;
  38.     /// The second parameter instead is the actual StackItem.
  39.     /// </returns>
  40.     public (bool IsAvailable, StackItem Item) GetElementInStack(int index)
  41.     {
  42.         if (index >= CurrentElementStack.Count)
  43.             return (false, new StackItem());
  44.         return (true, CurrentElementStack.ToArray()[index]);
  45.     }
  46. }
  47.  
  48. /// <summary>
  49. /// Item that is stored in the ElementStack.
  50. /// </summary>
  51. public struct StackItem
  52. {
  53.     /// <summary>
  54.     /// Type of the element.
  55.     /// </summary>
  56.     public ElementType ElementType;
  57.  
  58.     /// <summary>
  59.     /// Quantity of the element. This is supposed to be between 0 and 1, allowing for partial usage down the road.
  60.     /// 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.
  61.     /// </summary>
  62.     public float Quantity;
  63.  
  64.     public StackItem(ElementType elementType, float quantity)
  65.     {
  66.         ElementType = elementType;
  67.         Quantity = quantity;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement