Guest User

Untitled

a guest
Jul 17th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. function ShakeIt(config) {
  2. this.config = config;
  3. this.scores_page = config["scores_page"];
  4. this.game_page = config["game_page"];
  5. this.goal = config["goal"];
  6. this.db = config["db"];
  7.  
  8. this.score = 0;
  9. this.old_axis = 0;
  10. this.game_started = false;
  11. this.game_start_time;
  12. this.start_button;
  13. this.shake_db;
  14. this.power_size = 0;
  15. }
  16.  
  17. ShakeIt.prototype = {
  18. game_abort: function() {
  19. $(".ui-btn-text", this.start_button).text("Start");
  20. this.game_started = false;
  21. window.removeEventListener("devicemotion", onMotion);
  22. window.clearInterval(this.updateGameScore);
  23. },
  24. game_finish: function() {
  25. $(".ui-btn-text", this.start_button).text("Start");
  26. this.game_started = false;
  27. window.removeEventListener("devicemotion", onMotion);
  28. window.clearInterval(this.updateGameScore);
  29. this.shake_db.insertScore(new Date().getTime() - this.game_start_time, this.getFormatedNowTime());
  30. },
  31. game_start: function() {
  32. this.play_sound();
  33. $(".ui-btn-text", this.start_button).text("Cancel");
  34. this.game_started = true;
  35. this.score = 0;
  36. this.updateGameScore();
  37. this.updateGamePower();
  38. this.game_start_time = new Date().getTime();
  39. window.addEventListener("devicemotion", onMotion);
  40. var that = this;
  41. window.setInterval(function() { that.updateGamePower(); }, 200);
  42. },
  43. refreshScores: function() {
  44. var that = this;
  45. this.shake_db.loadScores(function(tx, rs) { that.renderScores(tx, rs); });
  46. },
  47.  
  48. // <fieldset class="ui-grid-a">
  49. // <div class="ui-block-a"><div class="ui-bar ui-bar-e">10:02"</div></div>
  50. // <div class="ui-block-b"><div class="ui-bar ui-bar-e">2010-10-20 20:30</div></div>
  51. // </fieldset>
  52. renderScores: function(tx, rs) {
  53. e = $("div[data-role=content]", this.scores_page);
  54. e.empty();
  55.  
  56. var r;
  57. console.dir(this);
  58. console.dir(self);
  59. e.append(this.renderScoreTitle());
  60. for (var i=0; i < rs.rows.length; i++) {
  61. r = rs.rows.item(i);
  62. e.append(this.renderScore(r['score'], r['created_at']));
  63. }
  64. },
  65. renderScoreTitle: function() {
  66. var template = '<div class="ui-bar">' +
  67. '<span class="time"/>' +
  68. '<span class="created_at"/>' +
  69. '</div>';
  70. var fieldset = $(template).find(".time").text("Time").end().find(".created_at").text("Date").end();
  71. return fieldset;
  72. },
  73. renderScore: function(score, created_at) {
  74. //var template = '<fieldset class="ui-grid-a">' +
  75. // '<div class="ui-block-a"><div class="ui-bar ui-bar-e time"></div></div>' +
  76. // '<div class="ui-block-b"><div class="ui-bar ui-bar-e created_at"></div></div>' +
  77. //'</fieldset>'
  78. var template = '<div class="ui-bar ui-bar-e">' +
  79. '<span class="time"/>' +
  80. '<span class="created_at"/>' +
  81. '</div>';
  82. var fieldset = $(template).find(".time").text(this.formatedScore(score)).end().find(".created_at").text(created_at).end();
  83. return fieldset;
  84. },
  85. formatedScore: function(score) {
  86. var sec = parseInt(score / 1000, 10) || 0;
  87. var msec = parseInt(score % 1000, 10) || 0;
  88. return sec + "'" + msec;
  89. },
  90. getFormatedNowTime: function() {
  91. var now = new Date();
  92. return now.getFullYear() + "/" + now.getMonth() + "/" + now.getDate() + " " + now.getHours() + ":" + now.getMinutes();
  93. },
  94. updateGameScore: function() {
  95. var score_width = (this.score / this.goal * 100) || 0
  96. $(".score", this.game_page).text(this.score).css("width", score_width.toString() + "%");
  97. },
  98. updateGamePower: function() {
  99. var color = RGB(0, this.power_size, 0);
  100. $(this.game_page).css("backgroundImage", "-moz-linear-gradient(center top," + color + ",#222222)");
  101. $(this.game_page).css("backgroundImage", "-webkit-gradient(linear,left top,left bottom,color-stop(0," + color + "),color-stop(1, #222222))");
  102. $(this.game_page).css("backgroundColor", color);
  103. },
  104. play_sound: function() {
  105. // var sound = $("#audio").get(0);
  106. // sound.play();
  107. var sound = new Audio();
  108. sound.src = "pong.mp3";
  109. sound.load();
  110. sound.play();
  111. },
  112.  
  113. react: function(isPunch, evt) {
  114. if (isPunch) {
  115. this.score++;
  116. this.updateGameScore();
  117. console.debug(this.score);
  118. console.debug(this.goal);
  119.  
  120. if (this.score >= this.goal) {
  121. this.game_finish();
  122. return;
  123. }
  124. this.power_size += 30;
  125. if (this.power_size > 255) { this.power_size = 255; }
  126. } else {
  127. this.power_size -= 5;
  128. if (this.power_size < 0) { this.power_size = 0; }
  129. }
  130. },
  131.  
  132. game_init: function() {
  133. this.shake_db = new ShakeDB();
  134. this.shake_db.init();
  135.  
  136. console.debug(this);
  137.  
  138. var that = this;
  139. $(this.game_page).bind("pageshow", function(event, ui) {
  140. that.updateGameScore();
  141. } ).bind("pagehide", function(event, ui) {
  142. that.game_abort();
  143. } );
  144. this.start_button = $("#start_game", this.game_page);
  145. this.start_button.bind("click", function() {
  146. if (that.game_started) {
  147. that.game_abort();
  148. } else {
  149. that.game_start();
  150. }
  151. } );
  152.  
  153. $(this.scores_page).bind("pagebeforeshow", function(event, ui) {
  154. console.debug("pagebeforeshow-scores");
  155. console.dir(that);
  156. that.refreshScores();
  157. } ).bind("pagebeforecreate", function(event, ui) {
  158. that.refreshScores();
  159. } );
  160.  
  161. $("a[data-icon=delete]", this.scores_page).bind("click", function(event) {
  162. that.shake_db.clearScores(function() { that.refreshScores(); });
  163. } );
  164. }
  165. };
Add Comment
Please, Sign In to add comment