GameManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEditor.Overlays;
  5. using UnityEngine;
  6. using UnityEngine.Audio;
  7. using UnityEngine.EventSystems;
  8. using UnityEngine.SceneManagement;
  9. using UnityEngine.UI;
  10. public class GameManager : MonoBehaviour
  11. {
  12. public TextMeshProUGUI timeRemainingText;
  13. public TextMeshProUGUI orbsCollectedText;
  14. public GameObject player;
  15. public bool isGameActive;
  16. public GameObject GameOverScreen;
  17. public GameObject VictoryScreen;
  18. public GameObject Orb;
  19. public GameObject Enviroment;
  20. public MixerVolume Mixer;
  21. public float playerPosX;
  22. public float playerPosY;
  23. public float playerPosZ;
  24. public float playerRotX;
  25. public float playerRotY;
  26. public float playerRotZ;
  27. public int orbsCollected = 0;
  28. public float seed;
  29. public int orbsCount;
  30. public int enviromentCount;
  31. public List<GameObject> orbs;
  32. public List<GameObject> enviroments;
  33. public float timeRemaining = 120;
  34. public float Volume;
  35. // Start is called once before the first execution of Update after the MonoBehaviour is created
  36. void Start()
  37. {
  38. isGameActive = true;
  39. for (int i = 0; i < enviromentCount; i++)
  40. {
  41. enviroments.Add(Enviroment);
  42. }
  43. for (int i = 0; i < enviroments.Count; i++)
  44. {
  45. float randomX = Random.Range(-1200, 1200);
  46. float randomZ = Random.Range(-1200, 1200);
  47. Instantiate(enviroments[i], new Vector3(randomX, 100, randomZ), Quaternion.identity);
  48. }
  49. for (int i = 0; i < orbsCount; i++)
  50. {
  51. orbs.Add(Orb);
  52. }
  53. for (int i = 0; i < orbs.Count; i++)
  54. {
  55. float randomX = Random.Range(-1200, 1200);
  56. float randomZ = Random.Range(-1200, 1200);
  57. Instantiate(orbs[i], new Vector3(randomX, 100, randomZ), Quaternion.identity);
  58. }
  59. }
  60. public void RestartGame()
  61. {
  62. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  63. }
  64. public void GameOver()
  65. {
  66. GameOverScreen.SetActive(true);
  67. isGameActive = false;
  68. Time.timeScale = 0;
  69. Cursor.lockState = CursorLockMode.None;
  70. Cursor.visible = true;
  71. }
  72. public void Victory()
  73. {
  74. VictoryScreen.SetActive(true);
  75. isGameActive = false;
  76. Time.timeScale = 0;
  77. Cursor.lockState = CursorLockMode.None;
  78. Cursor.visible = true;
  79. }
  80. public void SaveGame()
  81. {
  82. SaveSystem.SaveGame(this);
  83. }
  84. public void LoadGame()
  85. {
  86. GameData data = SaveSystem.LoadGame();
  87. playerPosX = data.playerPosX;
  88. playerPosY = data.playerPosY;
  89. playerPosZ = data.playerPosZ;
  90. playerRotX = data.playerRotX;
  91. playerRotY = data.playerRotY;
  92. playerRotZ = data.playerRotZ;
  93. orbsCollected = data.orbsCollected;
  94. seed = data.seed;
  95. for (int i = 0; i < orbs.Count; i++)
  96. {
  97. orbs[i].transform.position = data.orbs[i].transform.position;
  98. }
  99. for (int i = 0; i < enviroments.Count; i++)
  100. {
  101. enviroments[i].transform.position = data.enviroments[i].transform.position;
  102. }
  103. isGameActive = data.isGameActive;
  104. timeRemaining = data.timeRemaining;
  105. Volume = data.Volume;
  106. }
  107. // Update is called once per frame
  108. void Update()
  109. {
  110. UpdateTimeRemaining();
  111. //Victory
  112. if (orbsCollected >= 20)
  113. {
  114. Victory();
  115. }
  116. //Game Over
  117. if (timeRemaining <= 0)
  118. {
  119. GameOver();
  120. }
  121. if(Input.GetKeyDown(KeyCode.F5))
  122. {
  123. SaveGame();
  124. }
  125. if(Input.GetKeyDown(KeyCode.F9))
  126. {
  127. LoadGame();
  128. }
  129. Mixer.SetMasterVolume(Volume);
  130. }
  131. public void UpdateTimeRemaining()
  132. {
  133. timeRemaining -= 1*Time.deltaTime;
  134. timeRemainingText.text = "RemainingTime: " + timeRemaining;
  135. }
  136. public void UpdateCollectedOrbs()
  137. {
  138. orbsCollected++;
  139. orbsCollectedText.text = "Collected Orbs: " + orbsCollected;
  140. }
  141. }