Advertisement
PotiSolhdoost

Untitled

Sep 30th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemyManager : MonoBehaviour
  6. {
  7. [System.Serializable]
  8. public class Wave
  9. {
  10. public List<GameObject> enemyPrefabs;
  11. public List<Transform> spawnPoints;
  12. public int enemiesPerWave;
  13. }
  14.  
  15. [System.Serializable]
  16. public class Chapter
  17. {
  18. public List<Wave> waves;
  19. }
  20.  
  21. public List<Chapter> chapters;
  22. public List<GameObject> spawnedEnemies = new List<GameObject>();
  23.  
  24. private int currentChapterIndex = 0;
  25. private int currentWaveIndex = 0;
  26. public Wave currentWave;
  27.  
  28. public float spawnSequenceTime = 3f; //seconds
  29. public float currentSpawnTime;
  30.  
  31. public Transform orbitObject; // the object you want to move
  32. public Vector3 startScale = new Vector3(0.5f, 0.5f, 0.5f); // start scale of the object
  33. public Vector3 maxScale = new Vector3(1f, 1f, 1f); // max scale at top-middle
  34. public Vector3 endScale = new Vector3(0.5f, 0.5f, 0.5f); // end scale at top-right
  35.  
  36. public float waveCompletionDelay = 3f; // time to wait between waves
  37. public GameObject waveCompletedPanel; // a reference to your UI panel
  38.  
  39.  
  40. private void Start()
  41. {
  42. currentSpawnTime = spawnSequenceTime;
  43. StartCoroutine(SpawnRoutine());
  44. }
  45.  
  46. private void Update()
  47. {
  48. currentSpawnTime -= Time.deltaTime;
  49.  
  50. // Check wave completion
  51. if (spawnedEnemies.Count == 0 && currentWave != null && currentWave.enemiesPerWave <= 0)
  52. {
  53. print("Wave completed!");
  54. MoveToNextWave();
  55. }
  56. }
  57.  
  58. IEnumerator SpawnRoutine()
  59. {
  60. while (true)
  61. {
  62. if (currentWave != null && currentWave.enemiesPerWave > 0 && currentSpawnTime <= 0)
  63. {
  64. Spawn();
  65. currentSpawnTime = spawnSequenceTime;
  66. }
  67. else if (currentWave != null && currentWave.enemiesPerWave <= 0 && spawnedEnemies.Count == 0)
  68. {
  69. if (waveCompletedPanel) waveCompletedPanel.SetActive(true);
  70. yield return new WaitForSeconds(waveCompletionDelay);
  71. if (waveCompletedPanel) waveCompletedPanel.SetActive(false);
  72. MoveToNextWave();
  73. }
  74. yield return null;
  75. }
  76. }
  77.  
  78.  
  79. private void MoveToNextWave()
  80. {
  81. currentWaveIndex++;
  82. if (currentWaveIndex >= chapters[currentChapterIndex].waves.Count)
  83. {
  84. currentWaveIndex = 0;
  85. currentChapterIndex++;
  86.  
  87. if (currentChapterIndex >= chapters.Count)
  88. {
  89. // All levels completed. Maybe end the game or loop levels.
  90. return;
  91. }
  92. }
  93. currentWave = chapters[currentChapterIndex].waves[currentWaveIndex];
  94. UpdateOrbitObject();
  95. }
  96. void UpdateOrbitObject()
  97. {
  98. if (!orbitObject) return;
  99.  
  100. float progress = (float)currentWaveIndex / (chapters[currentChapterIndex].waves.Count - 1);
  101. Vector3 startPos = new Vector3(-Screen.width / 2, Screen.height / 2, 0);
  102. Vector3 endPos = new Vector3(Screen.width / 2, Screen.height / 2, 0);
  103.  
  104. orbitObject.position = Vector3.Lerp(startPos, endPos, progress);
  105. if (progress <= 0.5f)
  106. {
  107. orbitObject.localScale = Vector3.Lerp(startScale, maxScale, progress * 2);
  108. }
  109. else
  110. {
  111. orbitObject.localScale = Vector3.Lerp(maxScale, endScale, (progress - 0.5f) * 2);
  112. }
  113. }
  114.  
  115. private void Spawn()
  116. {
  117. if (currentWave == null) return;
  118.  
  119. print("Spawn");
  120. GameObject enemyPrefab = currentWave.enemyPrefabs[Random.Range(0, currentWave.enemyPrefabs.Count)];
  121. Transform spawnPoint = currentWave.spawnPoints[Random.Range(0, currentWave.spawnPoints.Count)];
  122.  
  123. GameObject enemy = Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
  124. spawnedEnemies.Add(enemy);
  125. currentWave.enemiesPerWave--;
  126.  
  127. enemy.transform.localScale = Vector3.zero;
  128. StartCoroutine(SpawnToSize(enemy));
  129. }
  130.  
  131. IEnumerator SpawnToSize(GameObject enemy)
  132. {
  133. float timeElapsed = 0f;
  134. float duration = 1.5f;
  135. print("spawning to size");
  136. while (timeElapsed < duration)
  137. {
  138. timeElapsed += Time.deltaTime;
  139. float t = timeElapsed / duration;
  140. enemy.transform.localScale = Vector3.Lerp(Vector3.zero, new Vector3(3f, 3f, 3f), t);
  141. yield return null;
  142. }
  143. enemy.transform.localScale = new Vector3(3f, 3f, 3f);
  144. }
  145.  
  146. public void EnemyDestroyed(GameObject enemy)
  147. {
  148. spawnedEnemies.Remove(enemy);
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement