|
13 | 13 |
|
14 | 14 | this.defaultHTML = $('#gameButtons').html();
|
15 | 15 |
|
16 |
| - //variables redefined when the game starts |
17 | 16 | this.loadingState = undefined;
|
18 | 17 | this.questionList = undefined;
|
19 | 18 | this.gameData = undefined;
|
20 | 19 | this.currentQuestion = undefined;
|
21 | 20 | this.currentAnswer = undefined;
|
22 | 21 | this.questionIterator = undefined;
|
23 | 22 |
|
24 |
| - //add light to the scene |
25 | 23 | var pointLight = new THREE.PointLight(0xFFFFFF);
|
26 | 24 | pointLight.position.set(0, 0, 130);
|
27 | 25 | this.scene.add(pointLight);
|
28 | 26 | };
|
29 | 27 |
|
30 |
| - //constants |
31 | 28 | var BUTTON_HTML = '<div id="$refId" class="button" data-logic="$id">$text</div>';
|
32 | 29 | var QUESTION_MOLECULE = 0;
|
33 | 30 | var QUESTION_TEXT = 1;
|
|
40 | 37 | GameScreen.prototype.constructor = GameScreen;
|
41 | 38 |
|
42 | 39 | GameScreen.prototype.onUpdate = function(delta) {
|
43 |
| - //update the this.timer |
44 | 40 | if(this.getSecondsLeft() === 0 && this.currentQuestion !== undefined) {
|
45 | 41 | this.endGame( );
|
46 | 42 | }
|
|
52 | 48 | $('#time').text(Timer.getDigitalRep(this.getSecondsLeft( )));
|
53 | 49 | $('#score').text(this.scoreManager.score);
|
54 | 50 |
|
55 |
| - //update the molecule |
56 | 51 | if(MouseManager.leftButton.isPressed && this.currentQuestion !== undefined) {
|
57 | 52 | this.currentQuestion[QUESTION_MOLECULE].rotation.z -=
|
58 | 53 | (MouseManager.currentX - MouseManager.leftButton.pressedX) / 1000;
|
|
86 | 81 |
|
87 | 82 | GameScreen.prototype.onResume = function( ) {
|
88 | 83 | function receiveQuestionList(data) {
|
89 |
| - /* Receives list of questions */ |
90 | 84 | this.gameData = data;
|
91 | 85 | this.loadingState = -1;
|
92 | 86 | $('#loadingMessage').text('Loading');
|
|
110 | 104 | FCCommunicationManager.loadFlashcardGame(UserData.auth, UserData.gameID, receiveQuestionList.bind(this));
|
111 | 105 | };
|
112 | 106 |
|
| 107 | + GameScreen.prototype.getSecondsLeft = function() { |
| 108 | + var time = this.gameLength - this.timer.getElapsedSec(); |
| 109 | + return (time > 0) ? time : 0; |
| 110 | + }; |
| 111 | + |
113 | 112 | GameScreen.prototype.startGame = function( ) {
|
114 | 113 | $('#beginButton').removeClass('in');
|
115 | 114 | $('#loadingUI').removeClass('in active');
|
|
121 | 120 | this.nextQuestion( );
|
122 | 121 | };
|
123 | 122 |
|
124 |
| - GameScreen.prototype.endGame = function( ) { |
125 |
| - function allowExit(response) { |
126 |
| - $('#finalScore').text('Final Score: ' + response.final_score); |
127 |
| - $('#rank').text('Rank: #' + response.rank); |
128 |
| - $('#gameCompletedUI').addClass('in active'); |
129 |
| - } |
130 |
| - disableButtons( ); |
131 |
| - this.scene.remove(this.currentQuestion[ QUESTION_MOLECULE ]); |
132 |
| - this.currentQuestion = undefined; |
133 |
| - this.timer.stop( ); |
134 |
| - $('#scoreChange') |
135 |
| - .stop(true, true) |
136 |
| - .animate({ |
137 |
| - opacity: 0 |
138 |
| - }, |
139 |
| - 300); |
140 |
| - FCCommunicationManager.endFlashcardGame(UserData.auth, |
141 |
| - this.gameData.game_session_id, |
142 |
| - this.timer.getElapsedMs(), |
143 |
| - allowExit.bind(this)); |
144 |
| - }; |
145 |
| - |
146 |
| - GameScreen.prototype.nextQuestion = function( ) { |
147 |
| - function insertInfo(replacements, templateString, selector) { |
148 |
| - var result = templateString; |
149 |
| - for(var key in replacements) { |
150 |
| - result = result.replace(key, replacements[key]); |
151 |
| - } |
152 |
| - |
153 |
| - $(selector).append(result); |
154 |
| - } |
155 |
| - |
156 |
| - function setQuestionText(questionText) { |
157 |
| - if(questionText) { |
158 |
| - $('#questionPanel').text(questionText); |
159 |
| - $('#questionPanel').addClass('in'); |
160 |
| - } else { |
161 |
| - $('#questionPanel').empty(); |
162 |
| - $('#questionPanel').removeClass('in'); |
163 |
| - } |
164 |
| - } |
165 |
| - |
166 |
| - function setButtons(answers) { |
167 |
| - $('#gameButtons').empty(); |
168 |
| - answers.forEach(function(answer) { |
169 |
| - insertInfo({ |
170 |
| - '$refId': answer.id, |
171 |
| - '$id': answer.id, |
172 |
| - '$text': answer.text |
173 |
| - }, |
174 |
| - BUTTON_HTML, '#gameButtons'); |
175 |
| - }); |
176 |
| - } |
177 |
| - |
178 |
| - this.userAnswers = new Map( ); |
179 |
| - if(this.questionIterator.hasNext( )) { |
180 |
| - if(this.questionIterator.index !== -1) { |
181 |
| - this.scene.remove(this.currentQuestion[ QUESTION_MOLECULE ]); |
182 |
| - } |
183 |
| - this.currentQuestion = this.questionIterator.next( ); |
184 |
| - this.scene.add(this.currentQuestion[ QUESTION_MOLECULE ]); |
185 |
| - setQuestionText(this.currentQuestion[ QUESTION_TEXT ]); |
186 |
| - setButtons(this.currentQuestion[ QUESTION_ANSWERS ]); |
187 |
| - } else { |
188 |
| - this.endGame( ); |
189 |
| - } |
190 |
| - }; |
191 |
| - |
192 | 123 | GameScreen.prototype.createPDB = function(data) {
|
193 | 124 | /* Pulls in PDB's for each question and builds them */
|
194 | 125 | this.loadingState++;
|
|
247 | 178 | }
|
248 | 179 | };
|
249 | 180 |
|
250 |
| - GameScreen.prototype.getSecondsLeft = function() { |
251 |
| - var time = this.gameLength - this.timer.getElapsedSec(); |
252 |
| - return (time > 0) ? time : 0; |
| 181 | + GameScreen.prototype.nextQuestion = function( ) { |
| 182 | + function insertInfo(replacements, templateString, selector) { |
| 183 | + var result = templateString; |
| 184 | + for(var key in replacements) { |
| 185 | + result = result.replace(key, replacements[key]); |
| 186 | + } |
| 187 | + |
| 188 | + $(selector).append(result); |
| 189 | + } |
| 190 | + |
| 191 | + function setQuestionText(questionText) { |
| 192 | + if(questionText) { |
| 193 | + $('#questionPanel').text(questionText); |
| 194 | + $('#questionPanel').addClass('in'); |
| 195 | + } else { |
| 196 | + $('#questionPanel').empty(); |
| 197 | + $('#questionPanel').removeClass('in'); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + function setButtons(answers) { |
| 202 | + $('#gameButtons').empty(); |
| 203 | + answers.forEach(function(answer) { |
| 204 | + insertInfo({ |
| 205 | + '$refId': answer.id, |
| 206 | + '$id': answer.id, |
| 207 | + '$text': answer.text |
| 208 | + }, |
| 209 | + BUTTON_HTML, '#gameButtons'); |
| 210 | + }); |
| 211 | + } |
| 212 | + |
| 213 | + this.userAnswers = new Map( ); |
| 214 | + if(this.questionIterator.hasNext( )) { |
| 215 | + if(this.questionIterator.index !== -1) { |
| 216 | + this.scene.remove(this.currentQuestion[ QUESTION_MOLECULE ]); |
| 217 | + } |
| 218 | + this.currentQuestion = this.questionIterator.next( ); |
| 219 | + this.scene.add(this.currentQuestion[ QUESTION_MOLECULE ]); |
| 220 | + setQuestionText(this.currentQuestion[ QUESTION_TEXT ]); |
| 221 | + setButtons(this.currentQuestion[ QUESTION_ANSWERS ]); |
| 222 | + } else { |
| 223 | + this.endGame( ); |
| 224 | + } |
253 | 225 | };
|
254 | 226 |
|
255 | 227 | GameScreen.prototype.answerQuestion = function(data) {
|
|
279 | 251 | );
|
280 | 252 | };
|
281 | 253 |
|
| 254 | + GameScreen.prototype.endGame = function( ) { |
| 255 | + function allowExit(response) { |
| 256 | + $('#finalScore').text('Final Score: ' + response.final_score); |
| 257 | + $('#rank').text('Rank: #' + response.rank); |
| 258 | + $('#gameCompletedUI').addClass('in active'); |
| 259 | + } |
| 260 | + disableButtons( ); |
| 261 | + this.scene.remove(this.currentQuestion[ QUESTION_MOLECULE ]); |
| 262 | + this.currentQuestion = undefined; |
| 263 | + this.timer.stop( ); |
| 264 | + $('#scoreChange') |
| 265 | + .stop(true, true) |
| 266 | + .animate({ |
| 267 | + opacity: 0 |
| 268 | + }, |
| 269 | + 300); |
| 270 | + FCCommunicationManager.endFlashcardGame(UserData.auth, |
| 271 | + this.gameData.game_session_id, |
| 272 | + this.timer.getElapsedMs(), |
| 273 | + allowExit.bind(this)); |
| 274 | + }; |
| 275 | + |
282 | 276 | function enableButtons(gameScreen) {
|
283 | 277 | $('#gameUI .button[data-logic=\'return\']').on('click', function() {
|
284 | 278 | $(this).trigger(new ScreenChangeEvent('menu'));
|
|
0 commit comments