Beispiel #1
0
    private GameObject FindInactive(List <GameObject> pool, SO_Enemy _enemyArch, int lastActive)                //Finds the next inactive (not currently chasing the player) enemy by iterating through the list of enemies that its given. if none are found then it adds another by calling expandPool()
    {
        bool _foundGO = false;                                                                                  //temp bool used to break out of for loops

        lastActive++;                                                                                           //makes sure it starts one after the last one accessed

        for (int i = lastActive; i < pool.Count; i++)                                                           //starts iterating through the pooled enemies to find an inactive enemy
        {
            if (!pool[i].activeSelf)
            {
                _foundGO   = true;
                lastActive = i;
                return(pool [i]);                                                                                       //if an inactive enemy is found then it changes _foundGO to true, returns the gameobject and breaks out of the loop (stops looking)
            }
        }

        if (!_foundGO)                                                                                                                                          //if an the end of the list was reached before an inactive object was found then it tries again from index 0
        {
            for (int i = 0; i < lastActive; i++)                                                                                                                //looks from the start of the list to the first one checked
            {
                if (!pool [i].activeSelf)
                {
                    _foundGO = true;    lastActive = i; return(pool [i]);
                }                                                       //if one is found then it returns the game object, sets the new last active, and breaks out of the loop (stops checking)
            }
        }                                                               //if a gameobject was found then it breaks out of the method
        if (!_foundGO)                                                  //if the entire list has been checked and nothing has been found
        {
            ExpandPool(pool, _enemyArch, false, poolLocation.position); //then an new enemy is created, added to the list and moved to the pool location
            return(pool [pool.Count]);                                  //return the last gameobject (enemy) in the list
        }
        return(pool[lastActive]);                                       //system needed a a gaurenteed return value, may cause issues
    }
Beispiel #2
0
 private void ExpandPool(List <GameObject> _pool, SO_Enemy enemyArch, bool toActivate, Vector3 loc)          //used to create more enemies if they are needed; pool is the list to be added to, enemy arch is the type of enemy to add, to activate is wether or not it should be active (pooled or spawned), loc is where it should be spawned
 {
     _pool.Add(enemyArch.CreateEnemyInstance(loc, toActivate));                                              //Adds an enemy to the list
 }