GameManager.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 List<GameObject> targets;
  11. public int score;
  12. public TextMeshProUGUI scoreText;
  13. public TextMeshProUGUI gameOverText;
  14. private float spawnRate = 1.0f;
  15. public Button restartButton;
  16. public bool isGameActive;
  17. public GameObject titleScreen;
  18. //for safe and load
  19. public int diff;
  20. // Start is called once before the first execution of Update after the MonoBehaviour is created
  21. void Start()
  22. {
  23. }
  24. public void StartGame(int difficulty)
  25. {
  26. isGameActive = true;
  27. StartCoroutine(SpawnTarget());
  28. score = 0;
  29. UpdateScore(0);
  30. titleScreen.gameObject.SetActive(false);
  31. spawnRate /= difficulty;
  32. diff = difficulty;
  33. }
  34. public void RestartGame()
  35. {
  36. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  37. }
  38. public void GameOver()
  39. {
  40. gameOverText.gameObject.SetActive(true);
  41. restartButton.gameObject.SetActive(true);
  42. isGameActive = false;
  43. }
  44. IEnumerator SpawnTarget()
  45. {
  46. while (isGameActive)
  47. {
  48. yield return new WaitForSeconds(spawnRate);
  49. int index = Random.Range(0, targets.Count);
  50. Instantiate(targets[index]);
  51. }
  52. }
  53. public void UpdateScore(int scoreToAdd)
  54. {
  55. score += scoreToAdd;
  56. scoreText.text = "Score: " + score;
  57. }
  58. public void SaveGame()
  59. {
  60. SaveSystem.SaveGame(this);
  61. }
  62. public void LoadGame()
  63. {
  64. GameData data = SaveSystem.LoadGame();
  65. score = data.score;
  66. scoreText.text = "Score: " + score;
  67. diff=data.difficulty;
  68. }
  69. // Update is called once per frame
  70. void Update()
  71. {
  72. if(Input.GetKeyDown(KeyCode.Q))
  73. {
  74. SaveGame();
  75. }
  76. if(Input.GetKeyDown(KeyCode.E))
  77. {
  78. LoadGame();
  79. }
  80. }
  81. }