我的圆形(波浪)系统运行不正常

时间:2021-07-22 15:31:13

标签: c# unity3d

我目前正在开发一个波系统,当所有敌人都被杀死时,它应该会在所需的位置 (enemySpawners) 上产生敌人。敌人在前 2 波中以正确的数量生成。

主要问题是在第一波和第二波之后,敌人会在一个波中的所有敌人都死亡之前生成。大多数情况下,在跳过当前波之前还剩下大约 4 到 6 个敌人。

我使用对象池来实例化敌人。 这是我的 EnemyPoolManager 脚本。

public class EnemyPoolManager : MonoBehaviour{

#region Singleton Pattern

static EnemyPoolManager _instance;

public static EnemyPoolManager instance{
    get{
        if(_instance == null){
            _instance = FindObjectOfType<EnemyPoolManager>();
        }
        return _instance;
    }
}
#endregion

public int waveCount = 1;
public GameObject prefab;
public GameObject spawnerPrefab;
public Transform enemySpawnerParent;

int enemyConstant = 2;
int numOfEnemiesToSpawn = 4;
bool waveOngoing;
[SerializeField]
List<GameObject> enemySpawners = new List<GameObject>();

void Awake(){
    foreach (GameObject obj in GameObject.FindGameObjectsWithTag("EnemySpawner")){
            enemySpawners.Add(obj);
    }
}

// Start is called before the first frame update
void Start()
{
    // Create a new pool with the specified amount of enemies.
    PoolManager.instance.CreatePool(prefab, 200);
    StartWave(numOfEnemiesToSpawn);
}

// Update is called once per frame
void Update()
{
    NextWave(numOfEnemiesToSpawn, enemySpawnerParent);
}

我运行 NextWave 方法和汇集 200 个敌人的原因完全是实验性的,当我让 wave 系统按预期工作时可能会发生变化。我将在下面包含的所有方法都与我包含的第一个代码在同一个脚本中。

这里一一介绍我的 wave 方法。从 NextWave 开始:

public void NextWave(int maxEnemies, Transform parent){
    print("NextWave()");
    // If there is a wave ongoing.
    if(waveOngoing == true){    
        // Set an available spawners value.
        int availableSpawners = 0;
        // Check each enemy spawner
        foreach(GameObject enemySpawner in enemySpawners){
            // If the enemy spawner does not have an enemy parented to them.
            if(enemySpawner.transform.childCount == 0){
                // Add one to the available spawner count.
                availableSpawners++;
                print("available spawner amount: " + availableSpawners);
            }
        }
        // If available spawners are equal to the max amount of enemies.
        if (availableSpawners == maxEnemies){
            // End the wave.
            EndWave(ref maxEnemies, parent);
        }
    }
}

这是 StartWave 方法:

public void StartWave(int maxEnemies){
    print("StartWave()");
    print(maxEnemies);
    // If there is no wave ongoing.
    if(waveOngoing == false){
        // Start a wave.
        waveOngoing = true;
        print("starting wave: " + waveCount);
        // Run for each enemy.
        for(int i = 0; i < maxEnemies; i++){
            // Set the spawner of the enemy to the index given by the i value.
            GameObject enemySpawner = enemySpawners[i];
            // Spawn the enemy.
            PoolManager.instance.ReuseObj(prefab, enemySpawner.transform.position, enemySpawner.transform.rotation, enemySpawner.transform);
        }
        // Add one to the wave count.
        waveCount++;
    }
}

这里是 EndWave 方法:

public void EndWave(ref int maxEnemies, Transform parent){
    print("EndWave()");
    // If there is a wave ongoing.
    if(waveOngoing == true){
        // End the wave.
        waveOngoing = false;
        // Print the wave count.(Necessary for future UI elements.)
        print("ended wave: " + waveCount);
        // Add the enemyConstant to the max amount of enemies spawnable.
        maxEnemies += enemyConstant;
        // Evaluate how many enemies should be spawned.
        int spawnersNeeded = EvaluateSpawnerAmount(maxEnemies);
        print("Evaluated, " + spawnersNeeded + " spawners needed");
        // For each spawner that is needed.
        for(int i = 0; i < spawnersNeeded; i++){
            Vector3 offset = new Vector3(Random.Range(0, 5), Random.Range(0, 5), 0);
            // Create a new spawner.
            GameObject obj = Instantiate(spawnerPrefab, Vector3.zero + offset, Quaternion.identity);
            obj.transform.parent = parent;
            enemySpawners.Add(obj);
        }
        // Update the amount of enemy spawners there are.
        // enemySpawners = GameObject.FindGameObjectsWithTag("EnemySpawner");
        print("List Length: " + enemySpawners.Count);
        // Start the wave with the updated values.
        StartWave(maxEnemies);
    }
}

还有 EvaluateSpawnerAmount 方法:

public int EvaluateSpawnerAmount(int maxEnemies){
    // Needed spawner amount.
    int neededSpawners = 0;
    // If enemy spawners are less than the max enemies.
    if(enemySpawners.Count < maxEnemies){
        // Needed spawner amount should be the amount that is left after the subtraction of each number.
        neededSpawners = maxEnemies - enemySpawners.Count;
    }
    // Return the needed spawner amount to pass it to the for loop.
    return(neededSpawners);
}

这就是我发现需要发布的所有代码。如果需要更多代码,我将相应地编辑这篇文章。

0 个答案:

没有答案