95 lines
2.2 KiB
C#
95 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class A1M2QuestionManager : MonoBehaviour
|
|
{
|
|
public List<A1M2QuestionClass> questions;
|
|
public static A1M2QuestionManager instance;
|
|
public AudioSource thisAS;
|
|
public Image bgImage;
|
|
public Transform answerOrganizer;
|
|
public Text questionTxt;
|
|
private int currentIndex;
|
|
private Animator anim;
|
|
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
anim = GetComponent<Animator>();
|
|
LoadNextQuestion();
|
|
GameControl.instance.StartedGame();
|
|
}
|
|
|
|
public void LoadNextQuestion()
|
|
{
|
|
if (questions[currentIndex].background)
|
|
{
|
|
bgImage.sprite = questions[currentIndex].background;
|
|
}
|
|
questionTxt.text = questions[currentIndex].questionString;
|
|
SetupAnswers();
|
|
}
|
|
|
|
public void SetupAnswers()
|
|
{
|
|
List<A1M2AnswerButton> btnList = new List<A1M2AnswerButton>();
|
|
btnList.AddRange(answerOrganizer.GetComponentsInChildren<A1M2AnswerButton>());
|
|
for(int i = 0; i < btnList.Count; i++)
|
|
{
|
|
btnList[i].SetupThis(questions[currentIndex].answers[i]);
|
|
}
|
|
PlayAudio();
|
|
}
|
|
|
|
public void CorrectAnswer()
|
|
{
|
|
anim.SetTrigger("Correct");
|
|
A10D1AudioSystem.instance.Correct();
|
|
currentIndex++;
|
|
if (currentIndex < questions.Count)
|
|
{
|
|
StartCoroutine(WaitToNext());
|
|
}
|
|
else
|
|
{
|
|
GameControl.instance.FinishGame();
|
|
}
|
|
|
|
}
|
|
|
|
public void WrongAnswer()
|
|
{
|
|
anim.SetTrigger("Wrong");
|
|
A10D1AudioSystem.instance.Wrong();
|
|
}
|
|
|
|
public void PlayAudio()
|
|
{
|
|
if (thisAS.isPlaying)
|
|
{
|
|
StartCoroutine(QueueSound(questions[currentIndex].questionAC));
|
|
}
|
|
else
|
|
{
|
|
thisAS.PlayOneShot(questions[currentIndex].questionAC);
|
|
}
|
|
}
|
|
|
|
IEnumerator QueueSound(AudioClip toPlay)
|
|
{
|
|
while(thisAS.isPlaying)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
thisAS.PlayOneShot(toPlay);
|
|
}
|
|
|
|
IEnumerator WaitToNext()
|
|
{
|
|
yield return new WaitForSecondsRealtime(1);
|
|
LoadNextQuestion();
|
|
}
|
|
}
|