GameManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.SceneManagement;
  4. using TMPro;
  5. using UnityEditor.Overlays;
  6. using UnityEngine.UI;
  7. using UnityEngine;
  8. public class GameManager : MonoBehaviour
  9. {
  10. public TextMeshProUGUI timeRemainingText;
  11. public TextMeshProUGUI orbsCollectedText;
  12. public bool isGameActive;
  13. public GameObject GameOverScreen;
  14. public GameObject VictoryScreen;
  15. public float playerPosX;
  16. public float playerPosY;
  17. public float playerPosZ;
  18. public float playerRotX;
  19. public float playerRotY;
  20. public float playerRotZ;
  21. public int orbsCollected = 0;
  22. public float seed;
  23. public List<GameObject> orbs;
  24. public float timeRemaining = 100;
  25. public int settings;
  26. // Start is called once before the first execution of Update after the MonoBehaviour is created
  27. void Start()
  28. {
  29. isGameActive = true;
  30. for (int i = 0; i < orbs.Count; i++)
  31. {
  32. Instantiate(orbs[i], new Vector3(10*i, 5, 10*i), Quaternion.identity);
  33. }
  34. }
  35. public void RestartGame()
  36. {
  37. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  38. }
  39. public void GameOver()
  40. {
  41. GameOverScreen.SetActive(true);
  42. isGameActive = false;
  43. }
  44. public void Victory()
  45. {
  46. VictoryScreen.SetActive(true);
  47. isGameActive = false;
  48. }
  49. public void SaveGame()
  50. {
  51. SaveSystem.SaveGame(this);
  52. }
  53. public void LoadGame()
  54. {
  55. GameData data = SaveSystem.LoadGame();
  56. playerPosX = data.playerPosX;
  57. playerPosY = data.playerPosY;
  58. playerPosZ = data.playerPosZ;
  59. playerRotX = data.playerRotX;
  60. playerRotY = data.playerRotY;
  61. playerRotZ = data.playerRotZ;
  62. orbsCollected = data.orbsCollected;
  63. seed = data.seed;
  64. for (int i = 0; i < orbs.Count; i++)
  65. {
  66. }
  67. isGameActive = data.isGameActive;
  68. timeRemaining = data.timeRemaining;
  69. settings = data.settings;
  70. }
  71. // Update is called once per frame
  72. void Update()
  73. {
  74. UpdateTimeRemaining();
  75. //Victory
  76. if (orbsCollected == 20)
  77. {
  78. Victory();
  79. }
  80. //Game Over
  81. if (timeRemaining == 0)
  82. {
  83. GameOver();
  84. }
  85. if(Input.GetKeyDown(KeyCode.F5))
  86. {
  87. SaveGame();
  88. }
  89. if(Input.GetKeyDown(KeyCode.F9))
  90. {
  91. LoadGame();
  92. }
  93. }
  94. public void UpdateTimeRemaining()
  95. {
  96. timeRemaining -= Time.deltaTime;
  97. timeRemainingText.text = "RemainingTime: " + timeRemaining * Time.deltaTime;
  98. }
  99. public void UpdateCollectedOrbs()
  100. {
  101. orbsCollected++;
  102. orbsCollectedText.text = "Collected Orbs: " + orbsCollected;
  103. }
  104. }