gandalfbialy

Untitled

Apr 12th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using UnityEngine.SceneManagement;
  7.  
  8. public class GameManager : MonoBehaviour
  9. {
  10.     public static GameManager gameManager;
  11.     [SerializeField] int timeToEnd;
  12.     bool isGamePaused = false;
  13.  
  14.     bool endGame = false;
  15.     bool isWin = false;
  16.  
  17.     public int points = 0;
  18.  
  19.     public int redKey = 0;
  20.     public int greenKey = 0;
  21.     public int goldKey = 0;
  22.  
  23.     AudioSource audioSource;
  24.  
  25.     public AudioClip resumeClip;
  26.     public AudioClip pauseClip;
  27.     public AudioClip winClip;
  28.     public AudioClip loseClip;
  29.  
  30.     public TMP_Text timeText;
  31.     public TMP_Text goldKeyText;
  32.     public TMP_Text redKeyText;
  33.     public TMP_Text greenKeyText;
  34.     public TMP_Text crystalText;
  35.     public Image snowFlake;
  36.     public GameObject infoPanel;
  37.     public TMP_Text pauseEnd;
  38.     public TMP_Text reloadInfo;
  39.     public TMP_Text useInfo;
  40.  
  41.     // Start is called before the first frame update
  42.     void Start()
  43.     {
  44.         Time.timeScale = 1f;
  45.  
  46.         if (gameManager == null)
  47.         {
  48.             gameManager = this;
  49.         }
  50.  
  51.         if (timeToEnd <= 0)
  52.         {
  53.             timeToEnd = 5;
  54.         }
  55.  
  56.         snowFlake.enabled = false;
  57.         timeText.text = timeToEnd.ToString();
  58.         infoPanel.SetActive(false);
  59.         pauseEnd.text = "Pause";
  60.         reloadInfo.text = "";
  61.         SetUseInfo("");
  62.  
  63.  
  64.         audioSource = GetComponent<AudioSource>();
  65.         InvokeRepeating("Stopper", 2, 1);
  66.     }
  67.  
  68.     public void PlayClip(AudioClip playClip)
  69.     {
  70.         audioSource.clip = playClip;
  71.         audioSource.Play();
  72.     }
  73.  
  74.     // Update is called once per frame
  75.     void Update()
  76.     {
  77.         PauseCheck();
  78.         PickUpCheck();
  79.  
  80.         if (endGame)
  81.         {
  82.             if (Input.GetKeyDown(KeyCode.Y))
  83.             {
  84.                 SceneManager.LoadScene(0);
  85.             }
  86.             if (Input.GetKeyDown(KeyCode.N))
  87.             {
  88.                 Application.Quit();
  89.             }
  90.         }
  91.     }
  92.  
  93.     void Stopper()
  94.     {
  95.         timeToEnd--;
  96.         //Debug.Log("Time:" + timeToEnd + "s");
  97.  
  98.         timeText.text = timeToEnd.ToString();
  99.         snowFlake.enabled = false;
  100.  
  101.         if (timeToEnd <= 0)
  102.         {
  103.             timeToEnd = 0;
  104.             endGame = true;
  105.         }
  106.  
  107.         if (endGame)
  108.         {
  109.             EndGame();
  110.         }
  111.     }
  112.  
  113.     public void PauseGame()
  114.     {
  115.         PlayClip(pauseClip);
  116.         infoPanel.SetActive(true);
  117.  
  118.         Debug.Log("Pause game");
  119.  
  120.         Time.timeScale = 0f;
  121.         isGamePaused = true;
  122.     }
  123.  
  124.     public void ResumeGame()
  125.     {
  126.         PlayClip(resumeClip);
  127.         infoPanel.SetActive(false);
  128.  
  129.         Debug.Log("Resume game");
  130.  
  131.         Time.timeScale = 1f;
  132.         isGamePaused = false;
  133.     }
  134.  
  135.     void PauseCheck()
  136.     {
  137.         if (Input.GetKeyDown(KeyCode.P))
  138.         {
  139.             if (isGamePaused)
  140.             {
  141.                 ResumeGame();
  142.             }
  143.             else
  144.             {
  145.                 PauseGame();
  146.             }
  147.         }
  148.     }
  149.  
  150.     public void EndGame()
  151.     {
  152.         CancelInvoke("Stopper");
  153.         infoPanel.SetActive(true);
  154.         Time.timeScale = 0f;
  155.         isGamePaused = true;
  156.  
  157.         if (isWin)
  158.         {
  159.             Debug.Log("You win!!! Reload?");
  160.             pauseEnd.text = "You Win!!!";
  161.             reloadInfo.text = "Reload? Y/N";
  162.         }
  163.         else
  164.         {
  165.             Debug.Log("You lost!!! Reload?");
  166.             pauseEnd.text = "You Lose!!!";
  167.             reloadInfo.text = "Reload? Y/N";
  168.         }
  169.     }
  170.  
  171.     public void AddPoints(int pointsToAdd)
  172.     {
  173.         points += pointsToAdd;
  174.         crystalText.text = points.ToString();
  175.     }
  176.  
  177.     public void AddTime(int value)
  178.     {
  179.         timeToEnd += value;
  180.         timeText.text = timeToEnd.ToString();
  181.     }
  182.  
  183.     public void FreezeTime(int freezeTime)
  184.     {
  185.         CancelInvoke("Stopper");
  186.         snowFlake.enabled = true;
  187.         InvokeRepeating("Stopper", freezeTime, 1);
  188.     }
  189.  
  190.     public void AddKey(KeyColor color)
  191.     {
  192.         if (color == KeyColor.Gold)
  193.         {
  194.             goldKey++;
  195.             goldKeyText.text = goldKey.ToString();
  196.         }
  197.         else if (color == KeyColor.Red)
  198.         {
  199.             redKey++;
  200.             redKeyText.text = redKey.ToString();
  201.         }
  202.         else if (color == KeyColor.Green)
  203.         {
  204.             greenKey++;
  205.             greenKeyText.text = greenKey.ToString();
  206.         }
  207.     }
  208.  
  209.     void PickUpCheck()
  210.     {
  211.         if (Input.GetKeyDown(KeyCode.L))
  212.         {
  213.             Debug.Log("Actual Time: " + timeToEnd);
  214.             Debug.Log("Key red: " + redKey + " green: " + greenKey + " gold: " + goldKey);
  215.             Debug.Log("Points: " + points);
  216.         }
  217.     }
  218.  
  219.     public void SetUseInfo(string info)
  220.     {
  221.         useInfo.text = info;
  222.     }
  223.  
  224.     public void WinGame()
  225.     {
  226.         isWin = true;
  227.         endGame = true;
  228.     }
  229. }
  230.  
Add Comment
Please, Sign In to add comment