185 lines
5.0 KiB
C#
185 lines
5.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class ShowQuiz : MonoBehaviour
|
|
{
|
|
public TextMeshProUGUI questText;
|
|
public TextMeshProUGUI[] answerTexts;
|
|
|
|
public ControlQuiz controlQuiz;
|
|
public GameObject panelFeedback;
|
|
public TextMeshProUGUI feedbackText;
|
|
public GameObject panelGameOver;
|
|
public Image panelFeedColor;
|
|
|
|
[Header("Nyawa")]
|
|
public GameObject[] heart;
|
|
public int heartCount;
|
|
|
|
public GameObject panelQuest;
|
|
public PickupItem pickupItem;
|
|
|
|
private List<int> usedQuestions = new List<int>();
|
|
private int currentQuestionIndex;
|
|
|
|
private bool isAnswerCorrect = false;
|
|
|
|
public TextMeshProUGUI textSoalBiasa;
|
|
public GameObject scrollSoalPanjang;
|
|
public TextMeshProUGUI textSoalPanjang;
|
|
|
|
AudioGame audioGame;
|
|
|
|
private void Awake()
|
|
{
|
|
audioGame = GameObject.FindGameObjectWithTag("Audio").GetComponent<AudioGame>();
|
|
}
|
|
void Start()
|
|
{
|
|
//totalPoint = 0;
|
|
heartCount = heart.Length;
|
|
UpdateNyawaUI();
|
|
//GenerateQuiz();
|
|
}
|
|
|
|
void GenerateQuiz()
|
|
{
|
|
do
|
|
{
|
|
currentQuestionIndex = Random.Range(0, controlQuiz.quests.Count);
|
|
}
|
|
while (usedQuestions.Contains(currentQuestionIndex));
|
|
|
|
usedQuestions.Add(currentQuestionIndex);
|
|
|
|
//questText.text = controlQuiz.quests[currentQuestionIndex].elementQuest.quest;
|
|
string soal = controlQuiz.quests[currentQuestionIndex].elementQuest.quest;
|
|
|
|
const int maxLengthNoScroll = 200;
|
|
bool isLong = soal.Length > maxLengthNoScroll;
|
|
|
|
if (isLong)
|
|
{
|
|
// Tampilkan scroll
|
|
scrollSoalPanjang.SetActive(true);
|
|
textSoalBiasa.gameObject.SetActive(false);
|
|
textSoalPanjang.text = soal;
|
|
}
|
|
else
|
|
{
|
|
// Tampilkan biasa
|
|
scrollSoalPanjang.SetActive(false);
|
|
textSoalBiasa.gameObject.SetActive(true);
|
|
textSoalBiasa.text = soal;
|
|
}
|
|
|
|
List<int> answerIndexes = new List<int> { 0, 1, 2, 3 };
|
|
ShuffleList(answerIndexes);
|
|
|
|
for (int i = 0; i < answerTexts.Length; i++)
|
|
{
|
|
int idx = answerIndexes[i];
|
|
answerTexts[i].text = controlQuiz.quests[currentQuestionIndex].elementQuest.answers[idx];
|
|
}
|
|
}
|
|
|
|
public void StartQuiz()
|
|
{
|
|
this.gameObject.SetActive(true);
|
|
GenerateQuiz();
|
|
}
|
|
|
|
public void ButtonAnswer()
|
|
{
|
|
TextMeshProUGUI currentAnswer = EventSystem.current.currentSelectedGameObject.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
|
|
string selectedAnswer = currentAnswer.text;
|
|
string correctAnswer = controlQuiz.quests[currentQuestionIndex].elementQuest.answers[
|
|
controlQuiz.quests[currentQuestionIndex].elementQuest.trueAnswer
|
|
];
|
|
|
|
if (selectedAnswer == correctAnswer)
|
|
{
|
|
feedbackText.text = "Benar!";
|
|
panelFeedColor.color = new Color32(12, 213, 7, 255);
|
|
//totalPoint += point;
|
|
isAnswerCorrect = true;
|
|
audioGame.PlaySFX(audioGame.correct);
|
|
}
|
|
else
|
|
{
|
|
feedbackText.text = "Salah!";
|
|
panelFeedColor.color= Color.red;
|
|
heartCount--;
|
|
UpdateNyawaUI();
|
|
isAnswerCorrect = false;
|
|
audioGame.PlaySFX(audioGame.incorrect);
|
|
|
|
if (heartCount <= 0)
|
|
{
|
|
Invoke(nameof(ShowGameOver), 1.5f);
|
|
panelFeedback.SetActive(true);
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
panelFeedback.SetActive(true);
|
|
Invoke(nameof(CloseFeedback), 1.5f);
|
|
}
|
|
|
|
void CloseFeedback()
|
|
{
|
|
panelFeedback.SetActive(false);
|
|
audioGame.PlaySFX(audioGame.popUp);
|
|
this.gameObject.SetActive(false);
|
|
|
|
if (panelQuest != null)
|
|
panelQuest.SetActive(false);
|
|
audioGame.PlaySFX(audioGame.popUp);
|
|
|
|
if (pickupItem != null)
|
|
{
|
|
pickupItem.OnQuizFinished(isAnswerCorrect);
|
|
audioGame.PlaySFX(audioGame.popUp);
|
|
}
|
|
}
|
|
|
|
void ShuffleList(List<int> list)
|
|
{
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
int temp = list[i];
|
|
int randIndex = Random.Range(i, list.Count);
|
|
list[i] = list[randIndex];
|
|
list[randIndex] = temp;
|
|
}
|
|
}
|
|
|
|
void UpdateNyawaUI()
|
|
{
|
|
for (int i = 0; i < heart.Length; i++)
|
|
{
|
|
if (i < heartCount)
|
|
heart[i].GetComponent<Image>().color = Color.red;
|
|
else
|
|
heart[i].GetComponent<Image>().color = Color.black;
|
|
}
|
|
}
|
|
|
|
void ShowGameOver()
|
|
{
|
|
panelFeedback.SetActive(false);
|
|
panelQuest.SetActive(false);
|
|
this.gameObject.SetActive(false);
|
|
|
|
if (panelGameOver != null)
|
|
panelGameOver.SetActive(true);
|
|
audioGame.PlaySFX(audioGame.gameOver);
|
|
|
|
}
|
|
}
|