147 lines
3.6 KiB
C#
147 lines
3.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class PopUpQuestion : Menu<PopUpQuestion>
|
|
{
|
|
public QuestionSO question;
|
|
[SerializeField] private TextMeshProUGUI questionText;
|
|
[SerializeField] private TextMeshProUGUI[] answers;
|
|
|
|
[SerializeField] private Button[] answerButtons;
|
|
[SerializeField] DataManager dataManager;
|
|
[SerializeField] private GameManager gameManager;
|
|
|
|
private bool hasAnswerEarly;
|
|
|
|
[Header("Timer")]
|
|
[SerializeField] Image timerImage;
|
|
Timer timer;
|
|
public bool isComplete = false;
|
|
|
|
private Image defaultButtonImage;
|
|
|
|
private void OnEnable()
|
|
{
|
|
timer = GetComponent<Timer>();
|
|
dataManager = DataManager.instance;
|
|
gameManager = FindObjectOfType<GameManager>();
|
|
if (gameManager != null)
|
|
{
|
|
question = gameManager.currentQuestion;
|
|
questionText.text = question.question;
|
|
ResetQuestionUI();
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
answers[i].text = question.answers[i];
|
|
SetButtonState(true);
|
|
}
|
|
SetDefaultButtonSprites();
|
|
}
|
|
}
|
|
|
|
void ResetQuestionUI()
|
|
{
|
|
hasAnswerEarly = false;
|
|
isComplete = false;
|
|
|
|
if (question != null)
|
|
questionText.text = question.question;
|
|
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
answers[i].text = question.answers[i];
|
|
|
|
Image buttonImage = answerButtons[i].GetComponent<Image>();
|
|
buttonImage.color = Color.white;
|
|
|
|
answerButtons[i].interactable = true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void SetButtonState(bool state)
|
|
{
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
answerButtons[i].interactable = state;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
StateQuestion();
|
|
}
|
|
|
|
void StateQuestion()
|
|
{
|
|
|
|
}
|
|
|
|
void SetDefaultButtonSprites()
|
|
{
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
Image buttonImage = answerButtons[i].gameObject.GetComponent<Image>();
|
|
defaultButtonImage = buttonImage;
|
|
}
|
|
}
|
|
|
|
public void OnAnswerSelected(int index)
|
|
{
|
|
hasAnswerEarly = true;
|
|
DisplayAnswer(index);
|
|
SetButtonState(false);
|
|
}
|
|
|
|
void DisplayAnswer(int index)
|
|
{
|
|
if (index == question.answerIndex)
|
|
{
|
|
// audio benar
|
|
questionText.text = "Kamu Benar !";
|
|
Image buttonImage = answerButtons[index].gameObject.GetComponent<Image>();
|
|
buttonImage.color = Color.green;
|
|
isComplete = true;
|
|
StartCoroutine(DelayAfterAnswerTrue());
|
|
}
|
|
else
|
|
{
|
|
// audio salah
|
|
questionText.text = "Jawaban Salah !";
|
|
Image buttonImage = answerButtons[index].gameObject.GetComponent<Image>();
|
|
buttonImage.color = Color.red;
|
|
StartCoroutine(DelayAfterAnswerFalse());
|
|
}
|
|
}
|
|
|
|
IEnumerator DelayAfterAnswerTrue()
|
|
{
|
|
if (question.isMateriKesehatanGigi)
|
|
{
|
|
dataManager.MateriKesehatanGigi += 20;
|
|
}
|
|
else
|
|
{
|
|
dataManager.MateriPenyakitGigi += 25;
|
|
}
|
|
yield return new WaitForSeconds(1f);
|
|
SceneController.Instance.LoadScene(question.nextSceneName);
|
|
dataManager.Save();
|
|
GameMenu.Open();
|
|
}
|
|
|
|
IEnumerator DelayAfterAnswerFalse()
|
|
{
|
|
yield return new WaitForSeconds(.5f);
|
|
SceneController.Instance.RestartScene();
|
|
GameMenu.Open();
|
|
}
|
|
|
|
|
|
}
|