Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- [System.Serializable]
- public class ElementStack
- {
- /// <summary>
- /// Note about the stack system: while it is called stack, the under-the-hood implementation is actually a queue list.
- /// This is because the entity is limited to a certain amount of elements in their reserve, and when there are too many
- /// elements and dequeue allows to easily remove the oldest element.
- /// </summary>
- [field: SerializeField] public List<StackItem> CurrentElementStack { get; private set; }
- [field: SerializeField] public int MaxStackSize { get; set; }
- [SerializeField] private StackItem[] _debugElementStack;
- public ElementStack(int maxStackSize)
- {
- CurrentElementStack = new List<StackItem>();
- this.MaxStackSize = maxStackSize;
- }
- /// <summary>
- /// Adds specified element to the stack. If the stack is already full, the oldest element is removed.
- /// </summary>
- /// <param name="elementType">Type of the element to add to the stack.</param>
- public void AddElementToStack(ElementType elementType)
- {
- if (CurrentElementStack.Count == MaxStackSize)
- CurrentElementStack.Pop();
- CurrentElementStack.Add(new StackItem(elementType, 1));
- _debugElementStack = CurrentElementStack.ToArray();
- }
- /// <summary>
- /// Returns the element at the specified index in the stack.
- /// </summary>
- /// <param name="index">The index of the stack to check</param>
- /// <returns>
- /// The return is a tuple that contains, as first attribute whether an Item was found at the specified index;
- /// The second parameter instead is the actual StackItem.
- /// </returns>
- public (bool IsAvailable, StackItem Item) GetElementInStack(int index)
- {
- if (index >= CurrentElementStack.Count)
- return (false, new StackItem());
- return (true, CurrentElementStack.ToArray()[index]);
- }
- /// <summary>
- /// Checks whether an element of a specific type is contained in the stack.
- /// </summary>
- /// <param name="type">Type of the element</param>
- /// <returns>Returns true if the stack contains at least one item of the specified type.</returns>
- public bool ContainsElementOfType(ElementType type)
- {
- return CurrentElementStack.Where(item => item.ElementType == type).Count() > 0;
- }
- /// <summary>
- /// Returns the first element of the specified type in the stack.
- /// </summary>
- /// <param name="type">Type of the element</param>
- /// <returns></returns>
- public (bool IsAvailable, StackItem Item) GetFirstElementOfType(ElementType type)
- {
- if (!ContainsElementOfType(type))
- return (false, new StackItem());
- return (true, CurrentElementStack.Where(item => item.ElementType == type).FirstOrDefault());
- }
- }
- /// <summary>
- /// Item that is stored in the ElementStack.
- /// </summary>
- [System.Serializable]
- public struct StackItem
- {
- /// <summary>
- /// Type of the element.
- /// </summary>
- public ElementType ElementType;
- /// <summary>
- /// Quantity of the element. This is supposed to be between 0 and 1, allowing for partial usage down the road.
- /// 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.
- /// </summary>
- public float Quantity;
- public StackItem(ElementType elementType, float quantity)
- {
- ElementType = elementType;
- Quantity = quantity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement