298 lines
8.1 KiB
C#
298 lines
8.1 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class QuizPopUp : Menu<QuizPopUp>
|
|
{
|
|
[Header("Questions")]
|
|
[SerializeField] TextMeshProUGUI questionText;
|
|
[SerializeField] QuestionSO currentQuestion;
|
|
|
|
[Header("Answers")]
|
|
[SerializeField] private Sprite normalButttonSprite;
|
|
[SerializeField] private Sprite trueAnswerSprite;
|
|
[SerializeField] private Sprite falseAnswerSprite;
|
|
[SerializeField] GameObject[] answerButtons;
|
|
int correctAnswerIndex;
|
|
bool hasAnsweredEarly;
|
|
|
|
[Header("Timer")]
|
|
|
|
[SerializeField] Image timerImage;
|
|
[SerializeField] TextMeshProUGUI timerText;
|
|
public GameManager gameManager;
|
|
|
|
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>();
|
|
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();
|
|
PlayDubbingQuestion();
|
|
audioController.Instance.bgmSource.volume = .1f;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
isAnsweringQuestion = false;
|
|
hasAnsweredEarly = false;
|
|
loadNextQuestion = false;
|
|
isComplete = false;
|
|
fillFraction = 1f;
|
|
timerValue = 0f;
|
|
|
|
questionText.text = "";
|
|
|
|
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;
|
|
}
|
|
|
|
if (timerImage != null)
|
|
{
|
|
timerImage.fillAmount = 1f;
|
|
}
|
|
|
|
audioController.Instance.bgmSource.volume = 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 (timerValue < 0)
|
|
{
|
|
timerValue = 0;
|
|
}
|
|
|
|
int hours = Mathf.FloorToInt(timerValue / 3600);
|
|
int minutes = Mathf.FloorToInt((timerValue % 3600) / 60);
|
|
int seconds = Mathf.FloorToInt(timerValue % 60);
|
|
|
|
if (hours > 0)
|
|
{
|
|
timerText.text = string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
|
|
}
|
|
else
|
|
{
|
|
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
|
|
}
|
|
|
|
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)
|
|
{
|
|
questionText.text = "Jawaban Benar!";
|
|
buttonImage = answerButtons[index].GetComponent<Image>();
|
|
buttonImage.sprite = trueAnswerSprite;
|
|
isComplete = true;
|
|
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()
|
|
{
|
|
Image buttonImage;
|
|
questionText.text = currentQuestion.question;
|
|
for (int i = 0; i < answerButtons.Length; i++)
|
|
{
|
|
buttonImage = answerButtons[i].GetComponent<Image>();
|
|
buttonImage.sprite = normalButttonSprite;
|
|
TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
|
|
buttonText.text = currentQuestion.answers[i];
|
|
|
|
|
|
}
|
|
}
|
|
|
|
private async void DelayAfterAnswerTrue()
|
|
{
|
|
SaveData saveData = await Cloudsave.LoadData<SaveData>("DataPlayer");
|
|
int checkData = gameManager.indexQuestion;
|
|
|
|
if (currentQuestion.isMateriKesehatanGigi)
|
|
{
|
|
if (checkData >= 0 && checkData < saveData.isQuisKesehatanGigiComplete.Length)
|
|
{
|
|
if (!saveData.isQuisKesehatanGigiComplete[checkData])
|
|
{
|
|
saveData.isQuisKesehatanGigiComplete[checkData] = true;
|
|
saveData.kesehatanGigiStatistik = Mathf.Min(saveData.kesehatanGigiStatistik + 25f, 100f);
|
|
await Cloudsave.SaveData(saveData, "DataPlayer");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"Index {checkData} out of bounds for isMateriKesehatanGigiComplete (length: {saveData.isQuisKesehatanGigiComplete.Length})");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (checkData >= 0 && checkData < saveData.isQuisPenyakitGigiComplete.Length)
|
|
{
|
|
if (!saveData.isQuisPenyakitGigiComplete[checkData])
|
|
{
|
|
saveData.isQuisPenyakitGigiComplete[checkData] = true;
|
|
saveData.PenyakitGigiStatistik = Mathf.Min(Mathf.Ceil(saveData.PenyakitGigiStatistik + 33.33f), 100f);
|
|
await Cloudsave.SaveData(saveData, "DataPlayer");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"Index {checkData} out of bounds for isMateriPenyakitGigi1Complete (length: {saveData.isQuisPenyakitGigiComplete.Length})");
|
|
}
|
|
}
|
|
|
|
SceneController.Instance.LoadScene(currentQuestion.nextSceneName);
|
|
await Cloudsave.SaveData(saveData, "DataPlayer");
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
private void PlayDubbingQuestion()
|
|
{
|
|
audioController.Instance.StopDubbing();
|
|
audioController.Instance.PlayDubbing(currentQuestion.dubbingName);
|
|
}
|
|
}
|