using System; using System.Collections.Generic; using GameOff.Quiz; using UnityEngine; using Random = System.Random; namespace GameOff.Core { [DefaultExecutionOrder(-60)] public class QuizHandler : MonoBehaviour { public static QuizHandler Instance { get; private set; } [SerializeField] private int questionAmount = 20; [SerializeField] private List questionVisual; [SerializeField] private int seed; [SerializeField] private bool randomGeneration; [SerializeField] private string[] correctAnswer; private readonly List ChoicePossibles = new List() { "a", "b", "c", "d", "e", "f" }; private Random _random; private List _questionInfos; private void Awake() { if (Instance) { Debug.LogError($"QuizManager already exist! {transform}"); Destroy(gameObject); return; } Instance = this; } private void Start() { if (randomGeneration) seed = UnityEngine.Random.Range(0, Int32.MaxValue); _random = new Random(seed); GenerateQuiz(); } private void GenerateQuiz() { _questionInfos = new List(); correctAnswer = new string[questionAmount]; for (int i = 0; i < questionAmount; i++) { int amount = _random.Next(2, ChoicePossibles.Count); string answer = ChoicePossibles[_random.Next(amount)]; List tmp = new List(); for (int x = 0; x < amount; x++) tmp.Add(ChoicePossibles[x]); _questionInfos.Add(new QuestionInfo(i + 1, questionVisual[_random.Next(questionVisual.Count)], tmp, answer)); correctAnswer[i] = tmp[_random.Next(amount)]; } } public string GetAnswerAtIndex(int index) => correctAnswer[index]; public List QuestionInfos => _questionInfos; public int QuestionAmount => questionAmount; } }