226 lines
5.3 KiB
C#
226 lines
5.3 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
public class GameControl : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public bool finishedGame = false;
|
|||
|
|
public bool failedGame = false;
|
|||
|
|
public int noteGame = 1;
|
|||
|
|
public bool inGame = false;
|
|||
|
|
public static GameControl instance;
|
|||
|
|
public bool ended = false;
|
|||
|
|
|
|||
|
|
GameObject originalCamera, originalCanvas;
|
|||
|
|
public GameObject gameCreate;
|
|||
|
|
Camera originalMainCamera;
|
|||
|
|
|
|||
|
|
AudioSource musicPlayer;
|
|||
|
|
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
ended = false;
|
|||
|
|
instance = this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetOriginalCanvasActive()
|
|||
|
|
{
|
|||
|
|
originalCanvas.GetComponent<CanvasGroup>().interactable = true;
|
|||
|
|
originalCanvas.GetComponent<CanvasGroup>().alpha = 1;
|
|||
|
|
originalCanvas.GetComponent<CanvasGroup>().blocksRaycasts = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetOriginalCanvasInactive()
|
|||
|
|
{
|
|||
|
|
originalCanvas.GetComponent<CanvasGroup>().interactable = false;
|
|||
|
|
originalCanvas.GetComponent<CanvasGroup>().alpha = 0;
|
|||
|
|
originalCanvas.GetComponent<CanvasGroup>().blocksRaycasts = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void StartGame(string tagGame)
|
|||
|
|
{
|
|||
|
|
inGame = true;
|
|||
|
|
originalMainCamera = Camera.main;
|
|||
|
|
originalCamera = GameObject.Find("Main Camera");
|
|||
|
|
originalCanvas = GameObject.Find("Canvas");
|
|||
|
|
|
|||
|
|
Debug.Log("Inciando jogo " + "Games/" + tagGame + "/Info");
|
|||
|
|
|
|||
|
|
StartCoroutine(StartTutorial(Resources.Load<GameObject>
|
|||
|
|
("Games/" + tagGame + "/Info").GetComponent<GameInfo>(), tagGame));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void StartedGame()
|
|||
|
|
{
|
|||
|
|
ended = false;
|
|||
|
|
finishedGame = false;
|
|||
|
|
failedGame = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void FinishGame(bool failed = false)
|
|||
|
|
{
|
|||
|
|
finishedGame = true;
|
|||
|
|
failedGame = failed;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IEnumerator StartTutorial(GameInfo gameInfo, string idgames)
|
|||
|
|
{
|
|||
|
|
if (gameInfo.tutorial != null)
|
|||
|
|
{
|
|||
|
|
GameObject tutorial = Instantiate(gameInfo.tutorial);
|
|||
|
|
|
|||
|
|
yield return new WaitForSecondsRealtime(4);
|
|||
|
|
|
|||
|
|
while (!Input.GetMouseButton(0))
|
|||
|
|
{
|
|||
|
|
yield return new WaitForEndOfFrame();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Destroy(tutorial);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CreateWaitScene();
|
|||
|
|
|
|||
|
|
yield return new WaitForSecondsRealtime(.25f);
|
|||
|
|
|
|||
|
|
gameCreate = Instantiate(Resources.Load<GameObject>("Games/" + idgames + "/Game"));
|
|||
|
|
gameCreate.name = "Game";
|
|||
|
|
|
|||
|
|
DestroyWaitScene();
|
|||
|
|
|
|||
|
|
SetOriginalCanvasInactive(); originalCamera.SetActive(false);
|
|||
|
|
|
|||
|
|
string musicToPlay = gameInfo.music.ToString();
|
|||
|
|
|
|||
|
|
if (musicToPlay != "nenhuma")
|
|||
|
|
{
|
|||
|
|
AudioClip clipToPlay = Resources.Load<AudioClip>("GamesSong/" + musicToPlay);
|
|||
|
|
|
|||
|
|
StartCoroutine(StartMusic(clipToPlay));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CreateWaitScene()
|
|||
|
|
{
|
|||
|
|
GameObject obj = Instantiate(new GameObject(), originalCanvas.transform);
|
|||
|
|
Image img = obj.AddComponent<Image>();
|
|||
|
|
img.color = new Color(0, 0, 0, .75f);
|
|||
|
|
obj.transform.localScale = new Vector3(20, 20, 20);
|
|||
|
|
|
|||
|
|
obj.name = "GameLoad";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void DestroyWaitScene()
|
|||
|
|
{
|
|||
|
|
Destroy(GameObject.Find("GameLoad"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IEnumerator StartMusic(AudioClip clip)
|
|||
|
|
{
|
|||
|
|
musicPlayer = gameObject.AddComponent<AudioSource>();
|
|||
|
|
musicPlayer.loop = true;
|
|||
|
|
|
|||
|
|
musicPlayer.clip = clip;
|
|||
|
|
musicPlayer.volume = 0;
|
|||
|
|
|
|||
|
|
musicPlayer.Play();
|
|||
|
|
|
|||
|
|
while (musicPlayer.volume < .1f)
|
|||
|
|
{
|
|||
|
|
float time = Time.deltaTime * .1f;
|
|||
|
|
musicPlayer.volume += time;
|
|||
|
|
|
|||
|
|
yield return new WaitForEndOfFrame();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
musicPlayer.volume = .1f;
|
|||
|
|
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IEnumerator StopMusic()
|
|||
|
|
{
|
|||
|
|
if (musicPlayer != null)
|
|||
|
|
{
|
|||
|
|
while (musicPlayer.volume > 0)
|
|||
|
|
{
|
|||
|
|
float time = Time.deltaTime * .25f;
|
|||
|
|
musicPlayer.volume -= time;
|
|||
|
|
|
|||
|
|
yield return new WaitForEndOfFrame();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Destroy(musicPlayer);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void FixedUpdate()
|
|||
|
|
{
|
|||
|
|
if (!ended)
|
|||
|
|
{
|
|||
|
|
if (finishedGame)
|
|||
|
|
{
|
|||
|
|
StartCoroutine(endWin());
|
|||
|
|
ended = true;
|
|||
|
|
|
|||
|
|
StartCoroutine(WaitAndEndGame(true));
|
|||
|
|
}
|
|||
|
|
if (failedGame)
|
|||
|
|
{
|
|||
|
|
StartCoroutine(endLost());
|
|||
|
|
ended = true;
|
|||
|
|
|
|||
|
|
StartCoroutine(WaitAndEndGame(false));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IEnumerator WaitAndEndGame(bool state)
|
|||
|
|
{
|
|||
|
|
yield return new WaitForEndOfFrame();
|
|||
|
|
|
|||
|
|
GameObject.Find("2").GetComponent<GamePage>().EndGame(state);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IEnumerator endWin()
|
|||
|
|
{
|
|||
|
|
Destroy(gameCreate);
|
|||
|
|
|
|||
|
|
originalCamera.SetActive(true);
|
|||
|
|
SetOriginalCanvasActive();
|
|||
|
|
|
|||
|
|
GeneralScript general = GeneralScript.instance;
|
|||
|
|
general.CompleteGame(noteGame);
|
|||
|
|
general.LiberarAvanco(true);
|
|||
|
|
|
|||
|
|
inGame = false;
|
|||
|
|
|
|||
|
|
StartCoroutine(StopMusic());
|
|||
|
|
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IEnumerator endLost()
|
|||
|
|
{
|
|||
|
|
Destroy(gameCreate);
|
|||
|
|
|
|||
|
|
SetOriginalCanvasActive();
|
|||
|
|
originalCamera.SetActive(true);
|
|||
|
|
|
|||
|
|
GeneralScript general = GeneralScript.instance;
|
|||
|
|
general.CompleteGame(noteGame);
|
|||
|
|
general.LiberarAvanco(false);
|
|||
|
|
|
|||
|
|
inGame = false;
|
|||
|
|
|
|||
|
|
StartCoroutine(StopMusic());
|
|||
|
|
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|