[SerializeField] private TextMeshProUGUI _waveSpawnText;
[SerializeField] private TextMeshProUGUI _waveNumberText;
在检查器中分配我们的变量。
public void UpdateWaveText(int wave)
{
_waveNumberText.text = "Wave: " + wave.ToString();
}
public IEnumerator NextWaveSpawnRoutine(int wave)
{
_waveSpawnText.gameObject.SetActive(true);
for (int i = 5; i >= 0; i--)
{
if(i > 0)
{
_waveSpawnText.text = "WAVE " + wave + " STARTS IN " + i + "!";
yield return _waveDelay;
}
else
{
_waveSpawnText.text = "WAVE " + wave + " STARTS NOW !";
yield return _nowDelay;
}
}
_waveSpawnText.gameObject.SetActive(false);
}
我们的
NextWaveSpawnRoutine 正在运行。
private int _currentWave = 1;
private int _enemiesPerWave = 5;
private float _spawnInterval = 5.0f;
private float _enemySpeedMultiplier = 1f;
private float _spawnIntervalDecreasePercentage = 10f;
private List<GameObject> _activeEnemies = new List<GameObject>();
private IEnumerator EnemySpawnRoutine()
{
yield return UIManager.Instance.NextWaveSpawnRoutine(_currentWave);
while (!_isPlayerAlive)
{
for (int i = 0; i < _enemiesPerWave; i++)
{
SpawnEnemy();
yield return new WaitForSeconds(_spawnInterval);
}
while (_activeEnemies.Count > 0)
{
yield return null;
}
_currentWave++;
_enemiesPerWave += 2;
_spawnInterval *= 1.0f - (_spawnIntervalDecreasePercentage / 100.0f);
_enemySpeedMultiplier += 0.05f;
UIManager.Instance.UpdateWaveText(_currentWave);
yield return UIManager.Instance.NextWaveSpawnRoutine(_currentWave);
}
}
private void SpawnEnemy()
{
float randomPositionX = Random.Range(_minXPosition, _maxXPosition);
_enemySpawnPosition.Set(randomPositionX, _spawnPositionY, 0f);
var enemy = Instantiate(_enemyPrefab, _enemySpawnPosition, Quaternion.identity, _enemyContainer.transform);
_activeEnemies.Add(enemy);
var enemyBehavior = enemy.GetComponent<EnemyBehavior>();
if (enemyBehavior != null)
enemyBehavior.IncreaseSpeed(_enemySpeedMultiplier);
}
public void DestroyEnemy(GameObject enemy)
{
_activeEnemies.Remove(enemy);
}
public void IncreaseSpeed(float multiplier)
{
_enemySpeed *= multiplier;
}
public void TakeDamage()
{
_animator.SetTrigger(_onEnemyDeathHash);
_enemySpeed = 0f;
_audioSource.Play();
Destroy(gameObject, 2.5f);
SpawnManager.Instance.DestroyEnemy(gameObject);
Destroy(this);
}
当我们的最后一个敌人被消灭后,我们就进入下一波。