not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

697 lines
21 KiB

  1. // Level config
  2. FoodChain.playLevels = [
  3. { flies: 2, rocks: 2, snakes: 0, time: 15 }, // Level 1
  4. { flies: 2, rocks: 3, snakes: 0, time: 10 }, // Level 2
  5. { flies: 3, rocks: 3, snakes: 0, time: 15 }, // Level 3
  6. { flies: 3, rocks: 4, snakes: 1, time: 30 }, // Level 4
  7. { flies: 4, rocks: 4, snakes: 1, time: 40 }, // Level 5
  8. { flies: 3, rocks: 3, snakes: 2, time: 50 }, // Level 6
  9. ];
  10. // Key to use
  11. FoodChain.playKeys = [
  12. { key: 75, heading: 0, dx: 1, dy: 0 },
  13. { key: 107, heading: 0, dx: 1, dy: 0 },
  14. { key: 73, heading: 90, dx: 0, dy: -1 },
  15. { key: 105, heading: 90, dx: 0, dy: -1 },
  16. { key: 74, heading: 0, dx: 1, dy: 0 },
  17. { key: 106, heading: 180, dx: -1, dy: 0 },
  18. { key: 76, heading: 270, dx: 0, dy: 1 },
  19. { key: 108, heading: 270, dx: 0, dy: 1 }
  20. ];
  21. // Play area size
  22. FoodChain.playArea = {
  23. width: -1,
  24. height: -1
  25. };
  26. // Sprite size constant
  27. FoodChain.sprites = {
  28. frog: { dx: 100, dy: 152 },
  29. rock: { dx: 112, dy: 101 },
  30. fly: { dx: 50, dy: 80 },
  31. snake: { dx: 100, dy: 250 }
  32. };
  33. // Play Game class
  34. enyo.kind({
  35. name: "FoodChain.PlayGame",
  36. kind: enyo.Control,
  37. published: {level: 1, life: 3},
  38. classes: "board",
  39. components: [
  40. { name: "cards", components: [
  41. // Level - Score - Time bar
  42. { classes: "status-bar", components: [
  43. { classes: "level-zone", components: [
  44. { name: "textlevel", classes: "title level-value" },
  45. { name: "level", content: "0", classes: "title level-value" }
  46. ]},
  47. { classes: "score-zone", components: [
  48. { name: "textscore", classes: "title score-text" },
  49. { name: "score", content: "0000", classes: "title score-value" },
  50. { name: "timercount", content: "0:0,0", classes: "title timer-value" }
  51. ]}
  52. ]},
  53. { name: "lifes", classes: "life-border", components: [
  54. { kind: "Image", classes: "life", src:"images/frog9.png" },
  55. { kind: "Image", classes: "life", src:"images/frog9.png" },
  56. { kind: "Image", classes: "life", src:"images/frog9.png" }
  57. ]},
  58. // Playing zone
  59. { name: "gamebox", classes: "game-box", components: [
  60. ]},
  61. // Buttons bar
  62. { name: "play", kind: "ShadowButton", img: "play", classes: "play", ontap: "play" },
  63. { name: "pause", kind: "ShadowButton", img: "pause", classes: "play", ontap: "pause" },
  64. { name: "forward", kind: "ShadowButton", img: "forward", classes: "restart", ontap: "next" },
  65. { name: "home", kind: "ShadowButton", img: "home", classes: "home2", ontap: "home" },
  66. // End of sound event
  67. {kind: "Signals", onEndOfSound: "endSound", onkeypress: "keyPressed"},
  68. // Preload all images for the game
  69. {showing: false, components: [
  70. {kind: "Image", id: "frog1", src:"images/frog1.png", classes: "image-preload", onload: "imageLoaded" },
  71. {kind: "Image", id: "frog2", src:"images/frog2.png", classes: "image-preload", onload: "imageLoaded" },
  72. {kind: "Image", id: "frog3", src:"images/frog3.png", classes: "image-preload", onload: "imageLoaded" },
  73. {kind: "Image", id: "frog4", src:"images/frog4.png", classes: "image-preload", onload: "imageLoaded" },
  74. {kind: "Image", id: "frog5", src:"images/frog5.png", classes: "image-preload", onload: "imageLoaded" },
  75. {kind: "Image", id: "frog6", src:"images/frog6.png", classes: "image-preload", onload: "imageLoaded" },
  76. {kind: "Image", id: "frog7", src:"images/frog7.png", classes: "image-preload", onload: "imageLoaded" },
  77. {kind: "Image", id: "frog8", src:"images/frog8.png", classes: "image-preload", onload: "imageLoaded" },
  78. {kind: "Image", id: "frog9", src:"images/frog9.png", classes: "image-preload", onload: "imageLoaded" },
  79. {kind: "Image", id: "fly1", src:"images/fly1.png", classes: "image-preload", onload: "imageLoaded" },
  80. {kind: "Image", id: "fly2", src:"images/fly2.png", classes: "image-preload", onload: "imageLoaded" },
  81. {kind: "Image", id: "snake1", src:"images/snake1.png", classes: "image-preload", onload: "imageLoaded" },
  82. {kind: "Image", id: "snake2", src:"images/snake2.png", classes: "image-preload", onload: "imageLoaded" },
  83. {kind: "Image", id: "snake3", src:"images/snake3.png", classes: "image-preload", onload: "imageLoaded" },
  84. {kind: "Image", id: "snake4", src:"images/snake4.png", classes: "image-preload", onload: "imageLoaded" },
  85. {kind: "Image", id: "snake5", src:"images/snake5.png", classes: "image-preload", onload: "imageLoaded" },
  86. {kind: "Image", id: "snake6", src:"images/snake6.png", classes: "image-preload", onload: "imageLoaded" },
  87. {kind: "Image", id: "snake7", src:"images/snake7.png", classes: "image-preload", onload: "imageLoaded" },
  88. {kind: "Image", id: "snake8", src:"images/snake8.png", classes: "image-preload", onload: "imageLoaded" },
  89. {kind: "Image", id: "rock", src:"images/rock.png", classes: "image-preload", onload: "imageLoaded" }
  90. ]}
  91. ]}
  92. ],
  93. // Constructor
  94. create: function() {
  95. this.inherited(arguments);
  96. this.imagesToLoad = 20;
  97. this.nextaction = 0;
  98. FoodChain.playArea = {
  99. width: 984,
  100. height: 560
  101. };
  102. var zoom = FoodChain.getZoomLevel();
  103. this.$.gamebox.setStyle("max-height: "+(FoodChain.playArea.height*zoom)+"px;"+"max-width: "+(FoodChain.playArea.width*zoom)+"px;");
  104. this.canvas = this.$.gamebox.createComponent({kind: "Canvas", id: "acanvas", name: "canvas", ontap: "clickToMove", attributes: {width: FoodChain.playArea.width, height: FoodChain.playArea.height}}, {owner: this});
  105. this.createComponent({ name: "timer", kind: "Timer", paused: true, onTriggered: "updateTimer" }, {owner: this});
  106. this.createComponent({ name: "timerMonster", kind: "Timer", baseInterval: 500, onTriggered: "monsterEngine" }, {owner: this});
  107. this.setLocale();
  108. this.$.score.setContent(String("0000"+FoodChain.context.score).slice(-4));
  109. FoodChain.context.game = this.kindName;
  110. this.levelChanged();
  111. },
  112. // Localization changed, update cards and string resource
  113. setLocale: function() {
  114. // Update string resources
  115. this.$.textlevel.setContent(__$FC("level"));
  116. this.$.textscore.setContent(__$FC("score"));
  117. },
  118. // Level changed, init board then start game
  119. levelChanged: function() {
  120. // Init frog
  121. FoodChain.context.level = this.level;
  122. this.frog = new Sprite({
  123. x: 70, y: (FoodChain.playArea.height/2)-20, heading: 0, images: ["frog1", "frog2", "frog3", "frog4", "frog5", "frog6", "frog7", "frog8", "frog9"],
  124. width: FoodChain.sprites.frog.dx, height: FoodChain.sprites.frog.dy, index: 0, sound: "audio/frog"
  125. });
  126. this.frog.alive = true;
  127. // Set randomely rocks
  128. this.rocks = [];
  129. for(var i = 0 ; i < FoodChain.playLevels[this.level-1].rocks ; i++) {
  130. // Ensure distance between blocks and between block and frog is sufficient
  131. var rock = FoodChain.createWithCondition(
  132. // Create randomly a new rock...
  133. function() {
  134. var x = 130+Math.floor(Math.random()*(FoodChain.playArea.width-300));
  135. var y = 130+Math.floor(Math.random()*(FoodChain.playArea.height-200));
  136. var h = Math.floor(Math.random()*4)*90;
  137. return new Sprite({
  138. x: x, y: y, heading: h, images: ["rock"], width: FoodChain.sprites.rock.dx, height: FoodChain.sprites.rock.dy, index: 0
  139. });
  140. },
  141. // ... while don't intersect with ...
  142. function(n, s) {
  143. return !n.intersect(s);
  144. },
  145. // ... other rocks and frog
  146. enyo.cloneArray(this.rocks).concat(this.frog)
  147. );
  148. this.rocks.push(rock);
  149. }
  150. // Set randomely flies
  151. this.flies = [];
  152. for(var i = 0 ; i < FoodChain.playLevels[this.level-1].flies ; i++) {
  153. // Ensure not on a rock, on the frog or on other fly
  154. var fly = FoodChain.createWithCondition(
  155. // Create randomly a new fly...
  156. function() {
  157. var x = 100+Math.floor(Math.random()*(FoodChain.playArea.width-300));
  158. var y = 100+Math.floor(Math.random()*(FoodChain.playArea.height-200));
  159. var h = Math.floor(Math.random()*4)*90;
  160. return new Sprite({
  161. x: x, y: y, heading: h, images: ["fly1", "fly2"], width: FoodChain.sprites.fly.dx, height: FoodChain.sprites.fly.dy, index: 0, sound: "audio/flies"
  162. });
  163. },
  164. // ... while don't intersect with ...
  165. function(n, s) {
  166. return !n.intersect(s);
  167. },
  168. // ... other rocks, flies and frog
  169. enyo.cloneArray(this.rocks).concat(this.flies).concat(this.frog)
  170. );
  171. // Create the new fly
  172. fly.alive = true;
  173. this.flies.push(fly);
  174. }
  175. // Create snakes (dead at init)
  176. this.snakes = [];
  177. for(var i = 0 ; i < FoodChain.playLevels[this.level-1].snakes ; i++) {
  178. var snake = new Sprite({
  179. x: 0, y: 0, heading: 0, images: ["snake1", "snake2", "snake3", "snake4", "snake5", "snake6", "snake7", "snake8"],
  180. width: FoodChain.sprites.snake.dx, height: FoodChain.sprites.snake.dy, index: 0, sound: "audio/snake"
  181. });
  182. snake.alive = false;
  183. this.snakes.push(snake);
  184. }
  185. // Saving context
  186. FoodChain.saveContext();
  187. // Button handling
  188. this.$.play.hide();
  189. this.$.pause.show();
  190. this.$.forward.hide();
  191. this.$.home.hide();
  192. // Timer and level init
  193. this.$.level.setContent(" "+this.level);
  194. this.timecount = {mins:0, secs:0, tenth:0};
  195. this.$.timercount.removeClass("timer-overtime");
  196. this.displayTimer();
  197. },
  198. // One image load
  199. imageLoaded: function() {
  200. if (--this.imagesToLoad == 0)
  201. this.initGame();
  202. },
  203. // All image loaded, start displaying game
  204. initGame: function() {
  205. var zoom = FoodChain.getZoomLevel();
  206. this.canvas.hasNode().style.MozTransform = "scale("+zoom+")";
  207. this.canvas.hasNode().style.MozTransformOrigin = "0 0";
  208. this.canvas.hasNode().style.zoom = zoom;
  209. this.ctx = this.canvas.node.getContext('2d');
  210. this.ctx.clearRect(0, 0, this.canvas.attributes.width, this.canvas.attributes.height);
  211. // Draw rocks
  212. for(var i = 0 ; i < this.rocks.length ; i++) {
  213. this.rocks[i].draw(this.ctx);
  214. }
  215. // Draw frog
  216. this.frog.draw(this.ctx);
  217. // Draw flies
  218. for(var i = 0 ; i < this.flies.length ; i++) {
  219. this.flies[i].draw(this.ctx);
  220. }
  221. // Start timer
  222. this.$.timer.resume();
  223. },
  224. // Show direction to frog using click on board
  225. clickToMove: function(s, e) {
  226. // Compute direction comparing click with frog position
  227. var dx = e.clientX-this.frog.getX(), dy = e.clientY-this.frog.getY();
  228. if (dx == 0 && dy == 0)
  229. return;
  230. if (Math.abs(dx) > Math.abs(dy)) {
  231. dx = dx > 0 ? 1 : -1;
  232. dy = 0;
  233. } else {
  234. dx = 0;
  235. dy = dy > 0 ? 1 : -1;
  236. }
  237. // Simulate the equivalent key direction
  238. for (var i = 0 ; i < FoodChain.playKeys.length ; i++ ) {
  239. var playKey = FoodChain.playKeys[i];
  240. if (playKey.dx == dx && playKey.dy == dy) {
  241. e.charCode = playKey.key;
  242. this.keyPressed(s, e);
  243. return;
  244. }
  245. }
  246. },
  247. // A key was pressed
  248. keyPressed: function(s, e) {
  249. var key = e.charCode;
  250. // Game paused
  251. if (this.$.timer.paused)
  252. return;
  253. // Frog is dead
  254. if (!this.frog.alive)
  255. return;
  256. // Compute asked direction
  257. var newHeading;
  258. var dx = 0, dy = 0;
  259. var foundKey = false;
  260. for (var i = 0 ; i < FoodChain.playKeys.length ; i++ ) {
  261. var playKey = FoodChain.playKeys[i];
  262. if (key == playKey.key) {
  263. dx = playKey.dx;
  264. dy = playKey.dy;
  265. newHeading = playKey.heading;
  266. foundKey = true;
  267. }
  268. }
  269. if (!foundKey) {
  270. FoodChain.log("key pressed: " + key);
  271. return;
  272. }
  273. // Move frog
  274. this.frog.unDraw(this.ctx);
  275. if (newHeading != this.frog.getHeading()) {
  276. // Just change heading
  277. this.frog.setHeading(newHeading);
  278. this.frog.firstImage();
  279. } else {
  280. // Move frog
  281. this.frog.animate(this.ctx, [1, 2, 3, 4, 5, 6, 0], dx*10, dy*10, enyo.bind(this, "frogEngine"));
  282. }
  283. this.frog.draw(this.ctx);
  284. },
  285. // Frog engine: test collition between frog and other sprites
  286. frogEngine: function() {
  287. // Test if not collide with a rock
  288. for(var i = 0 ; i < this.rocks.length ; i++) {
  289. if (this.frog.intersect(this.rocks[i])) {
  290. // Yes, frog is dead
  291. this.frog.unDraw(this.ctx);
  292. this.frog.useImage(7);
  293. this.frog.draw(this.ctx);
  294. this.rocks[i].draw(this.ctx);
  295. this.frog.alive = false;
  296. this.testEndOfGame();
  297. return false;
  298. }
  299. }
  300. // Test if eat a fly
  301. for(var i = 0 ; i < this.flies.length ; i++) {
  302. if (this.flies[i].alive && this.frog.intersect(this.flies[i])) {
  303. // Yes, flies is dead
  304. this.flies[i].unDraw(this.ctx);
  305. this.flies[i].alive = false;
  306. // Animate the happy frog, score and test for next level
  307. this.frog.playSound();
  308. this.addScore(1);
  309. return !this.testEndOfGame();
  310. }
  311. }
  312. // Test if out of playboard
  313. if (this.frog.getX() <= this.frog.width/2 || this.frog.getX() >= this.canvas.attributes.width-this.frog.width/2 ||
  314. this.frog.getY() <= this.frog.height/2 || this.frog.getY() >= this.canvas.attributes.height-this.frog.height/2) {
  315. // Yes, replace it on the limit
  316. this.frog.setX(Math.max(this.frog.width/2+1, this.frog.getX()));
  317. this.frog.setX(Math.min(this.canvas.attributes.width-this.frog.width/2-1, this.frog.getX()));
  318. this.frog.setY(Math.max(this.frog.height/2+1, this.frog.getY()));
  319. this.frog.setY(Math.min(this.canvas.attributes.height-this.frog.height/2-1, this.frog.getY()));
  320. this.frog.unDraw(this.ctx);
  321. this.frog.firstImage();
  322. this.frog.draw(this.ctx);
  323. this.frog.playSound();
  324. return false;
  325. }
  326. // Test if not collide with a rock
  327. for(var i = 0 ; i < this.snakes.length ; i++) {
  328. if (this.frog.intersect(this.snakes[i])) {
  329. // Yes, frog is dead
  330. this.frog.unDraw(this.ctx);
  331. this.frog.useImage(7);
  332. this.frog.draw(this.ctx);
  333. this.snakes[i].draw(this.ctx);
  334. this.frog.alive = false;
  335. this.testEndOfGame();
  336. return false;
  337. }
  338. }
  339. return true;
  340. },
  341. // Periodic engine wake up for flies and snakes
  342. monsterEngine: function() {
  343. // Flies engine
  344. this.fliesEngine();
  345. // Snakes engine
  346. this.snakesEngine();
  347. },
  348. // Flies engine: move and sound flies periodically
  349. fliesEngine: function() {
  350. // Game paused
  351. if (this.$.timer.paused)
  352. return;
  353. // For each fly
  354. for(var i = 0 ; i < this.flies.length ; i++) {
  355. // Dead fly, nothing to do
  356. if (!this.flies[i].alive)
  357. continue;
  358. // Randomly decide what to do
  359. var n = Math.floor(Math.random()*10);
  360. // Move the fly
  361. if (n == 1) {
  362. this.moveFly(this.flies[i]);
  363. // Just animate the fly
  364. } else if (n <= 2) {
  365. this.flies[i].animate(this.ctx, [0, 1, 0, 1, 0, 1], 0, 0, enyo.bind(this, "testFlyDead"));
  366. }
  367. }
  368. },
  369. // Move the fly due to periodic change or snake collision
  370. moveFly: function(fly) {
  371. var currentfly = fly;
  372. var dummyfly = FoodChain.createWithCondition(
  373. // Create randomly a new fly...
  374. function() {
  375. var x = 100+Math.floor(Math.random()*(FoodChain.playArea.width-300));
  376. var y = 100+Math.floor(Math.random()*(FoodChain.playArea.height-200));
  377. var h = Math.floor(Math.random()*4)*90;
  378. return new Sprite({x: x, y: y, heading: h, width: FoodChain.sprites.fly.dx, height: FoodChain.sprites.fly.dy});
  379. },
  380. // ... while don't intersect with ...
  381. function(n, s) {
  382. return s == currentfly || !n.intersect(s);
  383. },
  384. // ... other rocks, flies, frog and snakes
  385. enyo.cloneArray(this.rocks).concat(this.flies).concat(this.frog).concat(this.snakes)
  386. );
  387. // Redraw
  388. currentfly.unDraw(this.ctx);
  389. currentfly.setX(dummyfly.getX());
  390. currentfly.setY(dummyfly.getY());
  391. currentfly.setHeading(dummyfly.getHeading());
  392. currentfly.draw(this.ctx);
  393. currentfly.playSound();
  394. },
  395. // Snake engine: create and animate snake periodically
  396. snakesEngine: function() {
  397. // Game paused
  398. if (this.$.timer.paused)
  399. return;
  400. // For each snake
  401. for(var i = 0 ; i < this.snakes.length ; i++) {
  402. // Randomly decide what to do
  403. var n = Math.floor(Math.random()*4);
  404. if (n != 0)
  405. continue;
  406. // Snake has end its course
  407. var currentsnake = this.snakes[i];
  408. var maxheight = this.canvas.attributes.height;
  409. var maxwidth = this.canvas.attributes.width;
  410. if ((currentsnake.getHeading() == 90 && currentsnake.getY()+currentsnake.height < 0)
  411. || (currentsnake.getHeading() == 270 && currentsnake.getY()-currentsnake.height/2 > maxheight))
  412. currentsnake.alive = false;
  413. // Snake out of game, reintroduce it
  414. if (!currentsnake.alive) {
  415. // Compute trajectory free of rock & snakes interval
  416. var spritesToAvoid = enyo.cloneArray(this.rocks).concat(this.snakes);
  417. var freeInterval = [];
  418. for (var x = 100 ; x < FoodChain.playArea.width-300 ; x = x + 50) {
  419. // Compute is this interval is free
  420. var free = true;
  421. for (var j = 0 ; free && j < spritesToAvoid.length ; j++) {
  422. // Test each sprite
  423. var s = spritesToAvoid[j];
  424. if (s == currentsnake) continue;
  425. if ((x > s.getX() && x-currentsnake.width/2 < s.getX()+s.width/2) || (x < s.getX() && x+currentsnake.width/2 > s.getX()-s.width/2)) {
  426. free = false;
  427. }
  428. }
  429. // No, add to busyList
  430. if (free)
  431. freeInterval.push(x);
  432. }
  433. // Compute the snake position
  434. x = freeInterval[Math.floor(Math.random()*freeInterval.length)];
  435. var h = (Math.floor(Math.random()*2) == 0) ? 90 : 270;
  436. var y = (h == 90) ? maxheight : 0;
  437. // Set alive and launch animation
  438. currentsnake.setX(x);
  439. currentsnake.setY(y);
  440. currentsnake.setHeading(h);
  441. currentsnake.alive = true;
  442. currentsnake.playSound();
  443. var dy = (currentsnake.getHeading() == 90) ? -6 : 6;
  444. currentsnake.animate(this.ctx, [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7], 0, dy, enyo.bind(this, "snakeCollisionEngine"));
  445. }
  446. // Animate it
  447. else {
  448. var dy = (currentsnake.getHeading() == 90) ? -6 : 6;
  449. currentsnake.animate(this.ctx, [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7], 0, dy, enyo.bind(this, "snakeCollisionEngine"));
  450. }
  451. }
  452. },
  453. // Snake collision engine
  454. snakeCollisionEngine: function(snake) {
  455. // Hit frog ?
  456. if (snake.intersect(this.frog)) {
  457. // Yes but already dead
  458. if (!this.frog.alive) {
  459. this.frog.draw(this.ctx);
  460. return true;
  461. }
  462. // Yes, frog is dead
  463. this.frog.alive = false;
  464. this.frog.unDraw(this.ctx);
  465. this.frog.useImage(7);
  466. this.frog.draw(this.ctx);
  467. this.testEndOfGame();
  468. return true;
  469. }
  470. // Test if eat a fly
  471. for(var i = 0 ; i < this.flies.length ; i++) {
  472. if (this.flies[i].alive && snake.intersect(this.flies[i])) {
  473. // Yes move the fly
  474. this.moveFly(this.flies[i]);
  475. return true;
  476. }
  477. }
  478. return true;
  479. },
  480. // Force end of fly animation if fly is dead
  481. testFlyDead: function(fly) {
  482. if (!fly.alive) {
  483. fly.unDraw(this.ctx);
  484. return false;
  485. }
  486. return true;
  487. },
  488. // Test end of game
  489. testEndOfGame: function() {
  490. // Frog is dead
  491. if (!this.frog.alive) {
  492. // Delete a life
  493. this.life--;
  494. this.$.lifes.getControls()[this.life].hide();
  495. // Is it last life ?
  496. if (this.life > 0) {
  497. // No, next frog
  498. this.nextaction = 1;
  499. } else {
  500. // Yes
  501. this.$.timer.pause();
  502. this.$.pause.hide();
  503. this.$.home.show();
  504. }
  505. FoodChain.sound.play("audio/disappointed");
  506. }
  507. // Frog is alive: last fly eaten ?
  508. else {
  509. // Count living fly
  510. var flies = 0;
  511. for(var i = 0 ; i < this.flies.length ; i++) {
  512. if (this.flies[i].alive) flies++;
  513. }
  514. // Not the last, continue
  515. if (flies > 0)
  516. return false;
  517. // Show happy frog
  518. this.frog.unDraw(this.ctx);
  519. this.frog.setX(this.frog.getX()+50);
  520. this.frog.setY(this.frog.getY()+50);
  521. this.frog.useImage(8);
  522. this.frog.setHeading(90);
  523. this.frog.draw(this.ctx);
  524. // No more, go to next level
  525. this.$.timer.pause();
  526. FoodChain.sound.play("audio/applause");
  527. this.computeLevelScore();
  528. this.$.play.hide();
  529. this.$.pause.hide();
  530. this.$.home.show();
  531. if (this.level != FoodChain.playLevels.length)
  532. this.$.forward.show();
  533. return true;
  534. }
  535. },
  536. // Sound ended
  537. endSound: function(e, s) {
  538. // Next life
  539. if (this.nextaction == 1) {
  540. this.nextaction = 0;
  541. this.frog.unDraw(this.ctx);
  542. this.frog.setX(70);
  543. this.frog.setY((FoodChain.playArea.height/2)-20);
  544. this.frog.setHeading(0);
  545. this.frog.useImage(0);
  546. this.frog.draw(this.ctx);
  547. this.frog.alive = true;
  548. return;
  549. }
  550. },
  551. // Display timer value
  552. displayTimer: function() {
  553. this.$.timercount.setContent(this.timecount.mins+":"+String("00"+this.timecount.secs).slice(-2)+","+this.timecount.tenth);
  554. },
  555. // Update timer
  556. updateTimer: function(s, e) {
  557. this.timecount.tenth = this.timecount.tenth + 1;
  558. if (this.timecount.tenth == 10) {
  559. this.timecount.tenth = 0;
  560. this.timecount.secs = this.timecount.secs + 1;
  561. var currentcount = this.timecount.mins * 60 + this.timecount.secs;
  562. if (currentcount >= FoodChain.playLevels[this.level-1].time) {
  563. this.$.timercount.addClass("timer-overtime");
  564. }
  565. if (this.timecount.secs == 60) {
  566. this.timecount.secs = 0;
  567. this.timecount.mins = this.timecount.mins + 1;
  568. }
  569. }
  570. this.displayTimer();
  571. },
  572. // Compute score
  573. addScore: function(score) {
  574. FoodChain.context.score += score;
  575. this.$.score.setContent(String("0000"+FoodChain.context.score).slice(-4));
  576. },
  577. // Compute score for this level
  578. computeLevelScore: function() {
  579. var score = 0;
  580. var currentcount = this.timecount.mins * 60 + this.timecount.secs;
  581. if (currentcount < FoodChain.playLevels[this.level-1].time) {
  582. score += (FoodChain.playLevels[this.level-1].time - currentcount);
  583. }
  584. this.addScore(score);
  585. },
  586. // Resume game
  587. play: function() {
  588. this.$.timer.resume();
  589. this.$.play.hide();
  590. this.$.pause.show();
  591. this.$.home.hide();
  592. },
  593. // Pause game
  594. pause: function() {
  595. this.$.timer.pause();
  596. this.$.pause.hide();
  597. this.$.play.show();
  598. this.$.home.show();
  599. },
  600. // Go to the next level
  601. next: function() {
  602. this.level = this.level + 1;
  603. this.levelChanged();
  604. this.initGame();
  605. },
  606. // Go to the home page of the app
  607. home: function() {
  608. this.$.timer.stop();
  609. this.$.timerMonster.stop();
  610. FoodChain.goHome();
  611. }
  612. });