133 lines
3.6 KiB
C#
133 lines
3.6 KiB
C#
using PahlawanEnergi.Quiz;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace PahlawanEnergi.Quiz
|
|
{
|
|
public class QuizManager : MonoBehaviour
|
|
{
|
|
public static QuizManager instance;
|
|
|
|
[Header("Quiz")]
|
|
[SerializeField] private SO_QuizLibrary _library;
|
|
public int level;
|
|
[SerializeField] private Quiz quiz;
|
|
[SerializeField] private List<QuizInteractable> quizInteractables = new List<QuizInteractable>();
|
|
[SerializeField] private bool randomizeQuizOrder;
|
|
|
|
[Header("Score")]
|
|
[SerializeField] private int _score;
|
|
[SerializeField] private int interactCount;
|
|
[SerializeField] private TextMeshProUGUI scoreText;
|
|
|
|
public int Score => _score;
|
|
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
quiz = _library.GetQuizByLevel(level);
|
|
|
|
List<int> shuffledIds = new List<int>();
|
|
for (int i = 0; i < quizInteractables.Count; i++)
|
|
{
|
|
shuffledIds.Add(i + 1);
|
|
}
|
|
|
|
if (randomizeQuizOrder)
|
|
{
|
|
for (int i = shuffledIds.Count - 1; i > 0; i--)
|
|
{
|
|
int rnd = UnityEngine.Random.Range(0, i + 1);
|
|
int temp = shuffledIds[i];
|
|
shuffledIds[i] = shuffledIds[rnd];
|
|
shuffledIds[rnd] = temp;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < quizInteractables.Count; i++)
|
|
{
|
|
if (quizInteractables[i] != null)
|
|
{
|
|
quizInteractables[i].questionId = shuffledIds[i];
|
|
}
|
|
}
|
|
|
|
_score = 0;
|
|
scoreText.text = $"{_score}/{quiz.quizItems.Count}";
|
|
}
|
|
|
|
internal QuizItem GetQuestion(int questionId)
|
|
{
|
|
return quiz.GetQuizItemById(questionId);
|
|
}
|
|
|
|
public void CompleteQuestion(int questionId)
|
|
{
|
|
for (int i = 0; i < quizInteractables.Count; i++)
|
|
{
|
|
if (quizInteractables[i] != null && quizInteractables[i].questionId == questionId)
|
|
{
|
|
quizInteractables[i].gameObject.SetActive(false);
|
|
break;
|
|
}
|
|
}
|
|
interactCount++;
|
|
}
|
|
|
|
public void CheckEndGame()
|
|
{
|
|
if (interactCount >= quiz.quizItems.Count)
|
|
{
|
|
Invoke(nameof(EndGame), 1f);
|
|
}
|
|
}
|
|
|
|
private void EndGame()
|
|
{
|
|
int finalStars = 0;
|
|
|
|
if (_score == quiz.quizItems.Count)
|
|
{
|
|
finalStars = 3;
|
|
}
|
|
// Menghitung jika benar sekitar 60% atau lebih (misal 3 atau 4 benar dari 5 kuis)
|
|
else if (_score >= Mathf.CeilToInt(quiz.quizItems.Count * 0.6f))
|
|
{
|
|
finalStars = 2;
|
|
}
|
|
else if (_score > 0)
|
|
{
|
|
finalStars = 1;
|
|
}
|
|
|
|
_score = finalStars;
|
|
|
|
GameplayGUIManager.instance.OpenResultScreen();
|
|
PlayerProgressData.instance.PlayGame(level, _score);
|
|
}
|
|
|
|
public void AddScore(int value)
|
|
{
|
|
_score += value;
|
|
scoreText.text = $"{_score}/{quiz.quizItems.Count}";
|
|
}
|
|
|
|
public void GoToNextLevel()
|
|
{
|
|
SceneManager.LoadScene($"Level{level + 1}");
|
|
}
|
|
}
|
|
} |