IEnumerator SpawnCoroutine() { while (true) { yield return(new WaitForSeconds(this.spawnInterval)); if (!MapManager.IsMapOpened) { continue; } if (m_numTanksSpawned >= this.numTanksPerLevel) { continue; } if (MapManager.IsGameOver) { continue; } var enemyTanks = FindObjectsOfType <EnemyTank>(); if (enemyTanks.Length >= this.maxNumTanksAtATime) { continue; } // find position for spawning // use only spawns which are not occupied EnemySpawn[] spawns = MapManager.MapObjects.OfType <EnemySpawn>().Where(s => CanSpawnTankAt(s)).ToArray(); if (spawns.Length < 1) { continue; } EnemySpawn spawn = spawns[Random.Range(0, spawns.Length)]; // spawn new tank m_numTanksSpawned++; EnemyTank enemyTank = Instantiate(MapManager.Instance.enemyTankPrefab, new Vector3(spawn.transform.position.x, MapManager.Instance.enemyTankPrefab.transform.position.y, spawn.transform.position.z), Quaternion.LookRotation(-Vector3.forward)).GetComponent <EnemyTank>(); enemyTank.SetParamsBasedOnCurrentLevel(); } }
public static bool CanSpawnTankAt(EnemySpawn spawn) { return(CanSpawnTankAt(spawn.Position)); }