end.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. ui: undefined,
  10. control: undefined,
  11. character: undefined,
  12. balloon: undefined,
  13. basket: undefined,
  14. /**
  15. * Main code
  16. */
  17. create: function () {
  18. this.control = {
  19. animate: true,
  20. preAnimate: false,
  21. waitUserAction: false,
  22. endUpdate: false,
  23. counter: 0,
  24. direc: 1,
  25. };
  26. this.ui = {
  27. continue: {
  28. button: undefined,
  29. text: undefined,
  30. },
  31. };
  32. renderBackground('end');
  33. self.utils.renderProgressBar();
  34. // Back trees
  35. game.add
  36. .image(-100, context.canvas.height - 200, 'tree_4', 1.05)
  37. .anchor(0, 1);
  38. game.add
  39. .image(360 + 400 + 400, context.canvas.height - 200, 'tree_4', 1.05)
  40. .anchor(0, 1);
  41. self.utils.renderCharacters();
  42. if (this.control.animate) game.animation.play(this.character.animation[0]);
  43. // Front tree
  44. game.add
  45. .image(30 + 200 + 100, context.canvas.height - 30, 'tree_4', 1.275)
  46. .anchor(0, 1);
  47. game.event.add('click', this.events.onInputDown);
  48. game.event.add('mousemove', this.events.onInputOver);
  49. },
  50. /**
  51. * Game loop
  52. */
  53. update: function () {
  54. if (isDebugMode) {
  55. if (debugState.end.skip && debugState.end.stop) {
  56. self.control.animate = false;
  57. }
  58. if (debugState.moodle.emulate) {
  59. moodleVar = debugState.moodle.info;
  60. game.state.start('studentReport');
  61. return;
  62. }
  63. }
  64. self.control.counter++;
  65. // Balloon falling
  66. if (self.control.preAnimate) {
  67. const speedY = 3,
  68. speedX = 2;
  69. if (self.basket.y < context.canvas.height - 240) {
  70. self.balloon.y += speedY;
  71. self.basket.y += speedY;
  72. self.character.y += speedY;
  73. self.balloon.x += speedX;
  74. self.basket.x += speedX;
  75. self.character.x += speedX;
  76. } else {
  77. self.control.preAnimate = false;
  78. self.control.animate = true;
  79. game.animation.play(self.character.animation[0]);
  80. }
  81. }
  82. if (gameName == 'circleOne') {
  83. if (self.control.counter % 40 === 0) {
  84. self.balloon.x += 5 * self.control.direc;
  85. self.control.direc *= -1;
  86. }
  87. }
  88. // Character running
  89. if (self.control.animate) {
  90. if (self.character.x <= 1550) {
  91. self.character.x += 4;
  92. } else {
  93. self.control.animate = false;
  94. game.animation.stop(self.character.animation[0]);
  95. self.character.alpha = 0;
  96. self.control.waitUserAction = true;
  97. self.utils.renderEndUI();
  98. }
  99. }
  100. if (self.control.endLevel) {
  101. // FOR MOODLE
  102. if (!moodle) {
  103. completedLevels = 0;
  104. game.state.start('menu');
  105. } else {
  106. // FOR MOODLE
  107. parent.location.reload(true);
  108. }
  109. }
  110. game.render.all();
  111. },
  112. utils: {
  113. renderProgressBar: () => {
  114. const x0 = 300;
  115. const y0 = 20;
  116. // Bar content
  117. for (let i = 0; i < 4; i++) {
  118. game.add.image(
  119. context.canvas.width - x0 + 37.5 * i,
  120. y0,
  121. 'progress_bar_tile'
  122. );
  123. }
  124. // Bar wrapper
  125. game.add.geom.rect(
  126. context.canvas.width - x0 + 1,
  127. y0 + 1,
  128. 149, //150, //149,
  129. 34, //35, //34,
  130. 'transparent',
  131. 1,
  132. colors.blue,
  133. 3
  134. );
  135. // percentage label
  136. game.add.text(
  137. context.canvas.width - x0 + 160,
  138. y0 + 33,
  139. '100%',
  140. textStyles.h2_
  141. ).align = 'left';
  142. // Difficulty label
  143. game.add.text(
  144. context.canvas.width - x0 - 10,
  145. y0 + 33,
  146. game.lang.difficulty + ' ' + gameDifficulty,
  147. textStyles.h2_
  148. ).align = 'right';
  149. },
  150. renderCharacters: () => {
  151. gameList[gameId].assets.end.building();
  152. self.character = gameList[gameId].assets.end.character();
  153. self.character.animation =
  154. gameList[gameId].assets.end.characterAnimation();
  155. if (gameName === 'circleOne') {
  156. self.control.preAnimate = true;
  157. self.control.animate = false;
  158. // Balloon
  159. self.balloon = game.add.image(0, -350, 'balloon', 1.5);
  160. self.balloon.anchor(0.5, 0.5);
  161. self.basket = game.add.image(0, -150, 'balloon_basket', 1.5);
  162. self.basket.anchor(0.5, 0.5);
  163. self.character.curFrame = 6;
  164. }
  165. },
  166. renderEndUI: () => {
  167. const btnY = context.canvas.height / 2;
  168. // Continue Button
  169. self.ui.continue.button = game.add.geom.rect(
  170. context.canvas.width / 2,
  171. btnY,
  172. 600,
  173. 100,
  174. colors.green
  175. );
  176. self.ui.continue.button.anchor(0.5, 0.5);
  177. // Continue button text
  178. self.ui.continue.text = game.add.text(
  179. context.canvas.width / 2,
  180. btnY + 16,
  181. game.lang.back_to_menu,
  182. textStyles.btn
  183. );
  184. // Title
  185. const font = { ...textStyles.btnLg };
  186. font.fill = colors.blue;
  187. game.add.text(
  188. context.canvas.width / 2,
  189. btnY - 100,
  190. game.lang.congratulations,
  191. font
  192. );
  193. },
  194. },
  195. events: {
  196. /**
  197. * Called by mouse click event
  198. *
  199. * @param {object} mouseEvent contains the mouse click coordinates
  200. */
  201. onInputDown: function (mouseEvent) {
  202. const x = game.math.getMouse(mouseEvent).x;
  203. const y = game.math.getMouse(mouseEvent).y;
  204. if (self.control.waitUserAction) {
  205. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  206. if (audioStatus) game.audio.popSound.play();
  207. self.control.endLevel = true;
  208. }
  209. }
  210. },
  211. /**
  212. * Called by mouse move event
  213. *
  214. * @param {object} mouseEvent contains the mouse move coordinates
  215. */
  216. onInputOver: function (mouseEvent) {
  217. const x = game.math.getMouse(mouseEvent).x;
  218. const y = game.math.getMouse(mouseEvent).y;
  219. let overIcon;
  220. if (self.control.waitUserAction) {
  221. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  222. // If pointer is over icon
  223. document.body.style.cursor = 'pointer';
  224. self.ui.continue.button.scale =
  225. self.ui.continue.button.initialScale * 1.1;
  226. self.ui.continue.text.style = textStyles.btnLg;
  227. } else {
  228. // If pointer is not over icon
  229. self.ui.continue.button.scale =
  230. self.ui.continue.button.initialScale * 1;
  231. document.body.style.cursor = 'auto';
  232. self.ui.continue.text.style = textStyles.btn;
  233. }
  234. }
  235. },
  236. },
  237. };