2 * Copyright (c) 2012, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
16 var bowling_msg3 = [];
22 /* the game data object for this game */
25 this.pinpos = [53, 96, 140, 183, 75, 118, 162, 96, 140, 118];
26 this.throws = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]];
27 this.answers = [true, true, true, true, true];
30 this.gamethrow = function gamethrow(t) {
34 var throwidx = (t == undefined)?this.throwidx:t;
35 var gameidx = this.gameidx;
36 return this.throws[gameidx][throwidx];
38 this.getthrow = function getthrow(g, t) {
39 if((g >= 5)||(t >= 2))
42 var throwidx = (t == undefined)?this.throwidx:t;
43 var gameidx = (g == undefined)?this.gameidx:g;
44 return this.throws[gameidx][throwidx];
46 this.setthrow = function setthrow(val) {
50 this.throws[this.gameidx][this.throwidx] = val;
52 this.nextthrow = function nextthrow() {
56 this.gameidx += this.throwidx;
57 this.throwidx = (this.throwidx+1)%2;
62 /* called to close the app and repoen the home page */
65 $("#bowling_page").hide();
66 $("#bowling_win_page").hide();
67 $("#game_menu_border").hide();
68 $("#home_page").show();
72 /* called to stop any delayed events and return the app to starting position */
74 $("#bowling_win_page").hide();
76 clearTimeout(Pirates.timerstart);
80 $("#bowling_inset n").html(val.toLocaleString());
83 /* called to start or restart the game (from home menu or game menu) */
86 data = new GameData();
87 $("#home_page").hide();
88 $("#bowling_page").show();
89 sounds.present.play();
93 function showWinPage() {
95 for(var i = 1; i <= 5; i++)
97 var id = "#bowling_mini"+i;
101 $(id).removeClass("red");
102 $(id).addClass("green");
106 $(id).removeClass("green");
107 $(id).addClass("red");
110 var t1 = data.getthrow(i-1, 0);
111 var t2 = data.getthrow(i-1, 1);
115 for(j = 1; j <= t1; j++)
116 $(id).append("<div class=\"bowlingpinmicro m1p"+j+"\"></div>");
117 for(j = 1; j <= t2; j++)
118 $(id).append("<div class=\"bowlingpinmicro m2p"+j+"\"></div>");
119 for(j = 1; j <= s; j++)
120 $(id).append("<div class=\"bowlingpinmicro m3p"+j+"\"></div>");
122 "<div class=\"bowling_mval v1\">"+t1.toLocaleString()+"</div>");
124 "<div class=\"bowling_mval v2\">"+t2.toLocaleString()+"</div>");
126 "<div class=\"bowling_mval v3\">"+s.toLocaleString()+"</div>");
131 $("#bowling_msg3").html(bowling_msg3[0]);
135 $("#bowling_msg3").html(bowling_msg3[1]);
136 $("#bowling_msg3 c").html(correct.toLocaleString());
140 $("#bowling_msg3").html(bowling_msg3[2]);
142 $("#bowling_win_page").show();
143 sounds.present.play();
146 /* called to calculate what pins are hit based on ball position at roll */
147 /* and what direction they fly based on where the ball hit */
148 function hitPins(rollpos, hitpos) {
152 else if(rollpos > 270)
154 else if(rollpos > 245)
156 else if(rollpos > 230)
158 else if(rollpos > 214)
160 else if(rollpos > 202)
161 pins = [3, 4, 6, 7, 9];
162 else if(rollpos > 190)
163 pins = [2, 3, 4, 6, 7, 9];
164 else if(rollpos > 178)
165 pins = [2, 3, 4, 6, 7, 9, 10];
166 else if(rollpos > 165)
167 pins = [2, 3, 4, 6, 7, 8, 9, 10];
168 else if(rollpos > 155)
169 pins = [2, 3, 4, 5, 6, 7, 8, 9, 10];
170 else if(rollpos > 145)
171 pins = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
172 else if(rollpos > 140)
173 pins = [2, 3, 4, 5, 6, 7, 8, 9, 10];
174 else if(rollpos > 135)
175 pins = [2, 3, 4, 6, 7, 8, 9, 10];
176 else if(rollpos > 130)
177 pins = [2, 3, 6, 7, 8, 9, 10];
178 else if(rollpos > 120)
179 pins = [2, 3, 6, 8, 9, 10]; // dead center
180 else if(rollpos > 115)
181 pins = [2, 3, 5, 6, 8, 9, 10];
182 else if(rollpos > 110)
183 pins = [1, 2, 3, 5, 6, 8, 9, 10];
184 else if(rollpos > 105)
185 pins = [1, 2, 3, 5, 6, 7, 8, 9, 10];
186 else if(rollpos > 95)
187 pins = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
188 else if(rollpos > 85)
189 pins = [1, 2, 3, 5, 6, 7, 8, 9, 10];
190 else if(rollpos > 72)
191 pins = [1, 2, 3, 5, 6, 8, 9, 10];
192 else if(rollpos > 60)
193 pins = [1, 2, 3, 5, 6, 8, 10];
194 else if(rollpos > 48)
195 pins = [1, 2, 3, 5, 6, 8];
196 else if(rollpos > 36)
197 pins = [1, 2, 5, 6, 8];
198 else if(rollpos > 20)
202 else if(rollpos > -20)
204 else if(rollpos > -66)
209 data.setthrow(pins.length);
210 for(var i = 0; i < pins.length; i++)
212 if(hitpos > data.pinpos[i])
213 $("#bowling_pin"+pins[i]).addClass("flyleft");
215 $("#bowling_pin"+pins[i]).addClass("flyright");
219 /* triggered when the ball rolling animation is finished */
220 function ballDoneRolling() {
224 $("#bowling_ball_rolling").css("opacity", "0");
225 $("#bowling_ball_rolling").css("-webkit-transition", "-webkit-transform 0.1s linear");
226 $("#bowling_ball_rolling").css("-webkit-transform", "translate(0px, 0px) scale(1)");
227 var val = data.getthrow();
228 if(data.throwidx == 0)
230 $("#bowling_val1").html(val.toLocaleString());
231 $("#bowling_msg2 n").html(val.toLocaleString());
232 $("#bowling_page").addClass("state1");
233 timerroll = setTimeout(nextRoll, 1500);
234 var gval = data.gameidx+1;
235 $("#bowling_inset n").html(gval.toLocaleString());
239 $("#bowling_val2").html(val.toLocaleString());
240 $("#bowling_page").addClass("state2");
242 for(var i = 1; i <= val; i++)
244 var id = "#bowling_t"+(data.throwidx + 1)+"p"+i;
245 $(id).addClass("show");
250 /* called when any watched element's animation ends */
251 function webkitAnimationEnd(e) {
252 if(e&&e.srcElement&&e.srcElement.id)
254 var id = e.srcElement.id;
255 /* bowling pin animation has ended, hide it */
256 if(id.startsWith("bowling_pin"))
263 /* called when any watched element's animation starts */
264 function webkitAnimationStart(e) {
265 if(e&&e.srcElement&&e.srcElement.id)
267 var id = e.srcElement.id;
268 /* bowling pin animation has started, hide its shadow */
269 if(id.startsWith("bowling_pin"))
271 $("#"+id+"shadow").hide();
276 function rollReset() {
277 for(var i = 1; i <= 10; i++)
279 $("#bowling_pin"+i).removeClass("flyleft flyright");
280 $("#bowling_pin"+i).show();
281 $("#bowling_pin"+i+"shadow").show();
283 $("#bowling_rollbutton").removeClass("active");
284 $("#bowling_ball").show();
287 /* called to reset the bowling lane and wait for a new throw */
288 function nextRoll() {
296 function frameReset() {
297 $("#bowling_page").removeClass("state1 state2 state3");
298 $("#bowling_val1").empty();
299 $("#bowling_val2").empty();
300 for(var j = 1; j <= 2; j++)
301 for(var i = 1; i <= 10; i++)
303 var id = "#bowling_t"+j+"p"+i;
304 $(id).removeClass("show");
308 /* called on ROLL THE BALL button click */
310 if($("#bowling_rollbutton").hasClass("active"))
314 $("#bowling_rollbutton").addClass("active");
315 var pos = parseInt($("#bowling_ball").css("left"));
316 $("#bowling_ball").hide();
317 $("#bowling_ball_rolling").css("left", (pos-12)+"px");
318 var dx = Math.floor(((110-pos)/125)*61);
319 $("#bowling_ball_rolling").css("opacity", "1");
320 $("#bowling_ball_rolling").css("-webkit-transition", "-webkit-transform 1.3s linear");
321 $("#bowling_ball_rolling").css("-webkit-transform", "translate("+dx+"px, -224px) scale(0.4)");
322 hitPins(pos, pos+dx);
323 if((pos > 48)&&(pos <= 190))
324 sounds.rollmany.play();
325 else if((pos > -66)&&(pos <= 316))
326 sounds.rollfew.play();
328 sounds.gutterball.play();
331 function bowlingAnswer(val) {
332 var idx = data.gameidx;
333 var correct = data.gamethrow(0) + data.gamethrow(1);
336 $("#bowling_dialog").removeClass("incorrect");
337 $("#bowling_dialog").addClass("correct");
338 sounds.correct.play();
339 data.answers[idx] = true;
343 $("#bowling_dialog").removeClass("correct");
344 $("#bowling_dialog").addClass("incorrect");
345 sounds.incorrect.play();
346 data.answers[idx] = false;
348 var gleft = 5 - idx - 1;
351 $("#bowling_correct").html(bowling_correctp);
352 $("#bowling_correct n").html(gleft.toLocaleString());
355 $("#bowling_correct").html(bowling_corrects);
357 $("#bowling_correct").html(bowling_correctz);
358 $("#bowling_page").addClass("state3");
365 /* load up the pins in the bowling scorecard */
366 $("#bowling_scoregrid").empty();
367 for(var i = 0; i < 20; i++)
369 var pin = "t"+(Math.floor(i/10)+1)+"p"+((i%10)+1);
370 h += "<div id=\"bowling_"+pin+"\" class=\"bowlingpinmini\"></div>";
372 $("#bowling_scoregrid").html(h);
374 /* load up the number buttons in the answer tray */
375 $("#bowling_numbertray").empty();
376 for(var i = 0; i <= 20; i++)
378 h += "<div id=\"bowling_numbtn"+i+"\" class=\"bowlingnumbtn\">"+i+"</div>"
380 $("#bowling_numbertray").html(h);
381 $(".bowlingnumbtn").click(function(){
382 bowlingAnswer(parseInt(this.id.slice(14)));
388 setTimeout(loadHtml, 0);
389 bowling_correctp = "<b>Great job!</b><br>You chose the right answer!"+
390 "<br>You have <n></n> more games to go.";
391 bowling_corrects = "<b>Great job!</b><br>You chose the right answer!"+
392 "<br>You have <n>1</n> more game to go.";
393 bowling_correctz = "<b>Great job!</b><br>You chose the right answer!"+
394 "<br>You're done, continue to see your score.";
395 bowling_msg3[0] = "<b>Great job!</b><br>"+
396 "You chose the correct answers for all <b>5</b> games.";
397 bowling_msg3[1] = "<b>Pretty good!</b><br>"+
398 "You chose the correct answers for <c></c> of 5 games.";
399 bowling_msg3[2] = "<b>Oops!</b><br>"+
400 "You chose the wrong answers.<br>Don't worry, you'll do better next time.";
401 if (window.chrome&&window.chrome.i18n)
403 $("#bowling_rollbutton").html(chrome.i18n.getMessage("bowling_rollbutton"));
404 $("#bowling_msg1").html(chrome.i18n.getMessage("bowling_msg1"));
405 $("#bowling_msg2").html(chrome.i18n.getMessage("bowling_msg2"));
406 $("#bowling_msg3").html(chrome.i18n.getMessage("bowling_msg3"));
407 $("#bowling_inset").html(chrome.i18n.getMessage("bowling_inset"));
408 $("#bowling_label1").html(chrome.i18n.getMessage("bowling_label1"));
409 $("#bowling_label2").html(chrome.i18n.getMessage("bowling_label2"));
410 $("#bowling_label3").html(chrome.i18n.getMessage("bowling_label3"));
411 $("#bowling_correct").html(chrome.i18n.getMessage("bowling_correctp"));
412 bowling_correctp = chrome.i18n.getMessage("bowling_correctp");
413 bowling_corrects = chrome.i18n.getMessage("bowling_corrects");
414 bowling_correctz = chrome.i18n.getMessage("bowling_correctz");
415 $("#bowling_incorrect").html(chrome.i18n.getMessage("bowling_incorrect"));
416 $("#bowling_next").html(chrome.i18n.getMessage("bowling_next"));
417 $("#bowling_tryagain").html(chrome.i18n.getMessage("bowling_tryagain"));
418 $("#bowling_quit").html(chrome.i18n.getMessage("bowling_quit"));
422 var elem = document.getElementById('bowling_ball_rolling');
423 elem.addEventListener('webkitTransitionEnd', ballDoneRolling, false);
424 for(var i = 1; i <= 10; i++)
426 elem = document.getElementById("bowling_pin"+i);
427 elem.addEventListener('webkitAnimationEnd', webkitAnimationEnd, false);
428 elem.addEventListener('webkitAnimationStart', webkitAnimationStart, false);
430 sounds.gutterball = new GameSound("audio/Bowling_BallMove.ogg", 1);
431 sounds.rollmany = new GameSound("audio/Bowling_KnockOverPins_01.ogg", 1);
432 sounds.rollfew = new GameSound("audio/Bowling_KnockOverPins_02.ogg", 1);
433 sounds.present = new GameSound("audio/Bowling_PresentAnswer.ogg", 1);
434 sounds.correct = new GameSound("audio/Bowling_CorrectAnswer.ogg", 1);
435 sounds.incorrect = new GameSound("audio/Bowling_IncorrectAnswer.ogg", 1);
436 $("#bowling_rollbutton").click(function(){roll();});
437 $("#bowling_quit").click(close);
438 $("#bowling_tryagain").click(function(){
439 $("#bowling_page").removeClass("state3");
441 $("#bowling_next").click(function(){
445 $("#bowling_win_page").click(function(){