end.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [ENDING STATE] Ending screen shown when the player has completed all 4 levels and therefore completed the game.
  5. *
  6. * @namespace
  7. */
  8. const endState = {
  9. /**
  10. * Main code
  11. */
  12. create: function () {
  13. self.preAnimate = false;
  14. self.animate = true;
  15. self.waitUserAction = false;
  16. self.endUpdate = false;
  17. renderBackground();
  18. // Progress bar
  19. const y = 20;
  20. game.add.geom.rect(
  21. context.canvas.width - 300,
  22. y,
  23. 4 * 37.5,
  24. 35,
  25. undefined,
  26. 0,
  27. colors.greenNeon,
  28. 0.5
  29. );
  30. game.add.geom.rect(
  31. context.canvas.width - 300 + 1,
  32. y + 1,
  33. 149,
  34. 34,
  35. colors.blue,
  36. 3,
  37. undefined,
  38. 1
  39. );
  40. game.add.text(
  41. context.canvas.width - 300 + 160,
  42. y + 33,
  43. '100%',
  44. textStyles.h2_
  45. ).align = 'left';
  46. game.add.text(
  47. context.canvas.width - 300 - 10,
  48. y + 33,
  49. game.lang.difficulty + ' ' + gameDifficulty,
  50. textStyles.h2_
  51. ).align = 'right';
  52. game.add
  53. .image(360 + 400, context.canvas.height - 100, 'tree_4', 1.05)
  54. .anchor(0, 1);
  55. gameList[gameId].assets.end.building();
  56. this.character = gameList[gameId].assets.end.character();
  57. this.character.animation = gameList[gameId].assets.end.characterAnimation;
  58. if (gameName === 'circleOne') {
  59. this.preAnimate = true;
  60. this.animate = false;
  61. // Balloon
  62. this.balloon = game.add.image(0, -260, 'balloon', 1.5);
  63. this.balloon.anchor(0.5, 0.5);
  64. this.basket = game.add.image(0, -150, 'balloon_basket', 1.5);
  65. this.basket.anchor(0.5, 0.5);
  66. }
  67. if (this.animate) game.animation.play(this.character.animation[0]);
  68. //tree
  69. game.add
  70. .image(30 + 200, context.canvas.height - 20, 'tree_4', 1.275)
  71. .anchor(0, 1);
  72. // feedback
  73. this.continueButton = game.add.geom.rect(
  74. context.canvas.width / 2,
  75. context.canvas.height / 2,
  76. 500,
  77. 100,
  78. undefined,
  79. 1,
  80. colors.blueDark,
  81. 0
  82. );
  83. this.continueButton.anchor(0.5, 0.5);
  84. game.add.text(
  85. context.canvas.width / 2,
  86. 200,
  87. 'Congratulations!',
  88. textStyles.h1_
  89. );
  90. this.continueText = game.add.text(
  91. context.canvas.width / 2,
  92. context.canvas.height / 2 + 16,
  93. 'Go back to menu',
  94. textStyles.h1_
  95. );
  96. this.continueText.alpha = 0;
  97. game.event.add('click', this.onInputDown);
  98. game.event.add('mousemove', this.onInputOver);
  99. },
  100. /**
  101. * Game loop
  102. */
  103. update: function () {
  104. if (isDebugMode && debugState.end.status) {
  105. if (debugState.end.stop) {
  106. self.animate = false;
  107. }
  108. }
  109. // Balloon falling
  110. if (self.preAnimate) {
  111. if (self.character.y < 460) {
  112. self.balloon.y += 2;
  113. self.basket.y += 2;
  114. self.character.y += 2;
  115. self.balloon.x++;
  116. self.basket.x++;
  117. self.character.x++;
  118. } else {
  119. self.preAnimate = false;
  120. self.animate = true;
  121. game.animation.play(self.character.animation[0]);
  122. }
  123. }
  124. // Character running
  125. if (self.animate) {
  126. if (self.character.x <= 700) {
  127. self.character.x += 2;
  128. } else {
  129. self.waitUserAction = true;
  130. self.continueText.alpha = 1;
  131. self.continueButton.alpha = 1;
  132. }
  133. }
  134. if (self.endUpdate) {
  135. self.animate = false;
  136. game.animation.stop(self.character.animation[0]);
  137. // FOR MOODLE
  138. if (!moodle) {
  139. completedLevels = 0;
  140. game.state.start('menu');
  141. } else {
  142. // FOR MOODLE
  143. parent.location.reload(true);
  144. }
  145. }
  146. game.render.all();
  147. },
  148. /**
  149. * Called by mouse click event
  150. *
  151. * @param {object} mouseEvent contains the mouse click coordinates
  152. */
  153. onInputDown: function (mouseEvent) {
  154. console.log('clicked');
  155. const x = game.math.getMouse(mouseEvent).x;
  156. const y = game.math.getMouse(mouseEvent).y;
  157. if (game.math.isOverIcon(x, y, self.continueButton)) {
  158. self.endUpdate = true;
  159. //self.loadGame();
  160. }
  161. },
  162. /**
  163. * Called by mouse move event
  164. *
  165. * @param {object} mouseEvent contains the mouse move coordinates
  166. */
  167. onInputOver: function (mouseEvent) {
  168. console.log('moved');
  169. const x = game.math.getMouse(mouseEvent).x;
  170. const y = game.math.getMouse(mouseEvent).y;
  171. let overIcon;
  172. if (game.math.isOverIcon(x, y, self.continueButton)) {
  173. overIcon = true;
  174. console.log('is over icon');
  175. }
  176. // Update gui
  177. if (overIcon) {
  178. // If pointer is over icon
  179. document.body.style.cursor = 'pointer';
  180. self.continueButton.scale = self.continueButton.originalScale * 1.1;
  181. } else {
  182. // If pointer is not over icon
  183. self.continueButton.scale = self.continueButton.originalScale * 1;
  184. document.body.style.cursor = 'auto';
  185. }
  186. },
  187. };