pop.router.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. POP.router = function() {
  2. window.scrollTo(0,1);
  3. POP.timer = new Date().getTime() * 0.002;
  4. POP.draw.clear();
  5. switch(POP.state) {
  6. case 'splash':
  7. POP.splash();
  8. break;
  9. case 'gameOver':
  10. POP.gameOver();
  11. break;
  12. case 'hiScores':
  13. POP.hiScores();
  14. break;
  15. default:
  16. POP.play();
  17. break;
  18. }
  19. POP.m.click = false;
  20. POP.stats.update();
  21. requestAnimFrame(POP.router);
  22. };
  23. POP.splash = function() {
  24. var opacity = Math.abs(Math.sin(POP.timer * 0.8)),
  25. col = 'rgba(255,255,255,'+opacity+')';
  26. POP.delay += 1;
  27. /*
  28. if (POP.ua.appleMobile === true && window.navigator.standalone === false) {
  29. POP.draw.rect(0,0,POP.W, 30, 'rgba(0,0,0,0.2)');
  30. POP.draw.text(40,20, 'Psst! Save to homepage for full screen fun!', 10, '#fff');
  31. } else if (POP.Ua.hasTouch === false) {
  32. POP.draw.text(20,POP.H - 20, 'This game is much more fun on touch devices', 11, 'rgba(255,255,255,0.5)');
  33. }*/
  34. POP.draw.text(45,135,'戳泡泡!',64, 'rgba(0,0,0,0.3)');
  35. POP.draw.text(40,130,'戳泡泡!',64);
  36. POP.draw.text(110,240, POP.Ua.action + '开始游戏', 16, col);
  37. POP.draw.text(128, 340, '最高分', 20, 'rgba(255,255,255,0.8)');
  38. POP.draw.text("center", 380, POP.hiScore.toString(), 24, 'rgba(255,255,255,0.8)');
  39. if (POP.m.click && POP.delay > 20) {
  40. POP.textLayer.style.display = 'none';
  41. POP.delay = 0;
  42. POP.touches = [];
  43. POP.state = 'play';
  44. POP.gameStart = new Date().getTime();
  45. }
  46. };
  47. POP.gameOver = function() {
  48. var opacity = Math.abs(Math.sin(POP.timer * 0.8)),
  49. col = 'rgba(255,255,255,'+opacity+')',
  50. col2 = 'rgba(172,255,4,1)';
  51. POP.delay += 1;
  52. POP.draw.text(40,90,'Game Over',40, col);
  53. // display scores
  54. POP.draw.text(115,160,'泡泡数: '+POP.score.burst, 16, '#fff');
  55. POP.draw.text(115,190,'精准度: ' + POP.score.accuracy + '%', 16, '#fff');
  56. POP.draw.text(131,220,'时间: ' + POP.score.seconds, 16, '#fff');
  57. POP.draw.text("center",270,'' + POP.score.total, 30, '#fff');
  58. if (POP.newHiscore) {
  59. POP.draw.text('center', 310, '新记录!!!', 20, col2);
  60. // POP.draw.text('center', 320, POP.score.total.toString(), 20, col2);
  61. }
  62. // 绘画更多游戏
  63. //POP.draw.rect(10, 330, 50, 20, "#fff")
  64. POP.inputBox.style.display = "block";
  65. // POP.state = 'splash'; 就是重新开始游戏
  66. if (POP.m.click && POP.delay > 100) {
  67. POP.againGame();
  68. }
  69. if(!POP.isEndGame){
  70. POP.isEndGame = true;
  71. //btGame.setShare({title: "戳泡泡戳上瘾了,拿了"+POP.score.total+"分。你要不要来爽一把?"});
  72. //btGame.playScoreMsg("泡泡克星就是你!拿了"+POP.score.total+"分,不去炫耀一下吗?");
  73. dp_submitScore(POP.score.total);
  74. }
  75. };
  76. POP.isEndGame = false;
  77. POP.againGame = function(){
  78. /*POP.isEndGame = false;
  79. POP.score.taps =
  80. POP.score.burst =
  81. POP.score.escapees =
  82. POP.score.accuracy =
  83. POP.score.seconds = 0;
  84. POP.newHiscore = false;
  85. POP.lives = POP.MAX_LIVES;
  86. POP.level = 1;
  87. POP.delay = 0;
  88. POP.gameStart = null;
  89. POP.inputBox.style.display = 'none';
  90. POP.state = 'splash';*/
  91. location.reload();
  92. }
  93. POP.hiScores = function() {
  94. if (POP.m.click) {
  95. POP.state = 'splash';
  96. }
  97. POP.draw.text('center',50,'Hi-Scores',40, '#fff');
  98. };
  99. POP.play = function() {
  100. var i, accuracy, live_col, bubble_speed;
  101. POP.secsElapsed = ( new Date().getTime() - POP.gameStart ) / 1000;
  102. POP.level = ~~(POP.secsElapsed / 10) + 1;
  103. if (POP.nextBubble < 0) {
  104. POP.nextBubble = POP.rnd(30 - POP.level, 10);
  105. bubble_speed = POP.rnd(5 + POP.level) * -1;
  106. POP.bubbles.push(new POP.Bubble());
  107. }
  108. POP.nextBubble--;
  109. if (POP.m.click === true) {
  110. POP.score.taps += 1;
  111. POP.touches.push( new POP.Touch(POP.m.x, POP.m.y) );
  112. }
  113. // render bonus
  114. POP.bonus.render();
  115. if (POP.bonus.collides()) {
  116. switch (POP.bonus.type.action) {
  117. case 'green':
  118. POP.lives = POP.MAX_LIVES;
  119. break;
  120. case 'red':
  121. for (i = 0; i < POP.bubbles.length; i += 1) {
  122. POP.bubbles[i].burst();
  123. }
  124. break;
  125. case 'black':
  126. POP.lives -= 1;
  127. break;
  128. }
  129. POP.bonus.reset();
  130. }
  131. // render all touches
  132. for (i = 0; i < POP.touches.length; i += 1) {
  133. POP.touches[i].render();
  134. if (POP.touches[i].remove) {
  135. POP.touches.splice(i, 1);
  136. }
  137. }
  138. // render all bubbles
  139. for (i = 0; i < POP.bubbles.length; i += 1) {
  140. POP.bubbles[i].move().render().checkCollision();
  141. if (POP.bubbles[i].remove) {
  142. POP.bubbles.splice(i, 1);
  143. }
  144. }
  145. // render all explosions
  146. for (i = 0; i < POP.explosions.length; i += 1) {
  147. POP.explosions[i].move();
  148. if (POP.explosions[i].finished) {
  149. POP.explosions.splice(i, 1);
  150. }
  151. }
  152. // display crappy wave effect
  153. for (i = 0; i < POP.wave.total; i++) {
  154. var x_offset = Math.sin(POP.timer * 0.8) * 10;
  155. POP.draw.circle(
  156. POP.wave.x + x_offset + (i * POP.wave.r),
  157. POP.wave.y,
  158. POP.wave.r,
  159. '#fff');
  160. }
  161. // calculate score
  162. POP.score.seconds = ~~(( new Date().getTime() - POP.gameStart ) / 1000);
  163. POP.score.total = ~~( POP.score.seconds + (POP.score.burst * 10) );
  164. POP.draw.text(20,40, POP.score.total.toString(), 22, '#fff');
  165. // draw lives
  166. for (i = 1; i <= POP.MAX_LIVES; i++) {
  167. live_col = (i > POP.lives) ? 'rgba(0,0,0,0.7)' : 'rgba(173,255,4,0.7)';
  168. POP.draw.circle( (i * 25) + (POP.W - 100), 30, 10, live_col);
  169. }
  170. if (POP.lives <= 0) {
  171. POP.bubbles = [];
  172. POP.touches = [];
  173. POP.explosions = [];
  174. POP.state = 'gameOver';
  175. POP.secsElapsed = 0;
  176. POP.level = 0;
  177. POP.bonus.reset();
  178. POP.textLayer.style.display = 'block';
  179. // calculate final score
  180. POP.score.accuracy = ~~((POP.score.burst / POP.score.taps) * 100);
  181. if (POP.score.accuracy > 100) {
  182. POP.score.accuracy = 100;
  183. }
  184. POP.score.accuracy = (POP.score.burst === 0) ? 0 :POP.score.accuracy;
  185. POP.score.seconds = ~~(( new Date().getTime() - POP.gameStart ) / 1000);
  186. POP.score.total = ~~(POP.score.burst * 10) +
  187. (POP.score.seconds * 1);
  188. if (POP.score.total > POP.hiScore) {
  189. POP.newHiscore = true;
  190. POP.hiScore = POP.score.total;
  191. try{
  192. localStorage.POP_hiScore = POP.score.total;
  193. }catch(e){
  194. console.log(e);
  195. }
  196. }
  197. }
  198. };