Ejemplo n.º 1
0
    // this method iterates through the entire storage of dead Units
    // it converts all of the Units that meet the conditions of the parameter function into EnemyUnits
    // it also removes the original Units from the graveyard and sends out the final converted EnemyUnits
    public void ConvertToEnemies(ConversionCondition comparator)
    {
        List <Unit> result = new List <Unit>();

        // iterate backwards through the units, because we may remove them as we go
        for (int i = deadUnits.Count - 1; i >= 0; i--)
        {
            // if the given conditions are met with this particular unit
            if (comparator(deadUnits[i]))
            {
                Debug.Log("Converting Unit " + deadUnits[i].GetName() + " at Location " + deadUnits[i].GetMapPosition() + " into EnemyUnit...");

                // Get references
                var        oldComponent  = deadUnits[i];
                GameObject unitToConvert = deadUnits[i].gameObject;

                // convert the GameObject: add an EnemyUnit component, transfer data over, and then delete the old component
                EnemyUnit newEnemyComponent = unitToConvert.AddComponent <EnemyUnit>() as EnemyUnit;
                UnitConversions.UnitToEnemy(oldComponent, newEnemyComponent);
                deadUnits.RemoveAt(i);
                Destroy(oldComponent);

                // put it away to return at the end
                result.Add(newEnemyComponent);
            }
        }
        // "I've made new enemies, if anyone wants them"
        NewEnemiesEvent.Invoke(result);
    }