Example #1
0
    public GameObject SelectUnit(IEnumerable <GameObject> options)
    {
        if (!_initialized)
        {
            Commander commander = GetComponent <Commander>();
            WeightTable.Initialize(commander, commander.UnitSource.GetAvailableUnitPrefabs());
            _initialized = true;
        }

        var tableWeights  = WeightTable.GetWeights(options);
        var randomWeights = options.Select(x => tableWeights[x] * UnityEngine.Random.Range(WeightRandomizer.x, WeightRandomizer.y));
        var zip           = randomWeights.Zip(options, (weight, option) => new { weight, option });


        float      highestWeight = float.MinValue;
        GameObject highestUnit   = null;

        foreach (var entry in zip)
        {
            if (entry.weight < 0.000001f) // Weight of 0 means don't.
            {
                continue;
            }

            if (entry.weight > highestWeight)
            {
                highestWeight = entry.weight;
                highestUnit   = entry.option;
            }
        }

        UpdateWeightDebug(tableWeights, highestUnit);

        return(highestUnit);
    }
 public override void Initialize(Commander commander, IEnumerable <GameObject> availableUnits)
 => WeightTable.Initialize(commander, availableUnits);