Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using TMPro;
- public class MenuSystem : MonoBehaviour
- {
- [SerializeField] private GameObject _overlayEffects;
- [SerializeField] private TextMeshProUGUI _endText;
- [SerializeField] private GameObject _firstEffect;
- [SerializeField] private TextMeshProUGUI _timerDisplay;
- [SerializeField] private string _mainLevelName;
- [SerializeField] private AudioSource _audioSource;
- private bool _timerActive;
- private float _currentTime;
- private void Start()
- {
- DontDestroyOnLoad(this.gameObject);
- Invoke("RemoveFirstFade", 0.5f);
- if(PlayerPrefs.GetFloat("BestTime") != 0)
- {
- _timerDisplay.text = "Highscore " + string.Format("{0:00}:{1:00}", PlayerPrefs.GetFloat("BestTime")/60, PlayerPrefs.GetFloat("BestTime")%60);
- }
- else
- {
- _timerDisplay.text = "No Highscore";
- }
- }
- public void WinEffect()
- {
- _endText.text = "Finished!";
- _endText.color = Color.white;
- _audioSource.GetComponent<Animator>().SetTrigger("FadeOut");
- _timerActive = false;
- _overlayEffects.SetActive(true);
- if(_currentTime < PlayerPrefs.GetFloat("BestTime") || PlayerPrefs.GetFloat("BestTime") == 0)
- {
- PlayerPrefs.SetFloat("BestTime", _currentTime);
- }
- Invoke("LoadMenu", 3f);
- }
- public void LooseEffect()
- {
- _endText.text = "You lost!";
- _endText.color = Color.red;
- _audioSource.GetComponent<Animator>().SetTrigger("FadeOut");
- _timerActive = false;
- _overlayEffects.SetActive(true);
- Invoke("LoadMenu", 3f);
- }
- public void LoadMenu()
- {
- //clears curent manager
- RemoveManager();
- _audioSource.Stop();
- SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
- }
- public void LoadLevel()
- {
- _audioSource.GetComponent<Animator>().SetTrigger("FadeIn");
- _audioSource.Play();
- _currentTime = 0f;
- _timerActive = true;
- SceneManager.LoadScene(_mainLevelName, LoadSceneMode.Single);
- }
- void RemoveManager()
- {
- Destroy(this.gameObject);
- }
- void RemoveFirstFade()
- {
- Destroy(_firstEffect);
- }
- void Update()
- {
- Scene scene = SceneManager.GetActiveScene();
- if(_timerActive && scene.name == _mainLevelName)
- {
- float minutes = Mathf.FloorToInt(_currentTime / 60);
- float seconds = Mathf.FloorToInt(_currentTime % 60);
- _timerDisplay.text = string.Format("{0:00}:{1:00}", minutes, seconds); //(minutes).ToString() + ":" (minutes).ToString();
- _currentTime += Time.deltaTime;
- }
- //clear hi-scores
- if(Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.R))
- {
- PlayerPrefs.DeleteAll();
- _timerDisplay.text = "No Highscore";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement