116 lines
2.4 KiB
C#
116 lines
2.4 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SoalManager : MonoBehaviour
|
|
{
|
|
public Text scoreText; // Komponen Text untuk menampilkan skor
|
|
public Button resetButton; // Tombol untuk mereset skor
|
|
public AudioSource correctSound; // Suara untuk jawaban benar
|
|
public AudioSource wrongSound; // Suara untuk jawaban salah
|
|
public GameObject[] panels; // Array panel pertanyaan
|
|
|
|
private int score = 0;
|
|
private int currentPanelIndex = 0;
|
|
private bool hasAnswered = false;
|
|
private const int maxPanels = 11;
|
|
|
|
void Start()
|
|
{
|
|
scoreText.text = "0";
|
|
|
|
if (resetButton != null)
|
|
{
|
|
resetButton.onClick.AddListener(ResetScore);
|
|
}
|
|
|
|
|
|
|
|
ShowPanel(0);
|
|
}
|
|
|
|
public void CheckBoxChanged(bool isCorrect)
|
|
{
|
|
if (hasAnswered) return; // Cegah jawaban ganda
|
|
hasAnswered = true;
|
|
|
|
StartCoroutine(HandleAnswer(isCorrect));
|
|
}
|
|
|
|
private IEnumerator HandleAnswer(bool isCorrect)
|
|
{
|
|
if (isCorrect)
|
|
{
|
|
PlayCorrectFeedback();
|
|
score += 10;
|
|
scoreText.text = score.ToString();
|
|
}
|
|
else
|
|
{
|
|
PlayWrongFeedback();
|
|
}
|
|
|
|
// Tunggu sampai suara selesai sebelum pindah soal
|
|
yield return new WaitForSeconds(correctSound.clip.length);
|
|
|
|
NextPanel();
|
|
}
|
|
|
|
|
|
public void ResetScore()
|
|
{
|
|
score = 0;
|
|
scoreText.text = "0";
|
|
hasAnswered = false;
|
|
currentPanelIndex = 0;
|
|
ShowPanel(0);
|
|
}
|
|
|
|
private void PlayCorrectFeedback()
|
|
{
|
|
if (correctSound != null && correctSound.clip != null)
|
|
{
|
|
correctSound.PlayOneShot(correctSound.clip);
|
|
}
|
|
}
|
|
|
|
private void PlayWrongFeedback()
|
|
{
|
|
if (wrongSound != null && wrongSound.clip != null)
|
|
{
|
|
wrongSound.PlayOneShot(wrongSound.clip);
|
|
}
|
|
}
|
|
|
|
|
|
private void NextPanel()
|
|
{
|
|
if (!hasAnswered) return; // Hanya bisa lanjut jika sudah menjawab
|
|
|
|
currentPanelIndex++;
|
|
if (currentPanelIndex >= maxPanels)
|
|
{
|
|
Debug.Log("Game selesai!");
|
|
return;
|
|
}
|
|
|
|
hasAnswered = false;
|
|
ShowPanel(currentPanelIndex);
|
|
}
|
|
|
|
private void BackToPanelOne()
|
|
{
|
|
currentPanelIndex = 0;
|
|
hasAnswered = false;
|
|
ShowPanel(0);
|
|
}
|
|
|
|
private void ShowPanel(int index)
|
|
{
|
|
for (int i = 0; i < panels.Length; i++)
|
|
{
|
|
panels[i].SetActive(i == index);
|
|
}
|
|
}
|
|
}
|