123 lines
4.1 KiB
C#
123 lines
4.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
public class GameController : MonoBehaviour
|
|
{
|
|
private static GameController instance;
|
|
[SerializeField]
|
|
private string startScene, mainScene, endScene;
|
|
[SerializeField]
|
|
private AudioClip startMusic, mainMusic, endMusic;
|
|
[SerializeField]
|
|
private float fadeSpeed;
|
|
[SerializeField]
|
|
private Image fadeImg;
|
|
[SerializeField]
|
|
private TMP_Text coinText;
|
|
private AudioSource audioSource;
|
|
private Coroutine fadeCoroutine;
|
|
private int coinAmount = 0;
|
|
public int CoinAmount{get=>coinAmount;}
|
|
public static GameController Instance{
|
|
get{
|
|
if(instance is null)Debug.LogError("Game controller is null");
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private void Awake() {
|
|
if(GameController.Instance is null){
|
|
instance = this;
|
|
DontDestroyOnLoad(this.gameObject);
|
|
SceneManager.activeSceneChanged += ChangedScene;
|
|
coinText.gameObject.SetActive(false);
|
|
// coinText.text = "0";
|
|
// if(!SceneManager.GetActiveScene().name.Equals(mainScene) && !SceneManager.GetActiveScene().name.Equals(endScene)){
|
|
// coinText.gameObject.SetActive(false);
|
|
// }else{
|
|
// coinText.gameObject.SetActive(true);
|
|
// }
|
|
audioSource = GetComponent<AudioSource>();
|
|
}else{
|
|
Destroy(this.gameObject);
|
|
}
|
|
|
|
}
|
|
|
|
public void AddCoins(int amount){
|
|
//This should not be like this. Collectible should call the player that hit it and add to their amount
|
|
coinAmount += amount;
|
|
//coinText.text = coinAmount.ToString();
|
|
}
|
|
|
|
|
|
public void StartGame(){
|
|
fadeCoroutine = StartCoroutine(FadeToBlack(mainScene, true,fadeSpeed));
|
|
}
|
|
|
|
public void EndGame(){
|
|
Debug.Log("Game is over");
|
|
fadeCoroutine = StartCoroutine(FadeToBlack(endScene, true,fadeSpeed));
|
|
}
|
|
|
|
public void QuitGame(){
|
|
//fadeCoroutine = StartCoroutine(FadeToBlack(true,fadeTimer));
|
|
Application.Quit();
|
|
}
|
|
|
|
public void Options(){
|
|
//fadeCoroutine = StartCoroutine(FadeToBlack(true,fadeTimer));
|
|
Debug.Log("Clicked options");
|
|
}
|
|
|
|
public void MainMenu(){
|
|
fadeCoroutine = StartCoroutine(FadeToBlack(startScene, true,fadeSpeed));
|
|
}
|
|
|
|
private void ChangedScene(Scene curr, Scene next){
|
|
fadeCoroutine = StartCoroutine(FadeToBlack(string.Empty, false, fadeSpeed));
|
|
// if(!next.name.Equals(mainScene) && !next.name.Equals(endScene)){
|
|
// coinText.gameObject.SetActive(false);
|
|
// }else{
|
|
// coinText.gameObject.SetActive(true);
|
|
// }
|
|
}
|
|
|
|
public IEnumerator FadeToBlack(string sceneToLoad,bool fadeToBlack = true, float fadeSpeed = 2.5f){
|
|
Color imgColor = fadeImg.color;
|
|
float fadeAmount;
|
|
if(fadeToBlack){
|
|
while(fadeImg.color.a < 1){
|
|
fadeAmount = imgColor.a + (fadeSpeed * Time.deltaTime);
|
|
imgColor = new Color(imgColor.r, imgColor.g, imgColor.b, fadeAmount);
|
|
fadeImg.color = imgColor;
|
|
yield return null;
|
|
}
|
|
SceneManager.LoadScene(sceneToLoad);
|
|
}else{
|
|
while(fadeImg.color.a > 0){
|
|
fadeAmount = imgColor.a - (fadeSpeed * Time.deltaTime);
|
|
imgColor = new Color(imgColor.r, imgColor.g, imgColor.b, fadeAmount);
|
|
fadeImg.color = imgColor;
|
|
yield return null;
|
|
}
|
|
string active = SceneManager.GetActiveScene().name;
|
|
if(active.Equals(startScene)){
|
|
audioSource.clip = startMusic;
|
|
audioSource.Play();
|
|
}else if(active.Equals(mainScene)){
|
|
audioSource.clip = mainMusic;
|
|
audioSource.Play();
|
|
}else if(active.Equals(endScene)){
|
|
audioSource.clip = endMusic;
|
|
audioSource.Play();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|