using UnityEngine; using UnityEngine.UI; using System.Collections; using System; using System.Collections.Generic; namespace MoreMountains.Tools { /// /// A class to use to get more controlled randomness, taking values out of the bag randomly, and never getting them again. /// /// Usage : /// /// var shuffleBag = new ShuffleBag(40); /// for (int i = 0; i<40; i++) /// { /// newValue = something; /// shuffleBag.Add(newValue, amount); /// } /// /// then : /// float something = shuffleBag.Pick(); /// /// public class MMShufflebag { public int Capacity { get { return _contents.Capacity; } } public int Size { get { return _contents.Count; } } protected List _contents; protected T _currentItem; protected int _currentIndex = -1; /// /// Initializes the shufflebag /// /// public MMShufflebag(int initialCapacity) { _contents = new List(initialCapacity); } /// /// Adds the specified quantity of the item to the bag /// /// /// public void Add(T item, int quantity) { for (int i = 0; i < quantity; i++) { _contents.Add(item); } _currentIndex = Size - 1; } /// /// Returns a random item from the bag /// /// public T Pick() { if (_currentIndex < 1) { _currentIndex = Size - 1; _currentItem = _contents[0]; return _currentItem; } int position = UnityEngine.Random.Range(0, _currentIndex); _currentItem = _contents[position]; _contents[position] = _contents[_currentIndex]; _contents[_currentIndex] = _currentItem; _currentIndex--; return _currentItem; } } }