ZoeAppClass/Assets/Jogos/00 - Models/HangMan/GameHangManModel.cs

259 lines
6.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameHangManModel : MonoBehaviour
{
[System.Serializable]
public class Question
{
public string imgShow;
public string wordCorrect, subtitle;
public enum myEnum // your custom enumeration
{
Poucas,
Media,
Preencher
};
public myEnum letrasSobressalentes = myEnum.Preencher;
}
public List<Question> questoes;
public Color cameraColor;
int actualQuestion;
public Transform allignRight, allignQuest;
public GameObject modelRight, modelLetter;
List<GameHangManRight> listRight = new List<GameHangManRight>();
// Start is called before the first frame update
void Awake()
{
GameObject.Find("CameraHangMan").GetComponent<Camera>().backgroundColor = cameraColor;
List<GamesResourcesFinder.ArchiveDownload> downloads = new List<GamesResourcesFinder.ArchiveDownload>();
foreach (var item in questoes)
{
GamesResourcesFinder.ArchiveDownload download = new GamesResourcesFinder.ArchiveDownload();
download.resource = item.imgShow;
download.isImg = true;
downloads.Add(download);
}
GetComponent<GamesResourcesFinder>().AddObjNDownload(downloads);
SetQuestion();
GameControl.instance.StartedGame();
}
public void SetQuestion()
{
StartCoroutine(SetQuestionCor());
}
public void CreateSeparator(int siblingIndex)
{
GameObject objCreate = Instantiate(new GameObject(), allignQuest);
RectTransform rect = objCreate.AddComponent<RectTransform>();
rect.sizeDelta = new Vector2(75, 75);
objCreate.transform.SetSiblingIndex(siblingIndex);
}
IEnumerator SetQuestionCor()
{
int skipFrame = 0;
StartCoroutine(WaitForSprite());
while (allignRight.childCount > 0)
{
Destroy(allignRight.GetChild(0).gameObject);
skipFrame++;
if (skipFrame >= 10)
{
yield return new WaitForEndOfFrame();
skipFrame = 0;
}
}
while (allignQuest.childCount > 0)
{
Destroy(allignQuest.GetChild(0).gameObject);
skipFrame++;
if (skipFrame >= 10)
{
yield return new WaitForEndOfFrame();
skipFrame = 0;
}
}
listRight.Clear();
if (questoes[actualQuestion].subtitle != null && questoes[actualQuestion].subtitle != "")
{
GameObject.Find("SubtitleText").GetComponent<Text>().text = questoes[actualQuestion].subtitle;
}
List<char> charactersRight = new List<char>();
charactersRight.AddRange(questoes[actualQuestion].wordCorrect.ToCharArray());
List<Transform> objsRight = new List<Transform>();
List<char> alphaRoberto = new List<char>() { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'i', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z' };
foreach (char character in charactersRight)
{
if (char.IsSeparator(character))
{
GameObject objCreate = Instantiate(new GameObject(), allignRight);
RectTransform rect = objCreate.AddComponent<RectTransform>();
rect.sizeDelta = new Vector2(75, 75);
}
else
{
listRight.Add(Instantiate(modelRight, allignRight).GetComponent<GameHangManRight>());
listRight[listRight.Count - 1].SetStart(character);
GameObject obj = Instantiate(modelLetter, allignQuest);
obj.GetComponent<GameHangManLetter>().SetLetter(character);
objsRight.Add(obj.transform);
}
skipFrame++;
if (skipFrame >= 10)
{
yield return new WaitForEndOfFrame();
skipFrame = 0;
}
}
int valueLetters = 24;
switch (questoes[actualQuestion].letrasSobressalentes)
{
case Question.myEnum.Poucas:
valueLetters = 12;
break;
case Question.myEnum.Media:
valueLetters = 20;
break;
case Question.myEnum.Preencher:
valueLetters = 28;
break;
default:
break;
}
while (allignQuest.childCount < valueLetters)
{
GameObject obj = Instantiate(modelLetter, allignQuest);
obj.GetComponent<GameHangManLetter>().SetLetter(alphaRoberto[Random.Range(0, alphaRoberto.Count)]);
}
foreach (Transform item in objsRight)
{
item.SetSiblingIndex(Random.Range(0, 20));
}
}
IEnumerator WaitForSprite()
{
int thisIndex = actualQuestion;
GamesResourcesFinder finder = GetComponent<GamesResourcesFinder>();
Sprite thisSpr = null;
while (thisSpr == null)
{
if (actualQuestion != thisIndex)
{
yield break;
}
thisSpr = finder.AskForImage(questoes[actualQuestion].imgShow);
yield return new WaitForEndOfFrame();
}
if (actualQuestion != thisIndex)
{
yield break;
}
GameObject.Find("ImgShow").GetComponent<Image>().sprite = thisSpr;
}
public bool ChooseLetter(char charToComplete)
{
foreach (var item in listRight)
{
if (item.CheckChar(charToComplete))
{
CheckIfWin();
return true;
}
}
return false;
}
public void CheckIfWin()
{
bool won = true;
foreach (var item in listRight)
{
if (!item.isRight)
{
won = false;
}
}
if (won)
{
StartCoroutine(WinAnime());
}
}
public void Won()
{
actualQuestion++;
if (actualQuestion == questoes.Count)
{
GameControl.instance.FinishGame();
}
else
{
SetQuestion();
}
}
IEnumerator WinAnime()
{
List<string> messagesWinner = new List<string>() { "You rock!", "You nailed it", "You did great", "That was awesome", ";)", "Just Perfect", "You're the best",
"Marvelous", "You're a Pro!", "Genius", "Could it BE more perfect?"};
GameObject.Find("TxtInfo").GetComponent<Text>().text = messagesWinner[Random.Range(0, messagesWinner.Count)];
GameObject.Find("Canvas - HangMan").GetComponent<Animator>().SetTrigger("won");
yield return new WaitForEndOfFrame();
yield return new WaitForSecondsRealtime(1);
Won();
yield break;
}
}