Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EnemyManager : MonoBehaviour
- {
- [System.Serializable]
- public class Wave
- {
- public List<GameObject> enemyPrefabs;
- public List<Transform> spawnPoints;
- public int enemiesPerWave;
- }
- [System.Serializable]
- public class Chapter
- {
- public List<Wave> waves;
- }
- public List<Chapter> chapters;
- public List<GameObject> spawnedEnemies = new List<GameObject>();
- private int currentChapterIndex = 0;
- private int currentWaveIndex = 0;
- public Wave currentWave;
- public float spawnSequenceTime = 3f; //seconds
- public float currentSpawnTime;
- public Transform orbitObject; // the object you want to move
- public Vector3 startScale = new Vector3(0.5f, 0.5f, 0.5f); // start scale of the object
- public Vector3 maxScale = new Vector3(1f, 1f, 1f); // max scale at top-middle
- public Vector3 endScale = new Vector3(0.5f, 0.5f, 0.5f); // end scale at top-right
- public float waveCompletionDelay = 3f; // time to wait between waves
- public GameObject waveCompletedPanel; // a reference to your UI panel
- private void Start()
- {
- currentSpawnTime = spawnSequenceTime;
- StartCoroutine(SpawnRoutine());
- }
- private void Update()
- {
- currentSpawnTime -= Time.deltaTime;
- // Check wave completion
- if (spawnedEnemies.Count == 0 && currentWave != null && currentWave.enemiesPerWave <= 0)
- {
- print("Wave completed!");
- MoveToNextWave();
- }
- }
- IEnumerator SpawnRoutine()
- {
- while (true)
- {
- if (currentWave != null && currentWave.enemiesPerWave > 0 && currentSpawnTime <= 0)
- {
- Spawn();
- currentSpawnTime = spawnSequenceTime;
- }
- else if (currentWave != null && currentWave.enemiesPerWave <= 0 && spawnedEnemies.Count == 0)
- {
- if (waveCompletedPanel) waveCompletedPanel.SetActive(true);
- yield return new WaitForSeconds(waveCompletionDelay);
- if (waveCompletedPanel) waveCompletedPanel.SetActive(false);
- MoveToNextWave();
- }
- yield return null;
- }
- }
- private void MoveToNextWave()
- {
- currentWaveIndex++;
- if (currentWaveIndex >= chapters[currentChapterIndex].waves.Count)
- {
- currentWaveIndex = 0;
- currentChapterIndex++;
- if (currentChapterIndex >= chapters.Count)
- {
- // All levels completed. Maybe end the game or loop levels.
- return;
- }
- }
- currentWave = chapters[currentChapterIndex].waves[currentWaveIndex];
- UpdateOrbitObject();
- }
- void UpdateOrbitObject()
- {
- if (!orbitObject) return;
- float progress = (float)currentWaveIndex / (chapters[currentChapterIndex].waves.Count - 1);
- Vector3 startPos = new Vector3(-Screen.width / 2, Screen.height / 2, 0);
- Vector3 endPos = new Vector3(Screen.width / 2, Screen.height / 2, 0);
- orbitObject.position = Vector3.Lerp(startPos, endPos, progress);
- if (progress <= 0.5f)
- {
- orbitObject.localScale = Vector3.Lerp(startScale, maxScale, progress * 2);
- }
- else
- {
- orbitObject.localScale = Vector3.Lerp(maxScale, endScale, (progress - 0.5f) * 2);
- }
- }
- private void Spawn()
- {
- if (currentWave == null) return;
- print("Spawn");
- GameObject enemyPrefab = currentWave.enemyPrefabs[Random.Range(0, currentWave.enemyPrefabs.Count)];
- Transform spawnPoint = currentWave.spawnPoints[Random.Range(0, currentWave.spawnPoints.Count)];
- GameObject enemy = Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
- spawnedEnemies.Add(enemy);
- currentWave.enemiesPerWave--;
- enemy.transform.localScale = Vector3.zero;
- StartCoroutine(SpawnToSize(enemy));
- }
- IEnumerator SpawnToSize(GameObject enemy)
- {
- float timeElapsed = 0f;
- float duration = 1.5f;
- print("spawning to size");
- while (timeElapsed < duration)
- {
- timeElapsed += Time.deltaTime;
- float t = timeElapsed / duration;
- enemy.transform.localScale = Vector3.Lerp(Vector3.zero, new Vector3(3f, 3f, 3f), t);
- yield return null;
- }
- enemy.transform.localScale = new Vector3(3f, 3f, 3f);
- }
- public void EnemyDestroyed(GameObject enemy)
- {
- spawnedEnemies.Remove(enemy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement