Files
old-profesoratom/Assets/Scripts/QuizController.cs

139 lines
3.7 KiB
C#
Raw Normal View History

2017-05-17 14:11:01 +02:00

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
public class QuizController : MonoBehaviour {
public GameObject[] answers;
public Text counterText;
public Text scoreText;
public Text questionText;
public Button btnNextQuestion;
2017-05-17 15:40:49 +02:00
private float counterValue;
2017-05-17 14:11:01 +02:00
2017-05-17 15:40:49 +02:00
private const int numberOfAnswers = 4;
2017-05-17 14:11:01 +02:00
2017-05-17 15:40:49 +02:00
private const int scoreCorrectAnswer = 10;
private const int scoreIncorrectAnswer = -15;
private const int scoreTimedOut = 0;
private const int counterInitialValue = 10;
private const int displayAnswersCounterValue = 3;
2017-05-17 14:11:01 +02:00
private int scoreValue = 0;
private int questionIdx = 0;
private List<int> selectedAnswers;
List<Question> questions = new List<Question>() {
2017-05-18 10:29:14 +02:00
new Question("1. Neki od ovih prozora imaju 2-3 puta manje toplotnih gubitaka za razliku od drugih, koji su to?", new int[] {1, 3}),
new Question("2. Na jedan od načina možete uštedjeti i do 10 litara vode po osobi dnevno, uz koji predmet?", new int[] {2}),
2017-05-17 14:11:01 +02:00
};
// Use this for initialization
void Start () {
selectedAnswers = new List<int>();
2017-05-17 15:40:49 +02:00
counterValue = counterInitialValue;
2017-05-17 14:11:01 +02:00
UpdateScene();
UpdateCounter();
UpdateScore();
btnNextQuestion.GetComponent<Button>().onClick.AddListener(LoadNextQuestion);
}
// Update is called once per frame
void Update()
{
if (counterValue > 0)
{
counterValue -= Time.deltaTime;
UpdateCounter();
}
2017-05-17 15:40:49 +02:00
else
{
LoadNextQuestion();
counterValue = counterInitialValue;
}
2017-05-17 14:11:01 +02:00
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
Debug.Log(hit.transform.gameObject.name);
2017-05-17 15:40:49 +02:00
int answerIdx = GetAnswerIdx(hit.transform.gameObject.name) - numberOfAnswers * questionIdx;
2017-05-17 14:11:01 +02:00
if (!selectedAnswers.Contains(answerIdx))
selectedAnswers.Add(answerIdx);
}
}
}
void LoadNextQuestion()
{
2017-05-17 15:40:49 +02:00
scoreValue += GetScore();
UpdateScore();
2017-05-17 14:11:01 +02:00
questionIdx = (questionIdx + 1) % 2;
selectedAnswers.Clear();
UpdateScene();
2017-05-17 15:40:49 +02:00
counterValue = counterInitialValue;
2017-05-17 14:11:01 +02:00
}
private void UpdateScore()
{
2017-05-18 10:29:14 +02:00
scoreText.text = scoreValue.ToString();
2017-05-17 14:11:01 +02:00
}
private void UpdateCounter()
{
2017-05-18 10:29:14 +02:00
counterText.text = counterValue.ToString("f0");
2017-05-17 14:11:01 +02:00
}
private void UpdateScene()
{
// Set new question
questionText.text = questions[questionIdx].getQuestionText();
// Set right answers
for(int i = 0; i < answers.Length; i++)
{
2017-05-17 15:40:49 +02:00
if (i >= numberOfAnswers * questionIdx && i < numberOfAnswers * (questionIdx + 1))
2017-05-17 14:11:01 +02:00
{
Debug.Log("Set active: " + i);
answers[i].SetActive(true);
}
else
answers[i].SetActive(false);
}
}
private int GetAnswerIdx(string gameObjName)
{
string resultString = Regex.Match(gameObjName, @"\d+").Value;
return System.Int32.Parse(resultString);
}
2017-05-17 15:40:49 +02:00
private int GetScore() {
// Didn't try
if (selectedAnswers.Count == 0)
return scoreTimedOut;
// Correct answer
if (selectedAnswers.SequenceEqual(new List<int>(questions[questionIdx].getCorrectAnswers())))
return scoreCorrectAnswer;
else
return scoreIncorrectAnswer;
2017-05-17 14:11:01 +02:00
}
}