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 questoes; public Color cameraColor; int actualQuestion; public Transform allignRight, allignQuest; public GameObject modelRight, modelLetter; List listRight = new List(); // Start is called before the first frame update void Awake() { GameObject.Find("CameraHangMan").GetComponent().backgroundColor = cameraColor; List downloads = new List(); foreach (var item in questoes) { GamesResourcesFinder.ArchiveDownload download = new GamesResourcesFinder.ArchiveDownload(); download.resource = item.imgShow; download.isImg = true; downloads.Add(download); } GetComponent().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(); 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 = questoes[actualQuestion].subtitle; } List charactersRight = new List(); charactersRight.AddRange(questoes[actualQuestion].wordCorrect.ToCharArray()); List objsRight = new List(); List alphaRoberto = new List() { '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(); rect.sizeDelta = new Vector2(75, 75); } else { listRight.Add(Instantiate(modelRight, allignRight).GetComponent()); listRight[listRight.Count - 1].SetStart(character); GameObject obj = Instantiate(modelLetter, allignQuest); obj.GetComponent().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().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(); 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().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 messagesWinner = new List() { "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 = messagesWinner[Random.Range(0, messagesWinner.Count)]; GameObject.Find("Canvas - HangMan").GetComponent().SetTrigger("won"); yield return new WaitForEndOfFrame(); yield return new WaitForSecondsRealtime(1); Won(); yield break; } }