initial commit
This commit is contained in:
26
Assets/Scripts/Question.cs
Normal file
26
Assets/Scripts/Question.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Question
|
||||
{
|
||||
public Question(string question, int [] answers)
|
||||
{
|
||||
questionText = question;
|
||||
correctAnswers = answers;
|
||||
}
|
||||
|
||||
private int[] correctAnswers;
|
||||
|
||||
private string questionText;
|
||||
|
||||
public int[] getCorrectAnswers()
|
||||
{
|
||||
return correctAnswers;
|
||||
}
|
||||
|
||||
public string getQuestionText()
|
||||
{
|
||||
return questionText;
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/Question.cs.meta
Normal file
12
Assets/Scripts/Question.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 486cf298109d9614dbb59e37a86e3336
|
||||
timeCreated: 1494964353
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
132
Assets/Scripts/QuizController.cs
Normal file
132
Assets/Scripts/QuizController.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
|
||||
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;
|
||||
|
||||
private float counterValue = 90;
|
||||
|
||||
private const int scoreIncreaseValue = 10;
|
||||
|
||||
private const int scoreDecreaseValue = 15;
|
||||
|
||||
private int scoreValue = 0;
|
||||
|
||||
private int questionIdx = 0;
|
||||
|
||||
private List<int> selectedAnswers;
|
||||
|
||||
List<Question> questions = new List<Question>() {
|
||||
new Question("Q1", new int[] {1, 3}),
|
||||
new Question("Q2", new int[] {2}),
|
||||
};
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
selectedAnswers = new List<int>();
|
||||
UpdateScene();
|
||||
UpdateCounter();
|
||||
UpdateScore();
|
||||
btnNextQuestion.GetComponent<Button>().onClick.AddListener(LoadNextQuestion);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (counterValue > 0)
|
||||
{
|
||||
counterValue -= Time.deltaTime;
|
||||
UpdateCounter();
|
||||
}
|
||||
|
||||
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);
|
||||
int answerIdx = GetAnswerIdx(hit.transform.gameObject.name) - 4 * questionIdx;
|
||||
if (!selectedAnswers.Contains(answerIdx))
|
||||
selectedAnswers.Add(answerIdx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void IncreaseScore()
|
||||
{
|
||||
Debug.Log("Clicked on cube!");
|
||||
scoreValue += scoreIncreaseValue;
|
||||
UpdateScore();
|
||||
}
|
||||
|
||||
void LoadNextQuestion()
|
||||
{
|
||||
if (IsAnswerCorrect())
|
||||
IncreaseScore();
|
||||
else
|
||||
DecreaseScore();
|
||||
questionIdx = (questionIdx + 1) % 2;
|
||||
selectedAnswers.Clear();
|
||||
Debug.Log("" + questionIdx);
|
||||
UpdateScene();
|
||||
}
|
||||
|
||||
public void DecreaseScore()
|
||||
{
|
||||
Debug.Log("Clicked on sphere!");
|
||||
scoreValue -= scoreDecreaseValue;
|
||||
UpdateScore();
|
||||
}
|
||||
|
||||
private void UpdateScore()
|
||||
{
|
||||
scoreText.text = "Score: " + scoreValue.ToString();
|
||||
}
|
||||
|
||||
private void UpdateCounter()
|
||||
{
|
||||
counterText.text = "Counter: " + counterValue.ToString("f0");
|
||||
}
|
||||
|
||||
private void UpdateScene()
|
||||
{
|
||||
// Set new question
|
||||
questionText.text = questions[questionIdx].getQuestionText();
|
||||
// Set right answers
|
||||
for(int i = 0; i < answers.Length; i++)
|
||||
{
|
||||
if (i >= 4 * questionIdx && i < 4 * (questionIdx + 1))
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
private bool IsAnswerCorrect() {
|
||||
return selectedAnswers.SequenceEqual(new List<int>(questions[questionIdx].getCorrectAnswers()));
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/QuizController.cs.meta
Normal file
12
Assets/Scripts/QuizController.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da0ca66fc472e564b8d820d88ca4cd6a
|
||||
timeCreated: 1494959260
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user