GameManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using TMPro;
  2. using UnityEditor;
  3. using UnityEngine;
  4. public enum GameState
  5. {
  6. START_MENU,
  7. GAME_RUNNING,
  8. END_MENU
  9. }
  10. public class GameManager : MonoBehaviour
  11. {
  12. private GameState gameState;
  13. public float gameDuration = 30;
  14. private float timeRemaining;
  15. public GameObject player;
  16. public GameObject startMenu;
  17. public GameObject gameHud;
  18. public GameObject endMenu;
  19. //game
  20. public int numberOfEnemies = 3;
  21. public GameObject enemyPrefab;
  22. public int enemyMinPoint = 100;
  23. public int enemyMaxPoint = 150;
  24. public float minSpawnDistance = 0.5f;
  25. public float maxSpawnDistance = 1f;
  26. private float points = 0;
  27. public TextMeshProUGUI pointHud;
  28. public TextMeshProUGUI timerHud;
  29. //end game
  30. public TextMeshProUGUI endPointHud;
  31. // Start is called once before the first execution of Update after the MonoBehaviour is created
  32. void Start()
  33. {
  34. SetStartMenu();
  35. }
  36. // Update is called once per frame
  37. void Update()
  38. {
  39. switch (gameState)
  40. {
  41. case GameState.START_MENU:
  42. break;
  43. case GameState.GAME_RUNNING:
  44. CountdownTimer();
  45. break;
  46. case GameState.END_MENU:
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. private void CountdownTimer()
  53. {
  54. timeRemaining -= Time.deltaTime;
  55. timerHud.text = Mathf.Ceil(timeRemaining).ToString();
  56. if (timeRemaining <= 0)
  57. {
  58. timeRemaining = 0;
  59. timerHud.text = Mathf.Ceil(timeRemaining).ToString();
  60. EndGame();
  61. }
  62. }
  63. public void SetStartMenu()
  64. {
  65. gameState = GameState.START_MENU;
  66. startMenu?.SetActive(true);
  67. gameHud?.SetActive(false);
  68. endMenu?.SetActive(false);
  69. }
  70. public void StartGame()
  71. {
  72. gameState = GameState.GAME_RUNNING;
  73. timeRemaining = gameDuration;
  74. SetPoints(0);
  75. startMenu?.SetActive(false);
  76. gameHud?.SetActive(true);
  77. endMenu?.SetActive(false);
  78. for (int i = 0; i < numberOfEnemies; i++)
  79. {
  80. SpawnEnemy();
  81. }
  82. }
  83. public void EndGame()
  84. {
  85. gameState = GameState.END_MENU;
  86. endPointHud.text = points.ToString();
  87. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  88. for (int i = 0; i < enemies.Length; i++)
  89. {
  90. Destroy(enemies[i]);
  91. }
  92. startMenu?.SetActive(false);
  93. gameHud?.SetActive(false);
  94. endMenu?.SetActive(true);
  95. }
  96. public void CloseGame()
  97. {
  98. #if UNITY_EDITOR
  99. EditorApplication.ExitPlaymode();
  100. #else
  101. Application.Quit();
  102. #endif
  103. }
  104. public GameState GetGameState()
  105. {
  106. return gameState;
  107. }
  108. public void NotifyEnemyDeath(int points)
  109. {
  110. AddPoints(points);
  111. //spawn new enemy
  112. SpawnEnemy();
  113. }
  114. public void AddPoints(int points)
  115. {
  116. this.points += points;
  117. pointHud.text = this.points.ToString();
  118. }
  119. public void SetPoints(int points)
  120. {
  121. this.points = points;
  122. pointHud.text = this.points.ToString();
  123. }
  124. public void SpawnEnemy()
  125. {
  126. float spawnAngle = Random.Range(0, 360);
  127. float spawnDistance = Random.Range(minSpawnDistance, maxSpawnDistance);
  128. float spawnHeight = 0.2f;
  129. spawnAngle = spawnAngle * Mathf.Deg2Rad;
  130. Vector3 spawnPosition = new Vector3(
  131. player.transform.position.x + Mathf.Sin(spawnAngle) * spawnDistance,
  132. player.transform.position.y + spawnHeight,
  133. player.transform.position.z + Mathf.Cos(spawnAngle) * spawnDistance
  134. );
  135. GameObject spawnedEnemy = Instantiate(enemyPrefab);
  136. spawnedEnemy.transform.position = spawnPosition;
  137. float points = Random.Range(enemyMinPoint,enemyMaxPoint);
  138. points /= 10;
  139. points = Mathf.Round(points);
  140. points *= 10;
  141. EnemyController enemyController = spawnedEnemy.GetComponent<EnemyController>();
  142. if (enemyController != null) {
  143. enemyController.SetValues(10, (int) points);
  144. }
  145. }
  146. }