Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SCENELOADER
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class SceneLoader : MonoBehaviour
- {
- public void LoadNextScene()
- {
- int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
- SceneManager.LoadScene(currentSceneIndex + 1);
- }
- public void LoadStartScene()
- {
- SceneManager.LoadScene(0);
- FindObjectOfType<GameSession>().ResetGame();
- }
- public void QuitGame()
- {
- Application.Quit();
- }
- //This will return the current scene build index
- public int GetSceneIndex()
- {
- return SceneManager.GetActiveScene().buildIndex;
- }
- }
- //SCORE
- using UnityEngine;
- using TMPro;
- public class Score : MonoBehaviour
- {
- [SerializeField]
- int pointsPerBlockDestroyed = 83;
- [SerializeField]
- int currentScore = 0;
- [SerializeField]
- TextMeshProUGUI scoreText;
- //Get the SceneLoader to be able to call GetSceneIndex()
- //Check SceneLoader script
- [SerializeField]
- SceneLoader sceneLoader = null;
- //Use an array to have all the different points based on the current level
- [SerializeField]
- int[] points = null;
- //To check if we changed scenes and get the correct points for the current scene
- private int checkSceneIndex = 0;
- private void Start()
- {
- //Get the SceneLoader component
- sceneLoader = sceneLoader.GetComponent<SceneLoader>();
- //Get the current scene index and assign the points
- pointsPerBlockDestroyed = points[sceneLoader.GetSceneIndex()];
- //Get the current scene index
- checkSceneIndex = sceneLoader.GetSceneIndex();
- scoreText.text = currentScore.ToString();
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.U))
- {
- AddToScore();
- }
- //Get the current scene index, if the has scene changed,
- //then checkSceneIndex is not equal to currentSceneIndex,
- //if that's the case, update the points
- int currentSceneIndex = sceneLoader.GetSceneIndex();
- if (checkSceneIndex != currentSceneIndex)
- {
- pointsPerBlockDestroyed = points[currentSceneIndex];
- checkSceneIndex = currentSceneIndex;
- }
- }
- public void AddToScore()
- {
- currentScore += pointsPerBlockDestroyed;
- scoreText.text = currentScore.ToString();
- }
- }
- //GAMESESSION
- using UnityEngine;
- public class GameSession : MonoBehaviour
- {
- // config params
- [Range(0.1f, 10f)]
- [SerializeField]
- float gameSpeed = 1f;
- [SerializeField]
- bool isAutoPlayEnabled;
- private void Awake()
- {
- int gameStatusCount = FindObjectsOfType<GameSession>().Length;
- if (gameStatusCount > 1)
- {
- gameObject.SetActive(false);
- Destroy(gameObject);
- }
- else
- {
- DontDestroyOnLoad(gameObject);
- }
- }
- void Update()
- {
- Time.timeScale = gameSpeed;
- }
- public void ResetGame()
- {
- Destroy(gameObject);
- }
- public bool IsAutoPlayEnabled()
- {
- return isAutoPlayEnabled;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement