GameManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections;
  3. using TMPro;
  4. using UnityEngine;
  5. using Random = UnityEngine.Random;
  6. enum GameState
  7. {
  8. START_MENU,
  9. GAME_RUNNING,
  10. GAME_FINISHED,
  11. }
  12. public class GameManager : MonoBehaviour
  13. {
  14. public GameObject player;
  15. private GameState gameState;
  16. private int points = 0;
  17. // Game
  18. [Header("Game Settings")]
  19. public float gameDuration = 30.0f;
  20. private float gameTimeRemaining = 0;
  21. private bool allowTargetSpawn = false;
  22. #region Target spawning variables
  23. [Header("Target Spawning")]
  24. public GameObject targetPrefab;
  25. public int numberOfTargets = 3;
  26. public float targetSpawnDelay = 1.0f;
  27. public float targetMoveSpeed = 1.0f;
  28. public float targetWaitTime = 1.0f;
  29. public int targetMinPoint = 1;
  30. public int targetMaxPoint = 1;
  31. public float targetSpawnMinDistance = 1.0f;
  32. public float targetSpawnMaxDistance = 1.0f;
  33. public float targetSpawnMinHeight = 1.0f;
  34. public float targetSpawnMaxHeight = 1.0f;
  35. public float targetSpawnAngle = 90;
  36. public float targetMovePointMinDistance = 1.0f;
  37. public float targetMovePointMaxDistance = 1.0f;
  38. #endregion
  39. #region HUD
  40. [Header("HUD")]
  41. public TextMeshPro pointsHud;
  42. public TextMeshPro timeRemainingHud;
  43. public GameObject startMenu;
  44. public GameObject endMenu;
  45. #endregion
  46. // Start is called once before the first execution of Update after the MonoBehaviour is created
  47. void Start()
  48. {
  49. StartMenuGame();
  50. }
  51. // Update is called once per frame
  52. void Update()
  53. {
  54. switch (gameState) {
  55. case GameState.START_MENU:
  56. break;
  57. case GameState.GAME_RUNNING:
  58. gameTimeRemaining -= Time.deltaTime;
  59. if (gameTimeRemaining <= 0)
  60. {
  61. gameTimeRemaining = 0;
  62. EndGame();
  63. }
  64. timeRemainingHud.text = (Mathf.Floor(gameTimeRemaining * 100) / 100).ToString();
  65. break;
  66. case GameState.GAME_FINISHED:
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. public void StartMenuGame()
  73. {
  74. gameState = GameState.START_MENU;
  75. startMenu.SetActive(true);
  76. endMenu.SetActive(false);
  77. }
  78. public void StartGame()
  79. {
  80. gameState = GameState.GAME_RUNNING;
  81. gameTimeRemaining = gameDuration;
  82. SetPoints(0);
  83. allowTargetSpawn = true;
  84. startMenu.SetActive(false);
  85. endMenu.SetActive(false);
  86. for (int i = 0; i < numberOfTargets; i++)
  87. {
  88. SpawnTarget();
  89. }
  90. }
  91. private void EndGame()
  92. {
  93. gameState = GameState.GAME_FINISHED;
  94. allowTargetSpawn = false;
  95. startMenu.SetActive(false);
  96. endMenu.SetActive(true);
  97. GameObject[] targets = GameObject.FindGameObjectsWithTag("Target");
  98. foreach(GameObject Target in targets)
  99. {
  100. Destroy(Target);
  101. }
  102. //Display End screen, give possibility to go back to start menu
  103. }
  104. private IEnumerator SpawnTargetDelay(float delay)
  105. {
  106. yield return new WaitForSeconds(delay);
  107. SpawnTarget();
  108. }
  109. private void SpawnTarget()
  110. {
  111. if (!allowTargetSpawn)
  112. {
  113. return;
  114. }
  115. Debug.Log("Spawning target...");
  116. //Determine target spawn position
  117. float spawnAngle = Random.Range(-targetSpawnAngle/2,targetSpawnAngle/2);
  118. float spawnDistance = Random.Range(targetSpawnMinDistance, targetSpawnMaxDistance);
  119. float spawnHeight = Random.Range(targetSpawnMinHeight, targetSpawnMaxHeight);
  120. spawnAngle = spawnAngle * Mathf.Deg2Rad;
  121. Vector3 spawnPosition = new Vector3(
  122. player.transform.position.x + Mathf.Sin(spawnAngle) * spawnDistance,
  123. player.transform.position.y + spawnHeight,
  124. player.transform.position.z + Mathf.Cos(spawnAngle) * spawnDistance
  125. );
  126. //Determine target target position
  127. float targetMovePointDistance = Random.Range(targetMovePointMinDistance, targetMovePointMaxDistance);
  128. Vector3 randomDirection = Random.onUnitSphere;
  129. Vector3 targetMovePosition = spawnPosition + randomDirection * targetMovePointDistance;
  130. Debug.Log(randomDirection);
  131. Debug.Log("Intended move distance: " + targetMovePointDistance);
  132. Debug.Log("Actual distance: " + (spawnPosition - targetMovePosition).magnitude);
  133. GameObject spawnedTarget = Instantiate(targetPrefab);
  134. spawnedTarget.transform.position = spawnPosition;
  135. Target spawnedTargetScript = spawnedTarget.GetComponent<Target>();
  136. if (spawnedTargetScript != null)
  137. {
  138. spawnedTargetScript.SetValue(targetMoveSpeed, targetWaitTime, Random.Range(targetMinPoint, targetMaxPoint+1), targetMovePosition, this);
  139. }
  140. Debug.Log("Spawn Target, distance: " + (player.transform.position - spawnedTarget.transform.position).magnitude);
  141. }
  142. private void SetPoints(int Points)
  143. {
  144. points = Points;
  145. pointsHud.text = points.ToString();
  146. }
  147. private void AddPoints(int Points)
  148. {
  149. points += Points;
  150. pointsHud.text = points.ToString();
  151. }
  152. public void NotifyTargetDestroyed(Target Target)
  153. {
  154. AddPoints(Target.points);
  155. StartCoroutine(SpawnTargetDelay(targetSpawnDelay));
  156. Destroy(Target.gameObject);
  157. }
  158. }