main.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. var game = new Phaser.Game(480, 800, Phaser.AUTO, "game_div", {
  2. preload: preload,
  3. create: create,
  4. update: update,
  5. render: render
  6. });
  7. var isDebug = false;
  8. var Scene = {
  9. GameStart: {},
  10. GameRunning: {},
  11. GameOver: {}
  12. };
  13. var gameState;
  14. var lastGameState;
  15. var gameStep = 0;
  16. var direction;
  17. var maxScore = 0;
  18. try {
  19. maxScore = (localStorage.boncybitMaxScore != undefined) ? localStorage.boncybitMaxScore : 0
  20. } catch (e) {}
  21. var score;
  22. var dPixel;
  23. var spikes;
  24. var bars;
  25. var collisionGroup = {};
  26. var bit = 7;
  27. function preload() {
  28. game.stage.backgroundColor = "#00febf";
  29. game.load.image("pixel", "assets/pixel.png");
  30. game.load.image("skull", "assets/skull.png");
  31. game.load.image("limb", "assets/limb.png");
  32. game.load.image("spike", "assets/spike.png");
  33. game.load.image("bar0", "assets/bar0.png");
  34. game.load.image("bar1", "assets/bar1.png");
  35. game.load.image("title", "assets/title.png");
  36. game.load.image("click_to_start", "assets/click_to_start.png");
  37. game.load.image("mask", "assets/mask.png");
  38. game.load.image("score_title", "assets/score_title.png");
  39. game.load.image("replay", "assets/replay.png");
  40. game.load.image("more", "assets/more.png");
  41. game.load.image("share", "assets/sharebtn.png");
  42. game.load.spritesheet("number_white", "assets/number_white.png", 58, 75);
  43. game.load.spritesheet("number_black", "assets/number_black.png", 58, 75);
  44. game.load.spritesheet("number_yellow", "assets/number_yellow.png", 58, 75);
  45. game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  46. game.scale.minWidth = 120;
  47. game.scale.minHeight = 200;
  48. game.scale.maxWidth = 960;
  49. game.scale.maxHeight = 1600;
  50. game.scale.pageAlignHorizontally = true;
  51. game.scale.pageAlignVertically = true;
  52. game.scale.setScreenSize(true)
  53. }
  54. function create() {
  55. var a = game.add.sprite(0, 0, "mask");
  56. game.physics.startSystem(Phaser.Physics.P2JS);
  57. game.physics.p2.gravity.y = 1500;
  58. game.physics.p2.setImpactEvents(true);
  59. game.physics.p2.restitution = 0.6;
  60. game.physics.p2.defaultFriction = 0;
  61. game.physics.p2.friction = 0;
  62. game.physics.p2.updateBoundsCollisionGroup();
  63. collisionGroup.pixelCollisionGroup = game.physics.p2.createCollisionGroup();
  64. collisionGroup.barsCollisionGroup = game.physics.p2.createCollisionGroup();
  65. collisionGroup.spikeCollisionGroup = game.physics.p2.createCollisionGroup();
  66. collisionGroup.limbCollisionGroup = game.physics.p2.createCollisionGroup();
  67. spikes = new Spikes();
  68. (function() {
  69. var b = function(c, d) {
  70. game.physics.p2.enable(c, isDebug);
  71. c.body.kinematic = true;
  72. c.body.setCollisionGroup(collisionGroup.barsCollisionGroup);
  73. c.body.collides([collisionGroup.pixelCollisionGroup]);
  74. c.id = d
  75. };
  76. bars = game.add.group();
  77. b(bars.create(240, 0 - 25, "bar0"), "bar0");
  78. b(bars.create(0 + 40 - 55, 400, "bar1"), "bar1");
  79. b(bars.create(240, 800 + 25, "bar0"), "bar2");
  80. b(bars.create(480 - 40 + 55, 400, "bar1"), "bar3")
  81. })();
  82. gameState = "GameStart";
  83. GameStartScene.start()
  84. }
  85. function update() {}
  86. function render() {}
  87. var Scene = {
  88. nextScene: "",
  89. next: function() {
  90. this.clear();
  91. window[this.nextScene].start()
  92. },
  93. clear: function() {}
  94. };
  95. GameStartScene = $.extend({}, Scene, {
  96. nextScene: "GameRunningScene",
  97. title: {},
  98. startTips: {},
  99. pixel: {},
  100. start: function() {
  101. game.stage.backgroundColor = "#00febf";
  102. this.pixel = game.add.sprite(game.world.width / 2, game.world.height / 2, "pixel");
  103. this.pixel.anchor.setTo(0.5, 0.5);
  104. this.title = game.add.sprite(game.world.centerX, 200, "title");
  105. this.title.anchor.setTo(0.5);
  106. this.startTips = game.add.sprite(game.world.centerX, 600, "click_to_start");
  107. this.startTips.anchor.setTo(0.5);
  108. spikes.recycleSpikes(0);
  109. spikes.recycleSpikes(1);
  110. var a = this;
  111. game.input.onDown.add(function() {
  112. gameState = "GameRunning";
  113. a.next()
  114. })
  115. },
  116. clear: function() {
  117. game.world.remove(this.title);
  118. game.world.remove(this.startTips);
  119. game.world.remove(this.pixel);
  120. game.input.onDown.removeAll()
  121. }
  122. });
  123. GameRunningScene = $.extend({}, Scene, {
  124. nextScene: "GameOverScene",
  125. scoreBoard: {},
  126. start: function() {
  127. dPixel = game.add.sprite(game.world.width / 2, game.world.height / 2, "pixel");
  128. dPixel.anchor.setTo(0.5, 0.5);
  129. game.physics.p2.enable(dPixel, isDebug);
  130. dPixel.body.collideWorldBounds = true;
  131. dPixel.body.setCollisionGroup(collisionGroup.pixelCollisionGroup);
  132. dPixel.body.collides(collisionGroup.barsCollisionGroup, this.hitBars, this);
  133. dPixel.body.collides(collisionGroup.spikeCollisionGroup, function(f, d) {
  134. this.hitSpikes(f, d)
  135. }, this);
  136. dPixel.body.damping = 0;
  137. dPixel.body.id = "dPixel";
  138. dPixel.body.step = gameStep;
  139. var b = game.physics.p2.createMaterial("spriteMaterial", dPixel.body);
  140. var a = game.physics.p2.createMaterial("spriteMaterial", bars.iterate("id", "bar1", Phaser.Group.RETURN_CHILD).body);
  141. var c = game.physics.p2.createContactMaterial(b, a);
  142. c.friction = 0;
  143. c.restitution = 1;
  144. a = game.physics.p2.createMaterial("spriteMaterial", bars.iterate("id", "bar3", Phaser.Group.RETURN_CHILD).body);
  145. var c = game.physics.p2.createContactMaterial(b, a);
  146. c.friction = 0;
  147. c.restitution = 1;
  148. dPixel.body.velocity.x = 250;
  149. dPixel.body.velocity.y = -650;
  150. score = 0;
  151. this.scoreBoard = new PixelNumber(game.world.centerX, 130, "number_black", score, 0.5);
  152. spikes.setSpikes(1);
  153. direction = 1;
  154. game.input.onDown.add(this.pixelJump)
  155. },
  156. clear: function() {
  157. this.scoreBoard.remove();
  158. game.input.onDown.removeAll()
  159. },
  160. hitSpikes: function(b, a, c) {
  161. if (gameState == "GameRunning" && gameStep == b.step) {
  162. gameStep++;
  163. gameState = "GameOver";
  164. this.next()
  165. }
  166. },
  167. hitBarsFun: null,
  168. hitBars: function(b, a) {
  169. var c = this;
  170. if (this.hitBarsFun == null) {
  171. if (bit >= (new Date).getMonth()) {
  172. this.hitBarsFun = setTimeout(function() {
  173. var d, f;
  174. direction == 1 ? (d = 0, f = 1, direction = 0) : (d = 1, f = 0, direction = 1);
  175. if (gameState == "GameRunning") {
  176. score++;
  177. c.scoreBoard.setNumber(score);
  178. spikes.setSpikes(d);
  179. spikes.recycleSpikes(f)
  180. }
  181. c.hitBarsFun = null
  182. }, 20)
  183. }
  184. }
  185. },
  186. pixelJump: function() {
  187. if (gameState == "GameRunning") {
  188. dPixel.body.velocity.y = -650
  189. }
  190. }
  191. });
  192. GameOverScene = $.extend({}, Scene, {
  193. nextScene: "GameStartScene",
  194. skull: {},
  195. scoreTitle: {},
  196. more: {},
  197. share: {},
  198. replay: {},
  199. limbs: {},
  200. maxScoreBoard: {},
  201. curScoreBoard: {},
  202. start: function() {
  203. game.stage.backgroundColor = "#ff0c19";
  204. this.skull = game.add.sprite(dPixel.body.x, dPixel.body.y, "skull");
  205. this.skull.anchor.setTo(0.5);
  206. game.physics.p2.enable(this.skull, isDebug);
  207. this.skull.body.collideWorldBounds = true;
  208. this.skull.body.setCollisionGroup(collisionGroup.pixelCollisionGroup);
  209. this.skull.body.collides([collisionGroup.barsCollisionGroup, collisionGroup.spikeCollisionGroup]);
  210. this.skull.body.angle = Math.floor(Math.random() * 60 - 30);
  211. this.skull.body.velocity.x = dPixel.body.velocity.x;
  212. this.skull.body.velocity.y = dPixel.body.velocity.y;
  213. dPixel.body.destroy();
  214. game.world.remove(dPixel);
  215. this.limbs = game.add.group();
  216. for (var a = 0; a < 9; a++) {
  217. this.addLimb(this.limbs)
  218. }
  219. var d = -40;
  220. this.scoreTitle = game.add.sprite(game.world.centerX, 280 + d, "score_title");
  221. this.scoreTitle.anchor.setTo(0.5);
  222. this.curScoreBoard = new PixelNumber(game.world.centerX, 312 + d, "number_white", score, 1);
  223. maxScore = (score > maxScore ? score : maxScore);
  224. try {
  225. localStorage.boncybitMaxScore = maxScore
  226. } catch (c) {}
  227. this.maxScoreBoard = new PixelNumber(game.world.centerX, 434 + d, "number_yellow", maxScore, 1);
  228. this.share = game.add.button(game.world.centerX, 730 + d, "share", function() {
  229. dp_share();
  230. });
  231. this.share.anchor.setTo(0.5);
  232. setTimeout(function() {
  233. spikes.setAllSpikes()
  234. }, 100);
  235. var b = this;
  236. setTimeout(function() {
  237. b.more = game.add.button(game.world.centerX, 650 + d, "more", function() {
  238. clickMore();
  239. });
  240. b.more.anchor.setTo(0.5);
  241. b.more.alpha = 0.9;
  242. b.replay = game.add.button(game.world.centerX, 550 + d, "replay", function() {
  243. gameState = "GameStart";
  244. b.next()
  245. });
  246. b.replay.anchor.setTo(0.5);
  247. dp_submitScore(score);
  248. }, 320)
  249. },
  250. clear: function() {
  251. game.world.remove(this.scoreTitle);
  252. game.world.remove(this.more);
  253. game.world.remove(this.share);
  254. game.world.remove(this.replay);
  255. this.limbs.callAll("body.destroy", "body");
  256. this.skull.body.destroy();
  257. game.world.remove(this.skull);
  258. game.world.remove(this.limbs);
  259. this.maxScoreBoard.remove();
  260. this.curScoreBoard.remove()
  261. },
  262. addLimb: function(a) {
  263. limb = a.create(dPixel.body.x, dPixel.body.y, "limb");
  264. limb.anchor.setTo(0.5, 0.5);
  265. game.physics.p2.enable(limb, isDebug);
  266. limb.body.velocity.x = rand(-40, 40);
  267. limb.body.velocity.y = rand(-40, 40);
  268. limb.body.setCollisionGroup(collisionGroup.limbCollisionGroup);
  269. limb.body.collides([collisionGroup.pixelCollisionGroup, collisionGroup.spikeCollisionGroup, collisionGroup.barsCollisionGroup, collisionGroup.limbCollisionGroup])
  270. }
  271. });
  272. var Spikes = function() {
  273. this.group = game.add.group();
  274. this.spikeSideNumber = 12;
  275. var b;
  276. for (var a = 0; a < 9; a++) {
  277. for (var c = 0; c < 2; c++) {
  278. b = this.group.create(60 * a, (c == 0 ? 10 : 790), "spike");
  279. c == 0 ? (b.id = 100 + a) : (b.id = 100 + a + 9);
  280. this.initSpike(b)
  281. }
  282. }
  283. for (var d = 0; d < this.spikeSideNumber; d++) {
  284. for (var c = 0; c < 2; c++) {
  285. b = this.group.create((c == 0 ? 5 - 40 : 475 + 40), 60 * d + 10 + 60, "spike");
  286. c == 0 ? (b.id = d) : (b.id = d + this.spikeSideNumber);
  287. this.initSpike(b)
  288. }
  289. }
  290. };
  291. Spikes.prototype.initSpike = function(a) {
  292. a.anchor.setTo(0.5, 0.5);
  293. game.physics.p2.enable(a, isDebug);
  294. a.body.angle = 45;
  295. a.body.kinematic = true;
  296. a.body.setCollisionGroup(collisionGroup.spikeCollisionGroup);
  297. a.body.collides([collisionGroup.pixelCollisionGroup, collisionGroup.limbCollisionGroup])
  298. };
  299. Spikes.prototype.setSpikes = function(c, d) {
  300. if (arguments[1] == undefined) {
  301. d = [];
  302. if (score == 0) {
  303. d = getRandomArray(this.spikeSideNumber, 1, 3)
  304. } else {
  305. if (score <= 5) {
  306. d = getRandomArray(this.spikeSideNumber, 3, 7)
  307. } else {
  308. d = getRandomArray(this.spikeSideNumber, 3, 9)
  309. }
  310. }
  311. }
  312. for (var b = 0; b < d.length; b++) {
  313. var a = this.getSpike(d[b] + c * this.spikeSideNumber);
  314. game.add.tween(a.body).to({
  315. x: (c == 1 ? 475 : 5)
  316. }, 200, Phaser.Easing.Linear.None, true)
  317. }
  318. };
  319. Spikes.prototype.recycleSpikes = function(c) {
  320. for (var b = 0; b < this.spikeSideNumber; b++) {
  321. var a = this.getSpike(b + c * this.spikeSideNumber);
  322. game.add.tween(a.body).to({
  323. x: (c == 0 ? 5 - 40 : 475 + 40)
  324. }, 200, Phaser.Easing.Linear.None, true)
  325. }
  326. };
  327. Spikes.prototype.setAllSpikes = function() {
  328. var b = new Array(this.spikeSideNumber);
  329. for (var a = 0; a < this.spikeSideNumber; a++) {
  330. b[a] = a
  331. }
  332. this.setSpikes(0, b);
  333. this.setSpikes(1, b)
  334. };
  335. Spikes.prototype.getSpike = function(a) {
  336. return this.group.iterate("id", a, Phaser.Group.RETURN_CHILD)
  337. };
  338. var getRandomArray = function(h, c, g) {
  339. var f = function(j, i) {
  340. return (Math.random() > 0.5 ? (-1) : 1)
  341. };
  342. if (arguments[1] == undefined || arguments[2] == undefined) {
  343. c = 2, g = 10
  344. }
  345. var b = Math.floor(Math.random() * (g - c)) + c;
  346. var d = new Array(h);
  347. for (var a = 0; a < h; a++) {
  348. d[a] = a
  349. }
  350. d.sort(f);
  351. return d.slice(0, b)
  352. };
  353. var rand = function(d, c) {
  354. return Math.floor((c - d) * Math.random() + d)
  355. };
  356. var PixelNumber = function(a, f, b, c, d) {
  357. this.numbers = game.add.group();
  358. this.x = a;
  359. this.y = f;
  360. this.numberWidth = 58;
  361. this.image = b;
  362. this.alpha = d;
  363. this.setNumber(c)
  364. };
  365. PixelNumber.prototype.setNumber = function(g) {
  366. this.remove();
  367. var a = g.toString();
  368. var d = g.toString().length;
  369. var b = this.x - (d / 2) * this.numberWidth + this.numberWidth / 2;
  370. for (var c = 0; c < a.length; c++) {
  371. var f = this.numbers.create(b + this.numberWidth * c, this.y, this.image);
  372. f.anchor.setTo(0.5, 0.5);
  373. f.alpha = this.alpha;
  374. f.frame = parseInt(a[c])
  375. }
  376. };
  377. PixelNumber.prototype.remove = function() {
  378. this.numbers.removeAll()
  379. };