UnityUtilities
|
A RandomBag ensures that per interval [fillings * itemCount], every different item in it will be given back [fillings] times. Once the bag is empty, it is automatically refilled, either from a fixed array or by calling a delegate. More...
Public Member Functions | |
RandomBag (T[] refillItems, int fillings) | |
Initializes the bag with a array of items to refill it and the number of times the array is added to the bag. Once the bag is empty, the array will automatically be added to the bag [fillings] times again. More... | |
RandomBag (Action< List< T >> refillDelegate, int fillings) | |
Initializes the bag by calling the refillDelegate multiple times. Once the bag is empty, the delegate will automatically be called [fillings] times again. More... | |
void | SetRefillDelegate (Action< List< T >> refillDelegate) |
Sets the refill method to "When empty, this delegate is called [fillings] times to fill the bag." More... | |
void | SetRefillItems (T[] refillItems) |
Sets the refill method to "When empty, this array is added to the bag [fillings] times." More... | |
void | Reset () |
Clears the bag and refills it. More... | |
T | PopRandomItem () |
Gets a random item from the bag. If there is no item in the bag, it is automatically refilled first. More... | |
T [] | PopRandomItems (int count) |
Gets multiple random items from the bag. If there aren't enough item in the bag, it is automatically refilled. More... | |
IEnumerator< T > | GetEndlessEnumerator () |
Gets an enumerator returning an endless number of items, automatically refilling the bag when empty. More... | |
IEnumerator< T > | GetRemainderEnumerator () |
Gets an enumerator returning the items remaining in the bag. More... | |
Properties | |
int | Fillings [get] |
How often refillItems/refillDelegate is used to fill the bag when the bag is empty. More... | |
int | RemainderCount [get] |
The items currently remaining in the bag. More... | |
A RandomBag ensures that per interval [fillings * itemCount], every different item in it will be given back [fillings] times. Once the bag is empty, it is automatically refilled, either from a fixed array or by calling a delegate.
An example for that is used in some implementations of Tetris. The bag is filled with one instance (fillings=1) of each of the seven different pieces. Every time the next piece is needed, a random one is taken out of the bag until the bag is empty. That way, any two pieces are never longer than 7 pulls apart - and even that is only the case if the first and the last piece in the bag are the same one.
T | The type to be put in the bag. |
UnityUtilities.RandomBag< T >.RandomBag | ( | T [] | refillItems, |
int | fillings | ||
) |
Initializes the bag with a array of items to refill it and the number of times the array is added to the bag. Once the bag is empty, the array will automatically be added to the bag [fillings] times again.
refillItems | The array to fill the bag [fillings] times with. |
fillings | The number of times the array is added to the bag. |
UnityUtilities.RandomBag< T >.RandomBag | ( | Action< List< T >> | refillDelegate, |
int | fillings | ||
) |
Initializes the bag by calling the refillDelegate multiple times. Once the bag is empty, the delegate will automatically be called [fillings] times again.
refillDelegate | The delegate that will be called [fillings] times to fill the bag. |
fillings | The number of times refillDelegate is called. |
IEnumerator<T> UnityUtilities.RandomBag< T >.GetEndlessEnumerator | ( | ) |
Gets an enumerator returning an endless number of items, automatically refilling the bag when empty.
IEnumerator<T> UnityUtilities.RandomBag< T >.GetRemainderEnumerator | ( | ) |
Gets an enumerator returning the items remaining in the bag.
T UnityUtilities.RandomBag< T >.PopRandomItem | ( | ) |
Gets a random item from the bag. If there is no item in the bag, it is automatically refilled first.
T [] UnityUtilities.RandomBag< T >.PopRandomItems | ( | int | count | ) |
Gets multiple random items from the bag. If there aren't enough item in the bag, it is automatically refilled.
count | How many items to pull from the bag. |
void UnityUtilities.RandomBag< T >.Reset | ( | ) |
Clears the bag and refills it.
void UnityUtilities.RandomBag< T >.SetRefillDelegate | ( | Action< List< T >> | refillDelegate | ) |
Sets the refill method to "When empty, this delegate is called [fillings] times to fill the bag."
refillDelegate | The delegate that will be called [fillings] times to fill the bag once it's empty. |
void UnityUtilities.RandomBag< T >.SetRefillItems | ( | T [] | refillItems | ) |
Sets the refill method to "When empty, this array is added to the bag [fillings] times."
refillItems | The array to fill the bag [fillings] times with once it's empty. |
|
get |
How often refillItems/refillDelegate is used to fill the bag when the bag is empty.
|
get |
The items currently remaining in the bag.