32bit_jam_conjure/Assets/Scripts/GameController.cs
2022-10-29 22:49:26 -04:00

91 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
private static GameController instance;
[SerializeField]
private string startScene, mainScene, endScene;
[SerializeField]
private float fadeSpeed;
[SerializeField]
private Image fadeImg;
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;
}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;
}
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));
}
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;
}
}
}
}