166 lines
4.9 KiB
C#
166 lines
4.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
using TMPro;
|
|
|
|
public class quest : MonoBehaviour
|
|
{
|
|
public Text soalText;
|
|
public Text[] jawabanTexts;
|
|
public int[] randomJawabans;
|
|
public int[] randomSoals;
|
|
public controlquest controlQuest;
|
|
int nomorsoal;
|
|
public int gameRound; // game round mengikuti nilai array 0=1 1=2
|
|
public GameObject result, feed_benar, feed_salah;
|
|
public TMP_Text soal_T, skor_T;
|
|
public Color warnatombolbenar, warnatombolsalah;
|
|
public AudioSource wrong_audio, correct_audio, finish_audio;
|
|
|
|
public Text pointText;
|
|
public int point, soal;
|
|
public static int totalPoint;
|
|
|
|
private InputHandler inputHandler;
|
|
public bool isPretest = true;
|
|
|
|
void Awake()
|
|
{
|
|
// Pastikan InputHandler diambil lebih awal sebelum Start
|
|
inputHandler = InputHandler.instance;
|
|
|
|
if (inputHandler == null)
|
|
{
|
|
Debug.LogWarning("InputHandler.instance tidak ditemukan di scene! Pastikan sudah ada dan pakai DontDestroyOnLoad.");
|
|
}
|
|
}
|
|
|
|
|
|
void Start()
|
|
{
|
|
// Ambil InputHandler dari scene sebelumnya (yang tidak dihancurkan)
|
|
// Jika masih null (misalnya karena urutan eksekusi), coba cari lagi
|
|
if (inputHandler == null)
|
|
{
|
|
inputHandler = FindObjectOfType<InputHandler>();
|
|
if (inputHandler != null)
|
|
{
|
|
Debug.Log("InputHandler ditemukan melalui FindObjectOfType.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("InputHandler tetap tidak ditemukan! Pastikan ada di scene awal dan tidak dihancurkan.");
|
|
}
|
|
}
|
|
|
|
RandomNomorSoal();
|
|
GenerateQuest();
|
|
totalPoint = 0;
|
|
}
|
|
void Update()
|
|
{
|
|
soal_T.text = "Soal " + (nomorsoal + 1);
|
|
skor_T.text = "Skor : " + point;
|
|
}
|
|
|
|
void RandomNomorSoal()
|
|
{
|
|
for (int i = 0; i < randomSoals.Length; i++)
|
|
{
|
|
int a = randomSoals[i];
|
|
int b = Random.Range(0, randomSoals.Length);
|
|
randomSoals[i] = randomSoals[b];
|
|
randomSoals[b] = a;
|
|
}
|
|
}
|
|
|
|
void RandomNomorJawaban()
|
|
{
|
|
for (int i = 0; i < randomJawabans.Length; i++)
|
|
{
|
|
int a = randomJawabans[i];
|
|
int b = Random.Range(0, randomJawabans.Length);
|
|
randomJawabans[i] = randomJawabans[b];
|
|
randomJawabans[b] = a;
|
|
}
|
|
}
|
|
|
|
void GenerateQuest()
|
|
{
|
|
RandomNomorJawaban();
|
|
soalText.text = controlQuest.soals[randomSoals[nomorsoal]].elementSoal.soal;
|
|
|
|
for (int i = 0; i < jawabanTexts.Length; i++)
|
|
{
|
|
jawabanTexts[i].text = controlQuest.soals[randomSoals[nomorsoal]].elementSoal.jawaban[randomJawabans[i]];
|
|
}
|
|
}
|
|
|
|
public void ButtonJawabanSoal()
|
|
{
|
|
Text currentJawaban = EventSystem.current.currentSelectedGameObject.transform.GetChild(0).GetComponent<Text>();
|
|
//Button jawaban = EventSystem.current.currentSelectedGameObject.transform.parent.GetComponent<Button>();
|
|
|
|
|
|
if (currentJawaban.text == controlQuest.soals[randomSoals[nomorsoal]].elementSoal.jawaban[controlQuest.soals[randomSoals[nomorsoal]].elementSoal.jawabanBenar])
|
|
{
|
|
StartCoroutine(ShowFeedback(feed_benar));
|
|
Debug.Log("benar");
|
|
correct_audio.Play();
|
|
point += 10;
|
|
totalPoint = point;
|
|
//tombol.GetComponent<Image>().color = warnatombolbenar;
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(ShowFeedback(feed_salah));
|
|
Debug.Log("salah");
|
|
wrong_audio.Play();
|
|
//tombol.GetComponent<Image>().color = warnatombolsalah;
|
|
}
|
|
//nomorsoal++;
|
|
//soal++;
|
|
|
|
if (nomorsoal == gameRound - 1)
|
|
{
|
|
timertest.isStop = true;
|
|
result.SetActive(true);
|
|
finish_audio.Play();
|
|
pointText.text = totalPoint.ToString();
|
|
|
|
// Simpan skor ke InputHandler
|
|
if (InputHandler.instance != null)
|
|
{
|
|
if (isPretest)
|
|
{
|
|
InputHandler.instance.SetSkorPretest(totalPoint);
|
|
InputHandler.instance.AddNameToList();
|
|
}
|
|
else
|
|
{
|
|
InputHandler.instance.SetSkorPosttest(totalPoint);
|
|
InputHandler.instance.AddNameToList();
|
|
}
|
|
Debug.Log($"Skor {(isPretest ? "Pretest" : "Posttest")} disimpan: {totalPoint}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("InputHandler.instance tidak ditemukan!");
|
|
}
|
|
} else
|
|
{
|
|
nomorsoal++;
|
|
GenerateQuest();
|
|
}
|
|
}
|
|
|
|
IEnumerator ShowFeedback(GameObject feedback)
|
|
{
|
|
feedback.SetActive(true);
|
|
yield return new WaitForSeconds(1f);
|
|
feedback.SetActive(false);
|
|
}
|
|
}
|