void Awake() { if (InstantiateThis.tag == "Bug") { if (InstantiateThis.gameObject != null) { cockroach = InstantiateThis.GetComponent <Cockroach>(); } } }
// Update is called once per frame void Update() { // Timers m_roundTimer -= Time.deltaTime; m_antSpawnTimer -= Time.deltaTime; m_cockroachSpawnTimer -= Time.deltaTime; // // if no delay is turned on if (m_noDelay) { Vector3 spawnPosition = new Vector3(); // Pick a random side of the map int side = Random.Range(1, 5); // Spawn position is randomly chosen on along the chosen side switch (side) { case 1: spawnPosition.x = m_spawnArea.x / 2; spawnPosition.z = Random.Range(m_spawnArea.y / -2, m_spawnArea.y / 2); break; case 2: spawnPosition.x = Random.Range(m_spawnArea.y / -2, m_spawnArea.y / 2); spawnPosition.z = m_spawnArea.y / -2; break; case 3: spawnPosition.x = m_spawnArea.x / -2; spawnPosition.z = Random.Range(m_spawnArea.y / -2, m_spawnArea.y / 2); break; case 4: spawnPosition.x = Random.Range(m_spawnArea.y / -2, m_spawnArea.y / 2); spawnPosition.z = m_spawnArea.y / 2; break; } // Spawn ant if (m_antSpawnTimer <= 0.0f) { m_antSpawnTimer = m_antSpawnDelay; NavMeshHit hit = new NavMeshHit(); if (NavMesh.SamplePosition(spawnPosition, out hit, 5, -1)) { // Spawn the enemy prefab at the determined location GameObject newEnemy = Instantiate(m_antPrefab, spawnPosition, Quaternion.LookRotation((m_king.transform.position - spawnPosition).normalized)); // Allocate needed values in the enemy script Enemy enemyScript = newEnemy.GetComponent <Enemy>(); enemyScript.Players = m_players; enemyScript.Spawner = this; enemyScript.King = m_king; enemyScript.m_audioSourceSFX = m_audioSourceSFX; m_enemies.Add(enemyScript); m_antToSpawn--; // removing the limit to spanw m_enemySpawned++; // adding what has been spawned } } // Spawn cockroach if (m_cockroachSpawnTimer <= 0.0f) { m_cockroachSpawnTimer = m_cockroachSpawnDelay; NavMeshHit hit = new NavMeshHit(); if (NavMesh.SamplePosition(spawnPosition, out hit, 5, -1)) { // Spawn the cockroach prefab at the determined location GameObject newCockroach = Instantiate(m_cockroachPrefab, hit.position, Quaternion.LookRotation((m_king.transform.position - spawnPosition).normalized)); // Allocate needed values in the enemy script Cockroach cockroachScript = newCockroach.GetComponent <Cockroach>(); cockroachScript.Spawner = this; cockroachScript.King = m_king; m_enemies.Add(cockroachScript); m_cockroachToSpawn--; m_enemySpawned++; } } } else { // checking to see if there is no enemies to spawn and the timer is higher then 5 seconds if ((m_antToSpawn <= 0 && m_cockroachToSpawn <= 0) && m_roundTimer > 5.0f && m_enemySpawned <= 0) { m_roundTimer = 5.0f; // setting the timer to 5 secounds if (m_kingLaugh.Length > 0) { m_audioSourceSFX.PlayOneShot(m_kingLaugh[Random.Range(0, m_kingLaugh.Length)]); } } // round system // if the round timer is finished if (m_roundTimer <= 0.0f) { if (m_roundStarter.Length > 0) { int index = Random.Range(0, m_roundStarter.Length); if (m_roundStarter[index] != null) { m_audioSourceSFX.PlayOneShot(m_roundStarter[index]); } } m_roundTimer = m_roundLength; // start the round timer // checking to see if it doesn't go over the limit if (m_currentRound + 1 < m_antCount.Length) { m_currentRound++; // add to the current round } m_antToSpawn += m_antCount[m_currentRound] + Mathf.RoundToInt(m_antCount[m_currentRound] * 0.25f * (m_players.Length > 2 ? m_players.Length - 2 : 0)); // adding the limit that needs to be spawned m_cockroachToSpawn += m_cockroachCount[m_currentRound] + Mathf.RoundToInt(m_cockroachCount[m_currentRound] * 0.25f * (m_players.Length > 2 ? m_players.Length - 2 : 0)); // adding the limit that needs to be spawned OnRoundEnd(); // event call CalculateDelay(); } // Spawn ant if (m_antSpawnTimer <= 0.0f && m_antToSpawn > 0) { m_antSpawnTimer = m_antSpawnDelay; // start the ant spawn timer Vector3 spawnPosition = GetSpawnPosition(); // get the spawn position NavMeshHit hit = new NavMeshHit(); // if the spawn position is valid if (NavMesh.SamplePosition(spawnPosition, out hit, 5, -1)) { // Spawn the enemy prefab at the determined location GameObject newEnemy = Instantiate(m_antPrefab, spawnPosition, Quaternion.LookRotation((m_king.transform.position - spawnPosition).normalized)); // Allocate needed values in the enemy script Enemy enemyScript = newEnemy.GetComponent <Enemy>(); enemyScript.Players = m_players; enemyScript.Spawner = this; enemyScript.King = m_king; enemyScript.m_audioSourceSFX = m_audioSourceSFX; m_enemies.Add(enemyScript); m_antToSpawn--; // removing the limit to spanw m_enemySpawned++; // adding what has been spawned } } // Spawn cockroach if (m_cockroachSpawnTimer <= 0.0f && m_cockroachToSpawn > 0) { m_cockroachSpawnTimer = m_cockroachSpawnDelay; // start the cockroach spawn timer Vector3 spawnPosition = GetSpawnPosition(); // get the spawn position NavMeshHit hit = new NavMeshHit(); // if the spawn position is valid if (NavMesh.SamplePosition(spawnPosition, out hit, 5, -1)) { // Spawn the cockroach prefab at the determined location GameObject newCockroach = Instantiate(m_cockroachPrefab, hit.position, Quaternion.LookRotation((m_king.transform.position - spawnPosition).normalized)); // Allocate needed values in the enemy script Cockroach cockroachScript = newCockroach.GetComponent <Cockroach>(); cockroachScript.Spawner = this; cockroachScript.King = m_king; m_enemies.Add(cockroachScript); m_cockroachToSpawn--; m_enemySpawned++; } } } }