Pro_Unit

RandomStack

Dec 8th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. using UnityEngine;
  6. namespace GameCore
  7. {
  8.     [System.Serializable]
  9.     public class RandomStack<T>
  10.     {
  11.         [SerializeField] IEnumerable<T> RandCollection;
  12.         [SerializeField] IEnumerable<T> StarCollection;
  13.         [SerializeField] T LastRandom;
  14.         public RandomStack (IEnumerable<T> colection)
  15.         {
  16.             RandCollection = new List<T> (colection);
  17.             StarCollection = colection;
  18.         }
  19.         public T Get ()
  20.         {
  21.             if (StarCollection.Count () == 1)
  22.                 return StarCollection.ElementAt (0);
  23.             if (RandCollection.Count () == 0)
  24.                 RandCollection = new List<T> (StarCollection);
  25.             T rand = default (T);
  26.             do
  27.             {
  28.                 rand = RandCollection.GetRandom ();
  29.             }
  30.             while (rand.Equals (LastRandom));
  31.  
  32.             Remove (rand);
  33.             LastRandom = rand;
  34.             return rand;
  35.         }
  36.  
  37.         private void Remove (T rand)
  38.         {
  39.             List<T> list = RandCollection.ToList ();
  40.             list.Remove (rand);
  41.             RandCollection = new List<T> (list);
  42.             list.Clear ();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment