109 lines
2.2 KiB
C#
109 lines
2.2 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SoalManager : MonoBehaviour
|
|
{
|
|
public Text scoreText;
|
|
public Button resetButton;
|
|
public AudioSource correctSound;
|
|
public AudioSource wrongSound;
|
|
public GameObject[] panels;
|
|
|
|
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;
|
|
hasAnswered = true;
|
|
|
|
StartCoroutine(HandleAnswer(isCorrect));
|
|
}
|
|
|
|
private IEnumerator HandleAnswer(bool isCorrect)
|
|
{
|
|
if (isCorrect)
|
|
{
|
|
PlayCorrectFeedback();
|
|
score += 10;
|
|
scoreText.text = score.ToString();
|
|
}
|
|
else
|
|
{
|
|
PlayWrongFeedback();
|
|
}
|
|
|
|
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;
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |