4 using System.Collections.Generic;
17 private T[] choices = { };
18 private Func<T> picker;
22 if (picker != null)
return picker();
24 if (choices.Length == 0) {
25 picker = () =>
default;
26 }
else if (choices.Length == 1) {
27 picker = () => choices[0];
30 picker = () => choices[++cycleIndex % choices.Length];
32 picker = () => choices[cycleIndex =
Random.Range(0, choices.Length)];
34 remainingSelections =
new List<T>(collection: choices);
37 if (remainingSelections.Count == 0) remainingSelections =
new List<T>(collection: choices);
39 cycleIndex =
Random.Range(0, remainingSelections.Count);
40 T result = remainingSelections[index: cycleIndex];
41 remainingSelections.RemoveAt(index: cycleIndex);
52 if (next == -1) next = cycleIndex;
53 next = (next + 1) % choices.Length;
54 return next == (cycleIndex % choices.Length) ? default : choices[next];
58 public void Top() => cycleIndex = -1;
69 private int cycleIndex, next;
74 private List<T> remainingSelections;
77 public void Reset() => choices = emptyChoices;
79 private readonly T[] emptyChoices = { };
void Reset()
Remove all choices for an empty list
T Pick()
Method called to pick an item
T [] Choices
Used to update the choices to a new set using the same picker.
T Next()
Called when a pick fails and we need to try something else //#TBD#//
bool IsRandom
Defaults to random. Set false to cycle through entries sequentially
int ExhaustiveBelow
If the list is shorter then select items randomly, but never choose one a second time until all have ...
Pick one item from a list.
int CycleIndex
The location of the next choice in the sequence.
Interface so that code can use a picker without know more about the source. A picker returns a value ...