-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameManager.cs
196 lines (172 loc) · 6.06 KB
/
GameManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Text;
using System;
public class GameManager : MonoBehaviour {
private string questionsFilePath= "Resources/Questions.txt";
private List<int> usedQuestions = new List<int>();
public Text questionLabel;
private static string question;
public Text answerALabel, answerBLabel, answerCLabel, answerDLabel;
private static string answerA, answerB, answerC, answerD;
private static string correctAnswer;
private static string selectedAnswer;
private static string numberOfQuestionInFile;
public RawImage life1, life2, life3;
private int lifes;
private Texture redTxt = null;
private Texture greenTxt = null;
public Text timeLabel;
private float time;
public Text scoreLabel;
private int score;
private int numberOfQuestionsAnswered;
private static Boolean hasAnswered;
public GameObject sphere;
public Transform spherePos;
GameObject mySphere=null;
// Use this for initialization
void Start () {
score = 0;
numberOfQuestionsAnswered = 0;
lifes = 3;
play();
timeLabel.text = "Time: " + time;
scoreLabel.text = "Score: " + score;
redTxt = Resources.Load("LastLife", typeof(Texture2D)) as Texture;
greenTxt = Resources.Load("Life", typeof(Texture2D)) as Texture;
}
// Update is called once per frame
void Update () {
if(time>=0){
time = time-Time.deltaTime;
timeLabel.text = "Time: "+ (int)time;
scoreLabel.text = "Score: " + score;
if (hasAnswered) {
if (selectedAnswer.Equals(correctAnswer)) {
score = score + (((int)time) * 10);
numberOfQuestionsAnswered++;
play();
}
else {
if (lifes > 1){
lifes -= 1;
switch (lifes){
case 2: life3.enabled = false;
break;
case 1: life2.enabled = false;
life1.texture = redTxt;
break;
}
play();
}
else{
wrongAnswer();
}
}
}
}
else {
if (lifes > 1){
lifes -= 1;
switch (lifes){
case 2:
life3.enabled = false;
break;
case 1:
life2.enabled = false;
life1.texture = redTxt;
break;
}
Destroy(mySphere);
play();
}
else{
timeExpired();
}
}
}
public void play(){
readQuestion("" + getQuestion());
setQuestionAndAnswers();
time = 15f;
hasAnswered = false;
mySphere = Instantiate(sphere, spherePos.position, spherePos.rotation);
}
public void setQuestionAndAnswers(){
questionLabel.text = "" + question;
answerALabel.text = "A." + answerA;
answerBLabel.text = "B." + answerB;
answerCLabel.text = "C." + answerC;
answerDLabel.text = "D." + answerD;
}
public int getQuestion(){
int val = UnityEngine.Random.Range(1,30);
while(usedQuestions.Contains(val)){
val = UnityEngine.Random.Range(1, 30);
}
usedQuestions.Add(val);
return val;
}
public void timeExpired(){
Scores.submitScore(score, numberOfQuestionsAnswered, DateTime.Now);
Gameover.setMessage("Ups...Game Over!\r\n Timeout");
SceneLoader.gameOver();
life1.texture = greenTxt;
}
public void wrongAnswer(){
Scores.submitScore(score, numberOfQuestionsAnswered, DateTime.Now);
Gameover.setMessage("Ups...Game Over!\r\n Wrong Answer");
SceneLoader.gameOver();
life1.texture = greenTxt;
}
public void parseQuestion(string questionLine){
string[] entries = questionLine.Split(';');
for (int i = 0; i < entries.Length; i++){
switch (i){
case 0: numberOfQuestionInFile = entries[0]; break;
case 1: question = entries[1]; break;
case 2: answerA = entries[2]; break;
case 3: answerB = entries[3]; break;
case 4: answerC = entries[4]; break;
case 5: answerD = entries[5]; break;
case 6: correctAnswer = entries[6].Trim(); break;
}
}
}
public void readQuestion(string numberOfQuestionInFile){
try{
TextAsset ta = Resources.Load("questions") as TextAsset;
List<string> questions = textAssetToList(ta);
foreach(string quest in questions){
if (quest.StartsWith(numberOfQuestionInFile)){
parseQuestion(quest);
}
}
}
catch (Exception e){
Console.WriteLine("{0}\n", e.Message);
}
}
private List<string> textAssetToList(TextAsset ta){
var listToReturn = new List<string>();
var arrayString = ta.text.Split('\n');
foreach (var line in arrayString){
listToReturn.Add(line);
}
return listToReturn;
}
public static void setSelectedAnswer(String answer){
switch (answer){
case "A": selectedAnswer = answerA; break;
case "B": selectedAnswer = answerB; break;
case "C": selectedAnswer = answerC; break;
case "D": selectedAnswer = answerD; break;
}
hasAnswered = true;
}
}