Exemple #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 Dictionary <GameObject, float> GetWeights(IEnumerable <GameObject> options)
    {
        var oneminus = new Dictionary <GameObject, float>();
        var weights  = WeightTable.GetWeights(options);

        foreach (var pair in weights)
        {
            oneminus.Add(pair.Key, 1 - pair.Value);
        }
        return(oneminus);
    }