ZoeAppClass/Assets/Jogos/MemoryGame/Scripts/MemoryManager.cs

174 lines
4.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.SceneManagement; // ADICIONADO PARA TROCAR DE CENA
public class MemoryManager : MonoBehaviour
{
[System.Serializable]
public class MemoryClass
{
public string answer = "";
public Sprite image;
public string textInsImg = "";
public string imgCaller;
}
public static MemoryManager instance;
public GameObject memoryPrefab;
public Transform memoryOrg;
public int correctAnswers;
public Animator finishAnim;
public bool canPlay = false;
[SerializeField]
public List<MemoryClass> memories;
private List<MemoryButtonBehaviour> clickedButtons = new List<MemoryButtonBehaviour>();
private List<int> possibleImgId = new List<int>();
private List<int> possibleStrId = new List<int>();
private bool finishedGame, ended;
GamesResourcesFinder resourcesFinder;
void Start()
{
StartCoroutine(FakeStart());
}
IEnumerator FakeStart()
{
yield return new WaitForFixedUpdate();
resourcesFinder = GamesResourcesFinder.instance;
while (!resourcesFinder.gameReady)
{
yield return new WaitForEndOfFrame();
}
instance = this;
for (int i = 0; i < memories.Count; i++)
{
possibleImgId.Add(i);
possibleStrId.Add(i);
}
ended = false;
finishedGame = false;
SetupMemory();
StartCoroutine(StartTimer());
GameControl jooj = new GameControl();
jooj.ended = false;
jooj.finishedGame = false;
jooj.failedGame = false;
}
public void SetupMemory()
{
for (int i = 0; i < memories.Count; i++)
{
int rn1 = Random.Range(0, possibleImgId.Count);
Sprite thisSprite = null;
if (!string.IsNullOrEmpty(memories[possibleImgId[rn1]].imgCaller))
{
thisSprite = resourcesFinder.AskForImage(memories[possibleImgId[rn1]].imgCaller);
}
GameObject temp = Instantiate(memoryPrefab, memoryOrg, false);
temp.GetComponent<MemoryButtonBehaviour>().SetupButton(possibleImgId[rn1], thisSprite, memories[possibleImgId[rn1]].textInsImg);
temp.name = memories[rn1].answer + " Image ";
possibleImgId.RemoveAt(rn1);
int rn2 = Random.Range(0, possibleStrId.Count);
GameObject temp2 = Instantiate(memoryPrefab, memoryOrg, false);
temp2.GetComponent<MemoryButtonBehaviour>().SetupButton(possibleStrId[rn2], null, memories[possibleStrId[rn2]].answer);
temp2.name = memories[rn2].answer;
possibleStrId.RemoveAt(rn2);
}
}
public void CompareButtons(MemoryButtonBehaviour mem)
{
if (canPlay)
{
if (clickedButtons.Count < 1)
{
clickedButtons.Add(mem);
}
else if (clickedButtons.Count == 1)
{
clickedButtons.Add(mem);
if (clickedButtons[0].id == clickedButtons[1].id && clickedButtons[0].gameObject != clickedButtons[1].gameObject)
{
StartCoroutine(CorrectAnswer());
}
else
{
StartCoroutine(WrongAnswer());
}
}
else
{
mem.DoFlip();
}
}
}
IEnumerator StartTimer()
{
yield return new WaitForSecondsRealtime(11.5f);
canPlay = true;
}
IEnumerator CorrectAnswer()
{
yield return new WaitForSecondsRealtime(.75f);
DisableButtons();
correctAnswers++;
if (correctAnswers == memories.Count)
{
WinGame();
}
}
IEnumerator WrongAnswer()
{
yield return new WaitForSecondsRealtime(.75f);
clickedButtons[0].DoFlip();
clickedButtons[1].DoFlip();
clickedButtons.Clear();
}
public void DisableButtons()
{
clickedButtons[0].DoDeactivate();
clickedButtons[1].DoDeactivate();
clickedButtons.Clear();
}
public void WinGame()
{
finishAnim.SetTrigger("Success");
if (GameObject.Find("GameControl") != null)
{
GameControl jooj = GameObject.Find("GameControl").GetComponent<GameControl>();
jooj.finishedGame = true;
}
StartCoroutine(BackToMenuAfterDelay()); // CHAMA A CORROTINA PARA VOLTAR AO MENU
}
IEnumerator BackToMenuAfterDelay()
{
yield return new WaitForSeconds(2f); // Tempo de exibição da animação de sucesso
SceneManager.LoadScene("Menu"); // <-- SUBSTITUA "Menu" PELO NOME CORRETO DA SUA CENA DE MENU
}
public void GameOver()
{
canPlay = false;
finishAnim.SetTrigger("GameOver");
}
}