end.js 6.0 KB

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