145 lines
3.5 KiB
C#
145 lines
3.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class RWA_QuestionManager : MonoBehaviour
|
|
{
|
|
public static RWA_QuestionManager instance;
|
|
|
|
private Button spinButton;
|
|
[SerializeField]
|
|
private List<RWA_QuestionClass> questions;
|
|
private Animator animCanvas;
|
|
private Text winText, numberText;
|
|
private int playerScore, pickedNumber;
|
|
[SerializeField]
|
|
private List<int> possibleNumbers = new List<int>();
|
|
private RWA_QuestionObejct questionObject;
|
|
|
|
private void Awake()
|
|
{
|
|
spinButton = transform.Find("PickBtn").GetComponent<Button>();
|
|
spinButton.onClick.AddListener(AnimateNumbers);
|
|
for (int i = 0; i < questions.Count; i++)
|
|
{
|
|
possibleNumbers.Add(i);
|
|
}
|
|
winText = GameObject.Find("PointsText").GetComponent<Text>();
|
|
numberText = transform.Find("NumberText").GetComponent<Text>();
|
|
animCanvas = GetComponent<Animator>();
|
|
instance = this;
|
|
questionObject = GameObject.Find("QuestionObject").GetComponent<RWA_QuestionObejct>();
|
|
GameControl jooj = GameObject.Find("GameControl").GetComponent<GameControl>();
|
|
jooj.ended = false;
|
|
jooj.finishedGame = false;
|
|
jooj.failedGame = false;
|
|
}
|
|
|
|
public void SetupQuestion()
|
|
{
|
|
questionObject.SetupQuestion(questions[pickedNumber]);
|
|
}
|
|
|
|
public void ReceiveAnswer(RWA_AnswerClass ac)
|
|
{
|
|
questionObject.DeactivateAnswers();
|
|
if (ac.isCorrect)
|
|
{
|
|
CorrectAnswer();
|
|
}
|
|
else
|
|
{
|
|
WrongAnswer();
|
|
}
|
|
if (HasItEnded())
|
|
{
|
|
FinishGame();
|
|
}
|
|
else
|
|
{
|
|
NextQuestion();
|
|
spinButton.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
public void NextQuestion()
|
|
{
|
|
StartCoroutine(WaitToDoSomething());
|
|
}
|
|
|
|
public bool HasItEnded()
|
|
{
|
|
if (possibleNumbers.Count > 0)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public void PickRandomNumber()
|
|
{
|
|
if (possibleNumbers.Count > 0)
|
|
{
|
|
int tempR = Random.Range(0, possibleNumbers.Count);
|
|
pickedNumber = possibleNumbers[tempR];
|
|
possibleNumbers.RemoveAt(tempR);
|
|
numberText.text = pickedNumber.ToString();
|
|
animCanvas.SetTrigger("GoToQuestion");
|
|
|
|
SetupQuestion();
|
|
}
|
|
else
|
|
{
|
|
FinishGame();
|
|
}
|
|
|
|
}
|
|
|
|
public void CorrectAnswer()
|
|
{
|
|
animCanvas.SetTrigger("Correct");
|
|
playerScore++;
|
|
|
|
}
|
|
|
|
public void WrongAnswer()
|
|
{
|
|
animCanvas.SetTrigger("Wrong");
|
|
|
|
}
|
|
|
|
public void AnimateNumbers()
|
|
{
|
|
spinButton.gameObject.SetActive(false);
|
|
StartCoroutine(DoCrazyNumbers());
|
|
}
|
|
|
|
public void FinishGame()
|
|
{
|
|
animCanvas.SetTrigger("FinishedGame");
|
|
winText.text = "Points : " + playerScore.ToString();
|
|
GameControl jooj = GameObject.Find("GameControl").GetComponent<GameControl>();
|
|
jooj.finishedGame = true;
|
|
}
|
|
|
|
IEnumerator DoCrazyNumbers()
|
|
{
|
|
for (float i = 0; i < .6f; i += .1f)
|
|
{
|
|
numberText.text = possibleNumbers[Random.Range(0, possibleNumbers.Count)].ToString();
|
|
yield return new WaitForSecondsRealtime(i);
|
|
}
|
|
PickRandomNumber();
|
|
}
|
|
|
|
IEnumerator WaitToDoSomething()
|
|
{
|
|
yield return new WaitForSecondsRealtime(1);
|
|
animCanvas.SetTrigger("Numbers");
|
|
}
|
|
}
|