initial commit
This commit is contained in:
106
Assets/Scripts/QuestionController.cs
Normal file
106
Assets/Scripts/QuestionController.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using System.Collections;
|
||||
|
||||
public class QuestionController : MonoBehaviour
|
||||
{
|
||||
public static QuestionController Instance;
|
||||
|
||||
[Header("UI")]
|
||||
public GameObject questionPanel;
|
||||
public TextMeshProUGUI questionText;
|
||||
public Button yesButton;
|
||||
public Button noButton;
|
||||
|
||||
private bool correctAnswer;
|
||||
private QuestionData[] questions;
|
||||
private int currentQuestionIndex = 0;
|
||||
|
||||
public class QuestionData
|
||||
{
|
||||
public string text;
|
||||
public bool isYesCorrect;
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
if (questionPanel != null) questionPanel.SetActive(false);
|
||||
}
|
||||
|
||||
public void ShowQuestion(string question, bool isYesCorrect)
|
||||
{
|
||||
questions = new QuestionData[]
|
||||
{
|
||||
new QuestionData { text = question, isYesCorrect = isYesCorrect }
|
||||
};
|
||||
currentQuestionIndex = 0;
|
||||
ShowCurrentQuestion();
|
||||
}
|
||||
|
||||
public void ShowQuestions(QuestionData[] questionList)
|
||||
{
|
||||
questions = questionList;
|
||||
currentQuestionIndex = 0;
|
||||
ShowCurrentQuestion();
|
||||
}
|
||||
|
||||
void ShowCurrentQuestion()
|
||||
{
|
||||
if (questions == null || currentQuestionIndex >= questions.Length) return;
|
||||
|
||||
correctAnswer = questions[currentQuestionIndex].isYesCorrect;
|
||||
questionText.text = questions[currentQuestionIndex].text;
|
||||
questionPanel.SetActive(true);
|
||||
|
||||
yesButton.GetComponent<Image>().color = Color.white;
|
||||
noButton.GetComponent<Image>().color = Color.white;
|
||||
yesButton.interactable = true;
|
||||
noButton.interactable = true;
|
||||
}
|
||||
|
||||
public void OnYesPressed()
|
||||
{
|
||||
if (correctAnswer)
|
||||
OnCorrect(yesButton);
|
||||
else
|
||||
OnWrong(yesButton);
|
||||
}
|
||||
|
||||
public void OnNoPressed()
|
||||
{
|
||||
if (!correctAnswer)
|
||||
OnCorrect(noButton);
|
||||
else
|
||||
OnWrong(noButton);
|
||||
}
|
||||
|
||||
void OnCorrect(Button btn)
|
||||
{
|
||||
btn.GetComponent<Image>().color = new Color(0.3f, 1f, 0.3f);
|
||||
yesButton.interactable = false;
|
||||
noButton.interactable = false;
|
||||
StartCoroutine(NextAfterDelay());
|
||||
}
|
||||
|
||||
void OnWrong(Button btn)
|
||||
{
|
||||
btn.GetComponent<Image>().color = new Color(1f, 0.3f, 0.3f);
|
||||
}
|
||||
|
||||
IEnumerator NextAfterDelay()
|
||||
{
|
||||
yield return new WaitForSeconds(1f);
|
||||
|
||||
currentQuestionIndex++;
|
||||
|
||||
if (currentQuestionIndex < questions.Length)
|
||||
ShowCurrentQuestion();
|
||||
else
|
||||
{
|
||||
questionPanel.SetActive(false);
|
||||
GameManager.Instance.OnQuestionComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user