UnityUtilities
UnityUtilities.RandomBag< T > Class Template Reference

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...
 
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...
 

Detailed Description

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.

Template Parameters
TThe type to be put in the bag.

Constructor & Destructor Documentation

§ RandomBag() [1/2]

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.

Parameters
refillItemsThe array to fill the bag [fillings] times with.
fillingsThe number of times the array is added to the bag.

§ RandomBag() [2/2]

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.

Parameters
refillDelegateThe delegate that will be called [fillings] times to fill the bag.
fillingsThe number of times refillDelegate is called.

Member Function Documentation

§ GetEndlessEnumerator()

IEnumerator<T> UnityUtilities.RandomBag< T >.GetEndlessEnumerator ( )

Gets an enumerator returning an endless number of items, automatically refilling the bag when empty.

Returns
An enumerator returning an endless number of items from the bag.

§ GetRemainderEnumerator()

IEnumerator<T> UnityUtilities.RandomBag< T >.GetRemainderEnumerator ( )

Gets an enumerator returning the items remaining in the bag.

Returns
An enumerator returning the items currently remaining in the bag.

§ PopRandomItem()

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.

Returns
A random item from the bag.

§ PopRandomItems()

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.

Parameters
countHow many items to pull from the bag.
Returns
A number of random items from the bag.

§ Reset()

void UnityUtilities.RandomBag< T >.Reset ( )

Clears the bag and refills it.

§ SetRefillDelegate()

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."

Parameters
refillDelegateThe delegate that will be called [fillings] times to fill the bag once it's empty.

§ SetRefillItems()

void UnityUtilities.RandomBag< T >.SetRefillItems ( T []  refillItems)

Sets the refill method to "When empty, this array is added to the bag [fillings] times."

Parameters
refillItemsThe array to fill the bag [fillings] times with once it's empty.

Property Documentation

§ Fillings

int UnityUtilities.RandomBag< T >.Fillings
get

How often refillItems/refillDelegate is used to fill the bag when the bag is empty.

§ RemainderCount

int UnityUtilities.RandomBag< T >.RemainderCount
get

The items currently remaining in the bag.


The documentation for this class was generated from the following file: