using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class JPB_QuestionManager : MonoBehaviour { public static JPB_QuestionManager instance; [SerializeField] public JPB_QuestionCategoryBehaviour[] category; public int currentCat = 0; public int currentQuestion = 0; private int questionCount = 0; public Transform answerParent; public GameObject answerPrefab; public GameObject questionPresentation; private Image imageUi; private Text textUi; private GameObject correctPanel; private JPB_QuestionButtonBehaviour pickedQuestionButton; private string[] alphabet = new string[] { "A", "B", "C", "D" }; private void Awake() { instance = this; correctPanel = GameObject.Find("CorrectPanel"); imageUi = GetComponentsInChildren()[1]; textUi = GetComponentsInChildren()[0]; for(int i = 0; i < category.Length;i++) { questionCount += category[i].questions.Count; } } public void ReceiveQuestion(int q, int c, JPB_QuestionButtonBehaviour qBB) { pickedQuestionButton = qBB; currentCat = c; currentQuestion = q; SetupQuestion(); } public void SetupQuestion() { if (category[currentCat].questions.Count != 0) { textUi.text = category[currentCat].questions[currentQuestion].questionText; textUi.text[currentQuestion].ToString().ToUpper(); if (category[currentCat].questions[currentQuestion].questionImage != null) { imageUi.sprite = category[currentCat].questions[currentQuestion].questionImage; imageUi.color = Color.white; } else { imageUi.color = Color.clear; } PresentQuestion(); SetupAnswers(); } } public void SetupAnswers() { for (int i = 0; i < category[currentCat].questions[currentQuestion].answers.Length; i++) { GameObject temp = Instantiate(answerPrefab, answerParent, false); JPB_AnswerBehaviour tA = category[currentCat].questions[currentQuestion].answers[i]; Text[] groupText = temp.GetComponentsInChildren(); groupText[1].text = tA.answerText; groupText[0].text = alphabet[i]; temp.GetComponent().thisBtnId = i; temp.GetComponent().qBBParent = pickedQuestionButton; } } public void CheckAnswer(int id,JPB_QuestionButtonBehaviour qBtn) { if (category[currentCat].questions[currentQuestion].answers[id].isCorrect) { CorrectAnswer(); qBtn.ReturnResult(true); } else { WrongAnswer(); qBtn.ReturnResult(false); } } public void CorrectAnswer() { //TimerBehaviour.instance.StopTimer(); SoundPlayer.instance.PlaySFX(1); //int tempScore = (currentQuestion + 1) * 100 ; //JPB_TeamController.instance.AddScore(tempScore); //SetupCorrectScreen(tempScore); NextQuestion(); CleanUpAnswers(); } public void WrongAnswer() { SoundPlayer.instance.PlaySFX(0); //JPB_TeamController.instance.PassChance(); //TimerBehaviour.instance.StopTimer(); //TimerBehaviour.instance.ResetTimer(); //TimerBehaviour.instance.StartTimer(); NextQuestion(); CleanUpAnswers(); } public void NextQuestion() { questionCount--; if (questionCount == 0) { JPB_StepsManager.instance.DeclareWinner(); } else { JPB_StepsManager.instance.PickAnotherQuesiton(); } } public void PresentQuestion() { questionPresentation.GetComponentsInChildren()[0].text = category[currentCat].questions[currentQuestion].questionText; questionPresentation.GetComponentsInChildren()[1].text = category[currentCat].categoryName; } public void SetupCorrectScreen(int n) { string point; if (n == 1) { point = " Point to "; } else { point = " Points to "; } correctPanel.GetComponentsInChildren()[1].text = n + point + JPB_TeamController.instance.teams[JPB_TeamController.instance.currentlyPlayingTeam].teamName + " Team !"; } public void CleanUpAnswers() { foreach(Transform n in answerParent.gameObject.GetComponentsInChildren()) { if(n.name != "AnswersOrganizer") { Destroy(n.gameObject); } } } }