Ejemplo n.º 1
0
    void DetectPercentageChange()
    {
        if (SpawnedModel.Length <= 0)
        {
            return;
        }

        _CurrentPercentageTotal = 0;

        int becomeGreaterIndex = -1;

        // calculate total percentage and detect the increasing ones
        for (int i = 0; i < SpawnedModel.Length; i++)
        {
            SpawnModel currentModel = SpawnedModel [i];
            // detect change greater
            if (currentModel.Percentage > currentModel.GetPrevPercentage())
            {
                becomeGreaterIndex = i;
            }
            _CurrentPercentageTotal += currentModel.Percentage;
            currentModel.SetPrevPercentage(currentModel.Percentage);
        }

        // decrease from other
        float RemainingPercentage = 100 - _CurrentPercentageTotal;

        if (RemainingPercentage < 0)
        {
            if (SpawnedModel.Length > 1)
            {
                for (int i = 0; i < SpawnedModel.Length; i++)
                {
                    SpawnModel currentModel = SpawnedModel [i];

                    //don't decrease from currently increasing model
                    if (i != becomeGreaterIndex)
                    {
                        int decreasingValue = (int)Mathf.Ceil((-RemainingPercentage) / (SpawnedModel.Length - 1));

                        if (currentModel.Percentage - decreasingValue > 0)
                        {
                            currentModel.Percentage -= decreasingValue;
                        }
                        else
                        {
                            // clamp to 0
                            currentModel.Percentage = 0;
                        }
                    }
                }
            }
        }
    }