Advertisement
drakon-firestone

Untitled

Jun 5th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class GameManager : MonoBehaviour
  8. {
  9. public static GameManager Instance;
  10.  
  11. [SerializeField] private bool gameStarted = false;
  12. public bool GameStarted() => gameStarted;
  13.  
  14. [SerializeField] private Snake[] snakes;
  15.  
  16. [SerializeField] private Food food;
  17.  
  18. private void Awake()
  19. {
  20. if (Instance == null)
  21. Instance = this;
  22. else
  23. DestroyImmediate(this);
  24. }
  25.  
  26. private void Start()
  27. {
  28. ResetGame();
  29. }
  30.  
  31. private void StartGame()
  32. {
  33. gameStarted = true;
  34. }
  35.  
  36.  
  37. private void LateUpdate()
  38. {
  39. CheckForWin();
  40. }
  41.  
  42. private void CheckForWin()
  43. {
  44. bool gameEnded = false;
  45. foreach (Snake snake in snakes)
  46. {
  47. if (!snake.isAlive())
  48. {
  49. gameEnded = true;
  50. Debug.Log($"{snake.name} died.");
  51. }
  52. }
  53.  
  54. if (gameEnded)
  55. ResetGame();
  56. }
  57.  
  58. private void ResetGame()
  59. {
  60. gameStarted = false;
  61. foreach (Snake snake in snakes)
  62. {
  63. snake.ResetState();
  64. }
  65.  
  66. food.ResetState();
  67. Invoke(nameof(StartGame), 3);
  68. }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement