213 lines
5.4 KiB
C#
213 lines
5.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq; // Tambahkan ini
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
|
|
public class Soal : MonoBehaviour
|
|
{
|
|
public TextAsset assetSoal;
|
|
public AudioClip correctSound;
|
|
public AudioClip wrongSound;
|
|
private AudioSource audioSource;
|
|
private string[] soal;
|
|
private string[,] soalBag;
|
|
|
|
int indexSoal;
|
|
int maxSoal;
|
|
bool ambilSoal;
|
|
char kunciJ;
|
|
|
|
bool[] soalSelesai;
|
|
|
|
public Text txtSoal, txtOpsiA, txtOpsiB, txtOpsiC, txtOpsiD;
|
|
|
|
bool isHasil;
|
|
private float durasi;
|
|
public float durasiPenilaian;
|
|
|
|
int jwbBenar, jwbSalah;
|
|
float nilai;
|
|
|
|
public GameObject panel;
|
|
public GameObject imgPenilaian, imgHasil;
|
|
public Text txtHasil;
|
|
|
|
public GameObject BenarObj;
|
|
public GameObject SalahObj;
|
|
|
|
void Start()
|
|
{
|
|
durasi = durasiPenilaian;
|
|
|
|
soal = assetSoal.ToString().Split('#');
|
|
|
|
soalSelesai = new bool[soal.Length];
|
|
|
|
soalBag = new string[soal.Length, 6];
|
|
maxSoal = soal.Length;
|
|
OlahSoal();
|
|
|
|
ambilSoal = true;
|
|
TampilkanSoal();
|
|
|
|
print(soalBag[1, 3]);
|
|
|
|
audioSource = gameObject.AddComponent<AudioSource>();
|
|
}
|
|
|
|
private void OlahSoal()
|
|
{
|
|
for (int i = 0; i < soal.Length; i++)
|
|
{
|
|
string[] tempSoal = soal[i].Split('+');
|
|
for (int j = 0; j < tempSoal.Length; j++)
|
|
{
|
|
soalBag[i, j] = tempSoal[j];
|
|
continue;
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
|
|
public void back(string scenename)
|
|
{
|
|
SceneManager.LoadScene(scenename);
|
|
}
|
|
|
|
private void TampilkanSoal()
|
|
{
|
|
if (indexSoal < maxSoal)
|
|
{
|
|
if (ambilSoal)
|
|
{
|
|
for (int i = 0; i < soal.Length; i++)
|
|
{
|
|
int randomIndexSoal = UnityEngine.Random.Range(0, soal.Length);
|
|
if (!soalSelesai[randomIndexSoal])
|
|
{
|
|
txtSoal.text = soalBag[randomIndexSoal, 0];
|
|
txtOpsiA.text = soalBag[randomIndexSoal, 1];
|
|
txtOpsiB.text = soalBag[randomIndexSoal, 2];
|
|
txtOpsiC.text = soalBag[randomIndexSoal, 3];
|
|
txtOpsiD.text = soalBag[randomIndexSoal, 4];
|
|
kunciJ = soalBag[randomIndexSoal, 5][0];
|
|
|
|
soalSelesai[randomIndexSoal] = true;
|
|
|
|
ambilSoal = false;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Opsi(string opsiHuruf)
|
|
{
|
|
CheckJawaban(opsiHuruf[0]);
|
|
|
|
if (indexSoal == maxSoal - 1)
|
|
{
|
|
isHasil = true;
|
|
}
|
|
else
|
|
{
|
|
indexSoal++;
|
|
ambilSoal = true;
|
|
}
|
|
|
|
panel.SetActive(true);
|
|
}
|
|
|
|
private float HitungNilai()
|
|
{
|
|
return nilai = (float)jwbBenar / maxSoal * 100;
|
|
}
|
|
|
|
private void CheckJawaban(char huruf)
|
|
{
|
|
if (huruf.Equals(kunciJ))
|
|
{
|
|
BenarObj.SetActive(true);
|
|
SalahObj.SetActive(false);
|
|
jwbBenar++;
|
|
audioSource.PlayOneShot(correctSound);
|
|
}
|
|
else
|
|
{
|
|
SalahObj.SetActive(true);
|
|
BenarObj.SetActive(false);
|
|
jwbSalah++;
|
|
audioSource.PlayOneShot(wrongSound);
|
|
}
|
|
}
|
|
|
|
private void SimpanSkor(string namaLengkap, float nilai)
|
|
{
|
|
|
|
string savedNames = PlayerPrefs.GetString("NamaLengkapList", "");
|
|
List<string> namesList = string.IsNullOrEmpty(savedNames) ? new List<string>() : savedNames.Split(',').ToList();
|
|
if (!namesList.Contains(namaLengkap))
|
|
{
|
|
namesList.Add(namaLengkap);
|
|
PlayerPrefs.SetString("NamaLengkapList", string.Join(",", namesList));
|
|
}
|
|
PlayerPrefs.SetInt(namaLengkap + "Score", Mathf.RoundToInt(nilai));
|
|
PlayerPrefs.Save();
|
|
DisplayNamadanSkor.Instance.TampilkanNamaDanSkor();
|
|
|
|
Debug.Log($"Menyimpan skor untuk {namaLengkap}: {Mathf.RoundToInt(nilai)}"); // Tambahkan log untuk debug
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
if (panel.activeSelf)
|
|
{
|
|
durasiPenilaian -= Time.deltaTime;
|
|
|
|
if (isHasil)
|
|
{
|
|
imgPenilaian.SetActive(true);
|
|
imgHasil.SetActive(false);
|
|
|
|
if (durasiPenilaian <= 0)
|
|
{
|
|
txtHasil.text = "Jumlah benar : " + jwbBenar + "\nJumlah Salah : " + jwbSalah + "\n\nNilai : " + HitungNilai();
|
|
|
|
imgPenilaian.SetActive(false);
|
|
imgHasil.SetActive(true);
|
|
|
|
durasiPenilaian = 0;
|
|
string namaLengkap = PlayerPrefs.GetString("CurrentPlayerName", "");
|
|
SimpanSkor(namaLengkap, nilai);
|
|
}
|
|
Timer.Instance.GameAktif = false;
|
|
}
|
|
else
|
|
{
|
|
imgPenilaian.SetActive(true);
|
|
imgHasil.SetActive(false);
|
|
|
|
if (durasiPenilaian <= 0)
|
|
{
|
|
panel.SetActive(false);
|
|
durasiPenilaian = durasi;
|
|
|
|
TampilkanSoal();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|