Advertisement
PotiSolhdoost

Untitled

Oct 1st, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 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. public 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. private bool waveCompleted = false;
  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 (!waveCompleted && spawnedEnemies.Count == 0 && currentWave != null && currentWave.enemiesPerWave <= 0)
  52. {
  53. print("Wave completed!");
  54. waveCompleted = true;
  55. MoveToNextWave();
  56.  
  57. }
  58. }
  59.  
  60. Vector2 GetScreenWorldBounds()
  61. {
  62. Camera cam = Camera.main;
  63. Vector2 screenBounds = cam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, cam.transform.position.z));
  64. return screenBounds;
  65. }
  66.  
  67. IEnumerator SpawnRoutine()
  68. {
  69. while (true)
  70. {
  71. if (currentWave != null && currentWave.enemiesPerWave > 0 && currentSpawnTime <= 0)
  72. {
  73. Spawn();
  74. currentSpawnTime = spawnSequenceTime;
  75. }
  76. else if (currentWave != null && currentWave.enemiesPerWave <= 0 && spawnedEnemies.Count == 0)
  77. {
  78. if (waveCompletedPanel) waveCompletedPanel.SetActive(true);
  79. yield return new WaitForSeconds(waveCompletionDelay);
  80. if (waveCompletedPanel) waveCompletedPanel.SetActive(false);
  81. MoveToNextWave();
  82. }
  83. yield return null;
  84. }
  85. }
  86.  
  87.  
  88. private void MoveToNextWave()
  89. {
  90. currentWaveIndex++;
  91. Debug.Log("Current Chapter Index: " + currentChapterIndex);
  92. Debug.Log("Chapters Count: " + chapters.Count);
  93. Debug.Log("Current Wave Index: " + currentWaveIndex);
  94. Debug.Log("Current Chapter Waves Count: " + chapters[currentChapterIndex].waves.Count);
  95.  
  96. if (currentWaveIndex >= chapters[currentChapterIndex].waves.Count)
  97. {
  98. currentWaveIndex = 0;
  99. currentChapterIndex++;
  100.  
  101. if (currentChapterIndex >= chapters.Count)
  102. {
  103. // All levels completed. Maybe end the game or loop levels.
  104. return;
  105. }
  106. }
  107. currentWave = chapters[currentChapterIndex].waves[currentWaveIndex];
  108. waveCompleted = false;
  109. UpdateOrbitObject();
  110.  
  111.  
  112. }
  113.  
  114. void UpdateOrbitObject()
  115. {
  116. if (!orbitObject) return;
  117.  
  118. float progress = (float)currentWaveIndex / (chapters[currentChapterIndex].waves.Count - 1);
  119. Vector2 screenBounds = GetScreenWorldBounds();
  120. Vector3 startPos = new Vector3(-screenBounds.x, screenBounds.y, orbitObject.position.z);
  121. Vector3 endPos = new Vector3(screenBounds.x, screenBounds.y, orbitObject.position.z);
  122.  
  123. orbitObject.position = Vector3.Lerp(startPos, endPos, progress);
  124. if (progress <= 0.5f)
  125. {
  126. orbitObject.localScale = Vector3.Lerp(startScale, maxScale, progress * 2);
  127. }
  128. else
  129. {
  130. orbitObject.localScale = Vector3.Lerp(maxScale, endScale, (progress - 0.5f) * 2);
  131. }
  132. }
  133.  
  134.  
  135. private void Spawn()
  136. {
  137. if (currentWave == null) return;
  138.  
  139. print("Spawn");
  140. GameObject enemyPrefab = currentWave.enemyPrefabs[Random.Range(0, currentWave.enemyPrefabs.Count)];
  141. Transform spawnPoint = currentWave.spawnPoints[Random.Range(0, currentWave.spawnPoints.Count)];
  142.  
  143. GameObject enemy = Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
  144. spawnedEnemies.Add(enemy);
  145. currentWave.enemiesPerWave--;
  146.  
  147. enemy.transform.localScale = Vector3.zero;
  148. StartCoroutine(SpawnToSize(enemy));
  149. }
  150.  
  151. IEnumerator SpawnToSize(GameObject enemy)
  152. {
  153. float timeElapsed = 0f;
  154. float duration = 1.5f;
  155. print("spawning to size");
  156. while (timeElapsed < duration)
  157. {
  158. timeElapsed += Time.deltaTime;
  159. float t = timeElapsed / duration;
  160. enemy.transform.localScale = Vector3.Lerp(Vector3.zero, new Vector3(3f, 3f, 3f), t);
  161. yield return null;
  162. }
  163. enemy.transform.localScale = new Vector3(3f, 3f, 3f);
  164. }
  165.  
  166. public void EnemyDestroyed(GameObject enemy)
  167. {
  168. spawnedEnemies.Remove(enemy);
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement