MIF_E31221357/Assets/Scripts/ShowingQuest.cs

179 lines
4.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
public class ShowingQuest : MonoBehaviour
{
public TextMeshProUGUI questText;
public TextMeshProUGUI[] answerTexts;
public int[] randomAnswers;
public ControlQuest controlQuest;
int numberQuest;
public int gameRound;
public int[] randomQuests;
[Header("Panel")]
public GameObject panelResult;
public GameObject panelFeedback;
public TextMeshProUGUI feedbackText;
public Image panelFeedColor;
[Header("Point")]
public TextMeshProUGUI pointText;
public int point;
int totalPoint;
public TextMeshProUGUI textNoQuest;
public TextMeshProUGUI textSoalBiasa;
public GameObject scrollSoalPanjang;
public TextMeshProUGUI textSoalPanjang;
public enum TestType { Pretest, Posttest }
public TestType testType;
AudioGame audioGame;
private void Awake()
{
audioGame = GameObject.FindGameObjectWithTag("Audio").GetComponent<AudioGame>();
}
void Start()
{
RandomNumberQuest();
//RandomNumberAnswer();
GenerateQuest();
}
void Update()
{
textNoQuest.text = (numberQuest + 1).ToString();
}
void RandomNumberQuest()
{
for (int i = 0; i < randomQuests.Length; i++)
{
int a = randomQuests[i];
int b = Random.Range(0, randomQuests.Length);
randomQuests[i] = randomQuests[b];
randomQuests[b] = a;
}
}
void RandomNumberAnswer()
{
for (int i = 0; i < randomAnswers.Length; i++)
{
int a = randomAnswers[i];
int b = Random.Range(0, randomAnswers.Length);
randomAnswers[i] = randomAnswers[b];
randomAnswers[b] = a;
}
}
void GenerateQuest()
{
RandomNumberAnswer();
string soal = controlQuest.quests[randomQuests[numberQuest]].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;
}
for (int i = 0; i < answerTexts.Length; i++)
{
answerTexts[i].text = controlQuest.quests[randomQuests[numberQuest]].elementQuest.answers[randomAnswers[i]];
}
}
public void ButtonAnswer()
{
TextMeshProUGUI currentAnswer = EventSystem.current.currentSelectedGameObject.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
if (currentAnswer.text == controlQuest.quests[randomQuests[numberQuest]].elementQuest.answers[controlQuest.quests[randomQuests[numberQuest]].elementQuest.trueAnswer])
{
feedbackText.text = "Benar!";
panelFeedColor.color = new Color32(12, 213, 7, 255);
//totalPoint += point;
audioGame.PlaySFX(audioGame.correct);
Debug.Log("benar");
totalPoint += point;
}
else
{
feedbackText.text = "Salah!";
panelFeedColor.color = Color.red;
audioGame.PlaySFX(audioGame.incorrect);
Debug.Log("salah");
}
panelFeedback.SetActive(true);
Invoke(nameof(CloseFeedback), 1f);
if (numberQuest == gameRound - 1)
{
//string kodeLogin = TestManager.Instance.kodeLogin;
string kodeLogin = TestManager.Instance != null ? TestManager.Instance.kodeLogin : null;
if (string.IsNullOrEmpty(kodeLogin))
{
Debug.LogError("KodeLogin kosong! Skor tidak disimpan.");
return;
}
DBManager.SaveTestScore(kodeLogin, totalPoint, testType);
DBManager.SyncUserToFirebase(kodeLogin, success => {
Debug.Log("Sync selesai: " + success);
});
if (testType == TestType.Pretest)
{
TestManager.Instance.pretestScore = totalPoint;
}
else if (testType == TestType.Posttest)
{
TestManager.Instance.posttestScore = totalPoint;
}
panelResult.transform.GetComponentInChildren<TextMeshProUGUI>().text = "Selamat telah menyelesaikan semua soal!";
panelResult.SetActive(true);
pointText.text = $"Total point : {totalPoint} / {point * gameRound}";
}
else
{
numberQuest++;
GenerateQuest();
}
}
void CloseFeedback()
{
panelFeedback.SetActive(false);
audioGame.PlaySFX(audioGame.popUp);
}
}