GameManager.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using TMPro;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. public class GameManager : MonoBehaviour
  5. {
  6. public enum GameState
  7. {
  8. InMainMenu,
  9. Paused,
  10. Playing,
  11. GameOver,
  12. Won
  13. }
  14. public static GameManager gameManagerInstance { get; private set; }
  15. [SerializeField] int currentOrbCounter = 0;
  16. [SerializeField] int maximumOrbs = 10;
  17. [SerializeField] TextMeshProUGUI orbCounterText;
  18. public GameState gameState = GameState.InMainMenu;
  19. private void Awake()
  20. {
  21. if(gameManagerInstance == null)
  22. {
  23. gameManagerInstance = this;
  24. DontDestroyOnLoad(gameObject);
  25. }
  26. else
  27. {
  28. Destroy(gameObject);
  29. return;
  30. }
  31. }
  32. private void Update()
  33. {
  34. switch (gameState)
  35. {
  36. case GameState.InMainMenu: break;
  37. case GameState.Playing:
  38. if(orbCounterText == null)
  39. {
  40. orbCounterText = GameObject.Find("Orb_Text").GetComponent<TextMeshProUGUI>();
  41. }
  42. break;
  43. case GameState.GameOver:
  44. SceneManager.LoadScene("GameOverScene");
  45. break;
  46. default:
  47. Debug.Log("GameState error");
  48. break;
  49. }
  50. }
  51. public void IncreaseOrbCounter()
  52. {
  53. currentOrbCounter++;
  54. string orbCounterString = currentOrbCounter.ToString() + " / " + maximumOrbs.ToString();
  55. orbCounterText.text = orbCounterString;
  56. checkAllOrbsCollected(currentOrbCounter);
  57. }
  58. private void checkAllOrbsCollected(int currentOrbCounter)
  59. {
  60. if(currentOrbCounter == maximumOrbs)
  61. {
  62. gameState = GameState.Won;
  63. Debug.Log("Player won! All orbs were collected!");
  64. SceneManager.LoadScene("VictoryScene");
  65. }
  66. }
  67. public void StartGame()
  68. {
  69. SceneManager.LoadScene("Sandbox");
  70. gameState = GameState.Playing;
  71. }
  72. }