敌人波产卵者统一

时间:2019-06-25 15:54:43

标签: c# unity3d

我正在尝试为自己创建的自上而下游戏创建wave生成器。我已经创建了wave生成器脚本,但是当我点击play时,什么也没有发生。倒计时不会开始。理想情况下,它应从2开始,一旦达到0,则第一波应与一个敌人一起产生。一旦该敌人被杀死,倒数应从5开始,一旦达到0,则下一个有2个敌人的波浪应开始,依此类推。在所有当前敌人被消灭之前,新一波浪潮不应该开始。

public enum SpawnState { SPAWNING, WAITING, COUNTING };

public SpawnState state = SpawnState.COUNTING;

public Transform enemy;

public float timeBetweenWaves = 5f;
public float countDown = 2f;

private int waveIndex = 0;

public float searchCountdown = 1f;   

void Update()
{        
  if (state == SpawnState.WAITING)
  {           
    if (!EnemyisAlive())
       {
          WaveCompleted();
        }
        else
        {                
            return;
        }
    }

  if (countDown <= 0f)
  {          
      if (state != SpawnState.SPAWNING)
      {              
          StartCoroutine(SpawnWave());
          countDown = timeBetweenWaves;
      }
      else
      {              
          countDown -= Time.deltaTime;
      }

  }

}

void WaveCompleted()
{        
    state = SpawnState.COUNTING;
    countDown = timeBetweenWaves;
    SpawnWave();        
}

bool EnemyisAlive()
{        
    searchCountdown -= Time.deltaTime;
    if (searchCountdown <= 0)
    {
        searchCountdown = 1f;
        if (GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
        {
            return false;
        }
    }
    return true;
 }

IEnumerator SpawnWave()
 {        
    state = SpawnState.SPAWNING;
    waveIndex++;
    for (int i = 0; i < waveIndex; i++)
    {
        SpawnEnemy();
        yield return new WaitForSeconds(0.5f);

    }
    state = SpawnState.WAITING;
    yield break;
 }

void SpawnEnemy()
 {
    Instantiate(enemy, transform.position, transform.rotation);
 }   

1 个答案:

答案 0 :(得分:2)

我建议您对所有程序都使用协程。这使某些事情变得容易。您可以例如只需等到另一个Ienumertaor完成。然后,我只需将生成的敌人添加到列表中,为null个条目过滤并使用计数。使用Find或您的情况FindGameObjectsWithTag,每帧效率都非常低!

using System.Linq;
using System.Collections.Generic;

...

public Transform enemy;

public float timeBetweenWaves = 5f;
public float countDown = 2f;

//public float searchCountdown = 1f; 

private List<Transform> enemies = new List<Transform>();
private int waveIndex = 0;

private void Start()
{
    StartCoroutine(RunSpawner());
}

// this replaces your Update method
private IEnumerator RunSpawner()
{        
    // first time wait 2 seconds
    yield return new WaitForSeconds(countDown);

    // run this routine infinite
    while(true)
    {
        state = SpawnState.SPAWNING;    

        // do the spawning and at the same time wait until it's finished
        yield return SpawnWave();

        state = SpawnState.WAITING;

        // wait until all enemies died (are destroyed)
        yield return new WaitWhile(EnemyisAlive);

        state = SpawnState.COUNTING

        // wait 5 seconds
        yield return new WaitForSeconds(timeBetweenWaves);
    }
}

private bool EnemyisAlive()
{        
    // uses Linq to filter out null (previously detroyed) entries
    enemies = enemies.Where(e => e != null).ToList();

    return enemies.Count > 0;
}

private IEnumerator SpawnWave()
{        
    waveIndex++;
    for (int i = 0; i < waveIndex; i++)
    {
        SpawnEnemy();
        yield return new WaitForSeconds(0.5f);
    }
}

private void SpawnEnemy()
{
    enemies.Add(Instantiate(enemy, transform.position, transform.rotation));
}

要稍微提高效率,您还可以避免实例化和销毁,而应使用Object Pooling-仅启用和禁用对象,并最终仅在需要时才产生新对象。