GameManager.cs 3.4 KB

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