engine.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. (function() {
  2. var lastTime = 0;
  3. var vendors = ['webkit', 'moz'];
  4. for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  5. window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
  6. window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']
  7. }
  8. if (!window.requestAnimationFrame) {
  9. window.requestAnimationFrame = function(callback, element) {
  10. var currTime = new Date().getTime();
  11. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  12. var id = window.setTimeout(function() {
  13. callback(currTime + timeToCall)
  14. },
  15. timeToCall);
  16. lastTime = currTime + timeToCall;
  17. return id
  18. }
  19. }
  20. if (!window.cancelAnimationFrame) {
  21. window.cancelAnimationFrame = function(id) {
  22. clearTimeout(id)
  23. }
  24. }
  25. } ());
  26. var TOOL;
  27. var STORAGE;
  28. var CONTROL;
  29. var ENGINE;
  30. var GAME;
  31. $(function() {
  32. TOOL = new Tool();
  33. STORAGE = new Storage();
  34. CONTROL = new Control();
  35. ENGINE = new Engine();
  36. GAME = new Game();
  37. var e = $(".engine");
  38. var top = (CLIENT_H - e.height()) / 4;
  39. e.css("marginTop", top);
  40. $(".modal-dialog").css("marginTop", top * 2);
  41. if (TOOL.isPC) {}
  42. });
  43. function Storage() {
  44. this.path = "";
  45. this.storage = window.localStorage
  46. }
  47. Storage.prototype.init = function(path) {
  48. this.path = path
  49. };
  50. Storage.prototype.getInt = function(key) {
  51. return parseInt(this.storage.getItem(this.path + key) || 0)
  52. };
  53. Storage.prototype.getStr = function(key) {
  54. return (this.storage.getItem(this.path + key) || "")
  55. };
  56. Storage.prototype.save = function(key, value) {
  57. this.storage.setItem(this.path + key, value)
  58. };
  59. Storage.prototype.getJSON = function(key) {
  60. var value = this.storage.getItem(this.path + key);
  61. return (value ? JSON.parse(value) : null)
  62. };
  63. Storage.prototype.saveJSON = function(key, value) {
  64. this.storage.setItem(this.path + key, JSON.stringify(value))
  65. };
  66. Storage.prototype.erase = function(key) {
  67. this.storage.removeItem(this.path + key)
  68. };
  69. function Control() {
  70. CLICK_EVENT = "click";
  71. $(document).on("keydown", this, this.keyDownDelegate)
  72. }
  73. Control.prototype.clickOn = function(o, f) {
  74. $(o).on(CLICK_EVENT, f)
  75. };
  76. Control.prototype.touchOn = function(o) {
  77. $(o).on(TOOL.isPC ? "mousedown": "touchstart", this, this.touchStartDelegate);
  78. $(o).on(TOOL.isPC ? "mouseup": "touchend", this, this.touchEndDelegate)
  79. };
  80. Control.prototype.keyDownDelegate = function(e) {
  81. var c = e.data;
  82. c.keyDown(e)
  83. };
  84. Control.prototype.keyDown = function(event) {};
  85. Control.prototype.touchStartDelegate = function(e) {
  86. var t = TOOL.isPC ? e: e.originalEvent.touches[0];
  87. var c = e.data;
  88. c.touchStartX = t.pageX;
  89. c.touchStartY = t.pageY;
  90. c.touchStart(e);
  91. c.checkTouchPrevent(e)
  92. };
  93. Control.prototype.touchStart = function(event) {};
  94. Control.prototype.touchEndDelegate = function(e) {
  95. var ct = TOOL.isPC ? e: e.originalEvent.changedTouches[0];
  96. var c = e.data;
  97. c.touchEndX = ct.pageX;
  98. c.touchEndY = ct.pageY;
  99. var dx = c.touchEndX - c.touchStartX;
  100. var absDx = Math.abs(dx);
  101. var dy = c.touchEndY - c.touchStartY;
  102. var absDy = Math.abs(dy);
  103. if (Math.max(absDx, absDy) > 10) {
  104. c.touchDir = (absDx > absDy ? (dx > 0 ? DIR_RIGHT: DIR_LEFT) : (dy > 0 ? DIR_DOWN: DIR_UP));
  105. c.touchSlide(e)
  106. }
  107. c.touchEnd(e);
  108. c.checkTouchPrevent(e)
  109. };
  110. Control.prototype.touchEnd = function(event) {};
  111. Control.prototype.touchSlide = function(event) {};
  112. Control.prototype.checkTouchPrevent = function(e) {
  113. switch (e.target.tagName.toLowerCase()) {
  114. case "input":
  115. case "button":
  116. case "a":
  117. return
  118. }
  119. e.preventDefault()
  120. };
  121. function Engine() {
  122. var menu = $(".dropdown-menu");
  123. menu.append("<li><a class='konw-us'></a></li>");
  124. menu.append("<li><a href='http://www.17w67.com/index.html?from=djsl'>更多游戏</a></li>");
  125. CONTROL.clickOn(".konw-us", this.konwUs)
  126. }
  127. Engine.prototype.konwUs = function(event) {
  128. $(".dropdown-toggle").dropdown('toggle');
  129. $(".modal-title").text("了解我们");
  130. $(".modal-body").html("我们致力于提供纯净的游戏体验。<br>将游戏分享给更多人,是对我们最好的支持。<br>如果有好的建议,希望随时向我们提出。<br>微信订阅号:happy_games");
  131. ENGINE.showModal()
  132. };
  133. Engine.prototype.showModal = function() {
  134. $(".modal").modal()
  135. };
  136. function Tool() {
  137. this.isPC = this.checkIsPC()
  138. }
  139. Tool.prototype.positionEqual = function(p1, p2) {
  140. return (p1.x === p2.x && p1.y === p2.y)
  141. };
  142. Tool.prototype.css = function(e, name, value) {
  143. e.css(name, value);
  144. e.css("-webkit-" + name, value)
  145. };
  146. Tool.prototype.shuffle = function(arr) {
  147. var _floor = Math.floor,
  148. _random = Math.random,
  149. len = arr.length,
  150. i, j, ch, n = _floor(len / 2) + 1;
  151. while (n--) {
  152. i = _floor(_random() * len);
  153. j = _floor(_random() * len);
  154. if (i !== j) {
  155. ch = arr[i];
  156. arr[i] = arr[j];
  157. arr[j] = ch
  158. }
  159. }
  160. i = _floor(_random() * len);
  161. arr.push.apply(arr, arr.splice(0, i))
  162. };
  163. Tool.prototype.randomInt = function(n) {
  164. return parseInt(Math.random() * n)
  165. };
  166. Tool.prototype.checkIsPC = function() {
  167. return true
  168. };
  169. var CLICK_EVENT;
  170. var CLIENT_H = document.documentElement.clientHeight;
  171. var CLIENT_W = document.documentElement.clientWidth;
  172. var DIR_UP = 0;
  173. var DIR_RIGHT = 1;
  174. var DIR_DOWN = 2;
  175. var DIR_LEFT = 3;
  176. var DIRECTION = {
  177. 0 : {
  178. x: 0,
  179. y: -1
  180. },
  181. 1 : {
  182. x: 1,
  183. y: 0
  184. },
  185. 2 : {
  186. x: 0,
  187. y: 1
  188. },
  189. 3 : {
  190. x: -1,
  191. y: 0
  192. }
  193. };