コード例 #1
0
    IEnumerator WaveSpawnRoutine()
    {
        yield return(new WaitForSeconds(_timeBetweenWaves));

        _UIManager.OnStartNewWave(_waveNumber); // tell the UI so it can display wave number on screen
        _allWaveEnemiesAreCreated = false;
        _aliveEnemies.Clear();
        // get number of enemies
        int            numberOfEnemies     = _firstWaveNumberEnemies + _moreEnemiesPerWave * (_waveNumber - 1);
        int            enemiesCreatedSoFar = 0;
        WaitForSeconds timeBetweenEnemies  = new WaitForSeconds(_durationOfEntireWave / (float)numberOfEnemies);

        while (!_stopSpawning && !_allWaveEnemiesAreCreated)
        {
            yield return(timeBetweenEnemies);

            // spawn an enemy
            GameObject newEnemy = ChooseWeightedItem(_enemyTypes);
            newEnemy = Instantiate(newEnemy);
            newEnemy.transform.parent = _enemyContainer.transform;
            // add enemy to list
            _aliveEnemies.Add(newEnemy);
            if (++enemiesCreatedSoFar >= numberOfEnemies)
            {
                _allWaveEnemiesAreCreated = true;
            }
            // chance to add shields to enemies
            float shieldRandom = Random.Range(0f, 1f);
            if (shieldRandom < _shieldedEnemyChance)
            {
                EnemyLives lifeComponent = newEnemy.GetComponent <EnemyLives>();
                lifeComponent.ToggleShields(true);
            }
        }
    }
コード例 #2
0
    void Start()
    {
        enemyLives = GetComponent <EnemyLives>();

        _myBehavior = GetComponent <EnemyBehavior>();
        if (!_myBehavior)
        {
            Debug.LogError(this + " an enemy doesn't have a behavior script.");
        }
    }