-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameScript.cs
247 lines (196 loc) · 6.97 KB
/
GameScript.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2016 Hugo Perrin, Younes Laaboudi, Maxime Fétiveau, Jules Massin, Olivier Polidori, Seung-Eun Yi
//This file is part of SBT12-GameProjectForAutism.
// SBT12-GameProjectForAutism is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// SBT12-GameProjectForAutism is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with SBT12-GameProjectForAutism. If not, see<http://www.gnu.org/licenses/>.
using UnityEngine;
using System.Collections;
using System; // to import String
using System.IO ; // to import File
using System.Diagnostics; // to import Stopwatch
public class GameScript : MonoBehaviour {
GameController gameController; // Mouse or LeapMotion
public Bubbles bubblesPart1 ;
public Bubbles bubblesPart2 ;
public Bubbles bubblesPart3 ;
public Bubbles bubblesPart4 ;
public Bubbles movingBubblesPart1 ;
public Bubbles movingBubblesPart2 ;
public Bubbles movingBubblesPart3 ;
public Bubbles fallingBubblesPart1 ;
public Bubbles fallingBubblesPart2 ;
Bubbles currentBubblesSet;
public ArrayList bubblesSetsList = new ArrayList ();
private int numberLevels;
public PlayerController player;
private int score;
private int bonusPoints;
private int malusPoints;
private void NextLevel()
{
Application.LoadLevel("level1");
}
private bool endFlag;
void Awake () {
player.setGameController (new Mouse());
bubblesSetsList.Add (bubblesPart1);
bubblesSetsList.Add (bubblesPart2);
bubblesSetsList.Add (bubblesPart3);
bubblesSetsList.Add (bubblesPart4);
bubblesSetsList.Add (movingBubblesPart1);
bubblesSetsList.Add (movingBubblesPart2);
bubblesSetsList.Add (movingBubblesPart3);
bubblesSetsList.Add (fallingBubblesPart1);
bubblesSetsList.Add (fallingBubblesPart2);
numberLevels = bubblesSetsList.Count;
// starting the game
score = 0;
this.setAllBubblesInactive();
this.displayBubbles(bubblesPart1);
currentBubblesSet = bubblesPart1;
endFlag = false;
}
// Update is called once per frame
void Update () {
this.checkController ();
// enables to exit the game smoothly
// to go back to the first level
if (Input.GetKey ("r")) {
resetGame ();
}
//to manually operate how levels change
//if (Input.GetKeyDown ("n")) { // without "Down" several levels are simultaniousily passed
//nextLevel ();
//}
//if (Input.GetKeyDown ("b")) {
//previousLevel ();
//}
// get the menu
// updating score
score = 0;
foreach (Bubbles bubbles in bubblesSetsList) {
score += bubbles.numberBubblesPopped;
}
int i = bubblesSetsList.IndexOf (currentBubblesSet);
//handles the changes of levels inside level 0
if (i< numberLevels - 1 && currentBubblesSet.numberBubbles == currentBubblesSet.numberBubblesPopped){
currentBubblesSet.timeMesured = currentBubblesSet.timeElapsed.ElapsedMilliseconds*0.001f; // timeElapsed.stop() didn't work
nextLevel();
}
if (!endFlag && i == numberLevels - 1 && currentBubblesSet.numberBubbles == currentBubblesSet.numberBubblesPopped){
AudioSource sound = GetComponent<AudioSource> ();
sound.Play ();
currentBubblesSet.timeMesured = currentBubblesSet.timeElapsed.ElapsedMilliseconds*0.001f;
print("Fin du niveau 0");
//this.writeRecordedPosition ();
NextLevel();
endFlag = true;
}
//handles the difficulty of the level
if (currentBubblesSet.timeElapsed.Elapsed.TotalMilliseconds / 1000 > 3) {
if (!currentBubblesSet.changingSize) {
currentBubblesSet.startChangingSize ();
}
}
if (currentBubblesSet.timeElapsed.Elapsed.TotalMilliseconds / 1000 > 15
&& currentBubblesSet.numberBubblesPopped < currentBubblesSet.numberBubbles / 2) {
previousLevel ();
} else {
if (currentBubblesSet.timeElapsed.Elapsed.TotalMilliseconds / 1000 > 15
&& currentBubblesSet.numberBubblesPopped > currentBubblesSet.numberBubbles - 2) {
malusPoints += currentBubblesSet.numberBubbles - currentBubblesSet.numberBubblesPopped;
currentBubblesSet.numberBubblesPopped = currentBubblesSet.numberBubbles; // the next time Update is called, it will go to the next level
} else {
if (currentBubblesSet.timeElapsed.Elapsed.TotalMilliseconds / 1000 > 20
&& currentBubblesSet.numberBubblesPopped < currentBubblesSet.numberBubbles) {
previousLevel ();
}
}
}
}
void resetGame(){
setAllBubblesInactive ();
displayBubbles (bubblesPart1);
endFlag = false;
}
void previousLevel(){
setAllBubblesInactive ();
int i = bubblesSetsList.IndexOf(currentBubblesSet);
if (i > 0) { // if we are in the first level, there is no way of going down a level
displayBubbles ((Bubbles) bubblesSetsList [i - 1]);
currentBubblesSet = (Bubbles) bubblesSetsList [i - 1];
endFlag = false;
}
}
void nextLevel(){
int i = bubblesSetsList.IndexOf(currentBubblesSet);
if (i < numberLevels - 1) {
displayBubbles ((Bubbles) bubblesSetsList [i + 1]);
currentBubblesSet = (Bubbles) bubblesSetsList [i + 1];
}
}
void setAllBubblesInactive(){
foreach (Bubbles bubbles in bubblesSetsList){
bubbles.setBubblesInactive();
}
}
void displayBubbles(Bubbles bubbles){
foreach (Bubbles otherBubbles in bubblesSetsList){
otherBubbles.setBubblesInactive ();
}
bubbles.setBubblesActive ();
}
void writeRecordedPosition(){
String stringRecorded = "";
foreach(Vector4 vector in player.positionRecorded ){
stringRecorded += vector.x;
stringRecorded += " "; // the separator is a space
stringRecorded += vector.y;
stringRecorded += " ";
stringRecorded += vector.z;
stringRecorded += " ";
stringRecorded += vector.w;
stringRecorded += "\n";
}
File.WriteAllText("RecordedPosition.txt", stringRecorded);
String stringTimes = "";
foreach(Bubbles bubblesSet in bubblesSetsList ){
stringTimes += bubblesSet.ToString();
stringTimes += " "; // the separator is a space
stringTimes += bubblesSet.timeMesured.ToString();
stringTimes += "\n";
}
File.WriteAllText("TimesMesured.txt", stringTimes);
}
public void SetGameController(string type)
{
if (type=="leap")
{
player.setGameController(new LeapMotion());
}
else
{
player.setGameController(new Mouse());
}
}
void checkController(){
// Part related to the controller input
if (Input.GetKeyDown("l"))
{ // equivalent to get key L
player.setGameController (new LeapMotion());
print ("LeapMotion is the controller");
}
if (Input.GetKeyDown("m"))
{ // equivalent to get key M
player.setGameController (new Mouse());
print ("Mouse is the controller");
}
}
}