Able
Askowl Base Library Enabler
Set.cs
1 /*
2  * With thanks to Ryan Hipple -- https://github.com/roboryantron/Unite2017
3  */
4 
5 using System;
6 using UnityEngine;
7 
8 namespace Askowl {
9  /// <a href="http://bit.ly/2NXaCSb">Create a set in the inspector and provide an interface to pick one</a> <inheritdoc cref="Pick" />
10  [Serializable] public class Set<T> : Pick<T> {
11  #region Inspector Fields
12  /// <a href="http://bit.ly/2NXaCSb">List of elements to pick from - set in the inspector</a>
13  [SerializeField] private T[] elements = default;
14 
15  [SerializeField, Tooltip("true for sequential, false for random")]
16  private bool cycle = false;
17 
18  [SerializeField
19  , Tooltip(
20  "If the list is shorter then select items randomly, but never choose one a second time until all have been picked. This is useful for short lists to reduce unexpected repeats.")]
21  private int exhaustiveBelow = 10;
22  #endregion
23 
24  #region Picker
25  /// <a href=""></a> //#TBD#// <inheritdoc />
26  public override string ToString() =>
27  $"Set {GetType().Name}: Size = {elements.Length}, Cycle = {cycle}, Exhaustive Below = {exhaustiveBelow}";
28 
29  /// <a href=""></a> //#TBD#//
30  public int InitialSize => elements.Length;
31 
32  /// <a href="http://bit.ly/2NWwH3e">Rebuild selections if we change contents</a>
33  protected virtual void BuildSelector() {
34  built = true;
35  selector.Choices = elements;
36  }
37 
38  /// <a href="http://bit.ly/2NTAqP3">Pick one item from many</a> <inheritdoc />
39  public T Pick() => Selector.Pick();
40 
41  /// <a href="http://bit.ly/2NWwH3e">Remove all set entries</a>
42  public void Reset() {
43  built = false;
44  selector?.Reset();
45  }
46 
47  /// <a href="http://bit.ly/2NTAqP3">Selector to call to get picked elements</a>
48  protected Selector<T> Selector {
49  get {
50  if (selector == null) {
51  selector = new Selector<T>() {ExhaustiveBelow = exhaustiveBelow, IsRandom = !cycle};
52  built = false;
53  }
54  if (!built) BuildSelector();
55  return selector;
56  }
57  }
58 
59  private Selector<T> selector;
60  private bool built;
61  #endregion
62  }
63 }
Definition: Clock.cs:3
override string ToString()
//#TBD#//
T Pick()
Pick one item from many
T Pick()
Method called to pick an item
Definition: Selector.cs:21
virtual void BuildSelector()
Rebuild selections if we change contents
Definition: Set.cs:33
void Reset()
Remove all set entries
Definition: Set.cs:42
Pick one item from a list.
Definition: Selector.cs:10
Interface so that code can use a picker without know more about the source. A picker returns a value ...
Definition: Pick.cs:5
int InitialSize
//#TBD#//
Definition: Set.cs:30
Create a set in the inspector and provide an interface to pick one
Definition: Set.cs:10