262 lines
6.7 KiB
C#
262 lines
6.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
using UnityEngine.UI;
|
|
|
|
public class QuizPopUp : Menu<QuizPopUp>
|
|
{
|
|
[Header("Questions")]
|
|
[SerializeField] TextMeshProUGUI questionText;
|
|
[SerializeField] QuestionSO currentQuestion;
|
|
|
|
[Header("Answers")]
|
|
[SerializeField] private Sprite trueAnswerSprite;
|
|
[SerializeField] private Sprite falseAnswerSprite;
|
|
[SerializeField] GameObject[] answerButtons;
|
|
int correctAnswerIndex;
|
|
bool hasAnsweredEarly;
|
|
|
|
[Header("Timer")]
|
|
[SerializeField] Image timerImage;
|
|
|
|
public GameManager gameManager;
|
|
public DataManager dataManager;
|
|
|
|
public bool isComplete = false;
|
|
|
|
bool isGameOver = false;
|
|
|
|
//========================
|
|
[SerializeField] float timeToCompleteQuestion = 30f;
|
|
[SerializeField] float timeToShowCorrectAnswer = 10f;
|
|
|
|
public bool loadNextQuestion;
|
|
public float fillFraction;
|
|
public bool isAnsweringQuestion;
|
|
|
|
float timerValue;
|
|
|
|
private void OnEnable()
|
|
{
|
|
gameManager = FindObjectOfType<GameManager>();
|
|
dataManager = FindObjectOfType<DataManager>();
|
|
|
|
if (gameManager == null) return;
|
|
|
|
currentQuestion = gameManager.currentQuestion;
|
|
correctAnswerIndex = currentQuestion.answerIndex;
|
|
|
|
timerValue = timeToCompleteQuestion;
|
|
isAnsweringQuestion = true;
|
|
fillFraction = 1f;
|
|
loadNextQuestion = false;
|
|
hasAnsweredEarly = false;
|
|
isComplete = false;
|
|
|
|
SetButtonState(true);
|
|
|
|
DisplayQuestion();
|
|
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
// Reset question and timer state
|
|
isAnsweringQuestion = false;
|
|
hasAnsweredEarly = false;
|
|
loadNextQuestion = false;
|
|
isComplete = false;
|
|
fillFraction = 1f;
|
|
timerValue = 0f;
|
|
|
|
// Reset question text
|
|
questionText.text = "";
|
|
|
|
// Reset all button colors and enable them
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
Button button = answerButtons[i].GetComponent<Button>();
|
|
button.interactable = true;
|
|
|
|
Image buttonImage = answerButtons[i].GetComponent<Image>();
|
|
buttonImage.color = Color.white; // ganti ke warna default
|
|
}
|
|
|
|
// Reset timer UI
|
|
if (timerImage != null)
|
|
{
|
|
timerImage.fillAmount = 1f;
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (gameManager == null)
|
|
{
|
|
return;
|
|
}
|
|
StateLoadQuestion();
|
|
UpdateTimer();
|
|
if(isComplete)
|
|
{
|
|
Debug.Log("Game Over");
|
|
isComplete = false;
|
|
}
|
|
}
|
|
|
|
public void CancelTimer()
|
|
{
|
|
timerValue = 0;
|
|
}
|
|
|
|
public void UpdateTimer()
|
|
{
|
|
timerValue -= Time.deltaTime;
|
|
|
|
if (isAnsweringQuestion)
|
|
{
|
|
if (timerValue > 0)
|
|
{
|
|
fillFraction = timerValue / timeToCompleteQuestion;
|
|
}
|
|
else
|
|
{
|
|
timerValue = timeToShowCorrectAnswer;
|
|
isAnsweringQuestion = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (timerValue > 0)
|
|
{
|
|
fillFraction = timerValue / timeToShowCorrectAnswer;
|
|
}
|
|
else
|
|
{
|
|
timerValue = timeToCompleteQuestion;
|
|
isAnsweringQuestion = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void StateLoadQuestion()
|
|
{
|
|
timerImage.fillAmount = fillFraction;
|
|
if (loadNextQuestion)
|
|
{
|
|
GetNextQuestion();
|
|
hasAnsweredEarly = false;
|
|
loadNextQuestion = false;
|
|
}
|
|
else if (!hasAnsweredEarly && !isAnsweringQuestion)
|
|
{
|
|
SetButtonState(false);
|
|
Debug.Log("Game Over");
|
|
this.enabled = false;
|
|
questionText.text = "Waktu Habis";
|
|
StartCoroutine(DelayAfterAnswerFalse());
|
|
|
|
}
|
|
}
|
|
|
|
public void OnAnswerSelected(int index)
|
|
{
|
|
hasAnsweredEarly = true;
|
|
DisplayAnswer(index);
|
|
SetButtonState(false);
|
|
CancelTimer();
|
|
}
|
|
|
|
void DisplayAnswer(int index)
|
|
{
|
|
Image buttonImage;
|
|
|
|
if (index == currentQuestion.answerIndex)
|
|
{
|
|
//implement sound
|
|
questionText.text = "Jawaban Benar!";
|
|
buttonImage = answerButtons[index].GetComponent<Image>();
|
|
buttonImage.sprite = trueAnswerSprite;
|
|
// implement score ++;
|
|
isComplete = true;
|
|
StartCoroutine(DelayAfterAnswerTrue());
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
//implement sound
|
|
questionText.text = "Jawaban Salah !";
|
|
buttonImage = answerButtons[index].GetComponent<Image>();
|
|
buttonImage.sprite = falseAnswerSprite;
|
|
StartCoroutine(DelayAfterAnswerFalse());
|
|
|
|
}
|
|
}
|
|
|
|
void GetNextQuestion()
|
|
{
|
|
Debug.Log("Win Question");
|
|
}
|
|
|
|
void DisplayQuestion()
|
|
{
|
|
questionText.text = currentQuestion.question;
|
|
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
|
|
buttonText.text = currentQuestion.answers[i];
|
|
}
|
|
}
|
|
|
|
IEnumerator DelayAfterAnswerTrue()
|
|
{
|
|
dataManager.Load();
|
|
if (currentQuestion.isMateriKesehatanGigi)
|
|
{
|
|
int checkData = gameManager.indexQuestion;
|
|
if (dataManager.isMateriKesehatanGigiComplete[checkData] == false)
|
|
{
|
|
dataManager.isMateriKesehatanGigiComplete[checkData] = true;
|
|
dataManager.MateriKesehatanGigi += 20;
|
|
dataManager.Save();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int checkData = gameManager.indexQuestion;
|
|
if (dataManager.isMateriPenyakitGigi1Complete[checkData] == false)
|
|
{
|
|
dataManager.isMateriPenyakitGigi1Complete[checkData] = true;
|
|
dataManager.MateriPenyakitGigi += 25;
|
|
dataManager.Save();
|
|
}
|
|
}
|
|
yield return new WaitForSeconds(1);
|
|
SceneController.Instance.LoadScene(currentQuestion.nextSceneName);
|
|
dataManager.Save();
|
|
GameMenu.Open();
|
|
}
|
|
|
|
IEnumerator DelayAfterAnswerFalse()
|
|
{
|
|
yield return new WaitForSeconds(1f);
|
|
SceneController.Instance.RestartScene();
|
|
enabled = true;
|
|
GameMenu.Open();
|
|
}
|
|
|
|
void SetButtonState(bool state)
|
|
{
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
Button button = answerButtons[i].GetComponent<Button>();
|
|
button.interactable = state;
|
|
}
|
|
}
|
|
}
|