GameManager.cs 4.2 KB

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