map.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [MAP STATE] Screen that shows the 4 generated levels in a map (and the level where the player is currently in).
  5. *
  6. * @namespace
  7. */
  8. const mapState = {
  9. /**
  10. * Main code
  11. */
  12. create: function () {
  13. // Background color
  14. game.add.geom.rect(0, 0, context.canvas.width, context.canvas.height, undefined, 0, colors.blueBckg, 1);
  15. // Map
  16. game.add.image(0, 40, 'bgmap');
  17. // Calls function that loads navigation icons
  18. // FOR MOODLE
  19. if (moodle) {
  20. navigationIcons.add(
  21. false, false, false, // Left icons
  22. false, false, // Right icons
  23. false, false);
  24. } else {
  25. navigationIcons.add(
  26. true, true, false, // Left icons
  27. false, false, // Right icons
  28. 'customMenu', false);
  29. }
  30. // Progress bar
  31. const percentText = completedLevels * 25;
  32. if (completedLevels >= 4) game.add.geom.rect(660, 10, 4 * 37.5, 35, undefined, 0, colors.intenseGreen, 0.5);
  33. else game.add.geom.rect(660, 10, completedLevels * 37.5, 35, undefined, 0, colors.yellow, 0.9);
  34. game.add.geom.rect(661, 11, 149, 34, colors.blue, 3, undefined, 1); // Box
  35. game.add.text(820, 38, percentText + '%', textStyles.h2_blue).align = 'left';
  36. game.add.text(650, 38, game.lang.difficulty + ' ' + gameDifficulty, textStyles.h2_blue).align = 'right';
  37. // Map positions
  38. this.points = {
  39. x: [90, 204, 318, 432, 546, 660],
  40. y: [486, 422, 358, 294, 230, 166]
  41. };
  42. if (gameType == 'squareOne') {
  43. // Garage
  44. game.add.image(this.points.x[0], this.points.y[0], 'garage', 0.4).anchor(0.5, 1);
  45. // Farm
  46. game.add.image(this.points.x[5], this.points.y[5], 'farm', 0.6).anchor(0.1, 0.7);
  47. } else {
  48. // House
  49. game.add.image(this.points.x[0], this.points.y[0], 'house', 0.7).anchor(0.7, 0.8);
  50. // School
  51. game.add.image(this.points.x[5], this.points.y[5], 'school', 0.35).anchor(0.2, 0.7);
  52. }
  53. // Rocks and bushes
  54. const rocks = {
  55. x: [156, 275, 276, 441, 452, 590, 712],
  56. y: [309, 543, 259, 156, 419, 136, 316],
  57. type: [1, 1, 2, 1, 2, 2, 2]
  58. };
  59. for (let i in rocks.type) {
  60. if (rocks.type[i] == 1) {
  61. game.add.image(rocks.x[i], rocks.y[i], 'rock', 0.32).anchor(0.5, 0.95);
  62. } else {
  63. game.add.image(rocks.x[i], rocks.y[i], 'bush', 0.4).anchor(0.5, 0.95);
  64. }
  65. }
  66. // Trees
  67. const trees = {
  68. x: [105, 214, 354, 364, 570, 600, 740, 779],
  69. y: [341, 219, 180, 520, 550, 392, 488, 286],
  70. type: [2, 4, 3, 4, 1, 2, 4, 4]
  71. };
  72. for (let i in trees.type) {
  73. game.add.image(trees.x[i], trees.y[i], 'tree' + trees.type[i], 0.6).anchor(0.5, 0.95);
  74. }
  75. // Map positions
  76. for (let i = 1; i < this.points.x.length - 1; i++) {
  77. const aux = (i < mapPosition || (mapMove && i == mapPosition)) ? 'place_on' : 'place_off';
  78. // Map road positions - game levels
  79. game.add.image(this.points.x[i], this.points.y[i], aux, 0.3).anchor(0.5, 0.5);
  80. // Map road signs - game level number
  81. game.add.image(this.points.x[i] - 20, this.points.y[i] - 60, 'sign', 0.4).anchor(0.5, 1);
  82. game.add.text(this.points.x[i] - 20, this.points.y[i] - 79, i, textStyles.h2_white);
  83. }
  84. // Game Character
  85. if (gameType == 'squareOne') {
  86. if (gameOperation == 'Plus') {
  87. this.character = game.add.sprite(this.points.x[mapPosition], this.points.y[mapPosition], 'tractor', 0, 0.5);
  88. this.character.animation = ['green_tractor', [0, 1, 2, 3, 4], 3];
  89. } else {
  90. this.character = game.add.sprite(this.points.x[mapPosition], this.points.y[mapPosition], 'tractor', 10, 0.5);
  91. this.character.animation = ['red_tractor', [10, 11, 12, 13, 14], 3];
  92. }
  93. this.character.rotate = -30; // 25 anticlock
  94. } else {
  95. this.character = game.add.sprite(this.points.x[mapPosition], this.points.y[mapPosition], 'kid_run', 0, 0.4);
  96. this.character.animation = ['kid', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3];
  97. }
  98. this.character.anchor(0.5, 1);
  99. game.animation.play(this.character.animation[0]);
  100. this.count = 0;
  101. const speed = 60;
  102. const xA = this.points.x[mapPosition];
  103. const yA = this.points.y[mapPosition];
  104. const xB = this.points.x[mapPosition + 1];
  105. const yB = this.points.y[mapPosition + 1];
  106. self.speedX = (xB - xA) / speed;
  107. self.speedY = (yA - yB) / speed;
  108. game.event.add('click', this.onInputDown);
  109. game.event.add('mousemove', this.onInputOver);
  110. },
  111. /**
  112. * Game loop
  113. */
  114. update: function () {
  115. let endUpdate = false;
  116. self.count++;
  117. if (self.count > 60) { // Wait 1 second before moving or staring a game
  118. if (mapMove) { // Move character on screen for 1 second
  119. self.character.x += self.speedX;
  120. self.character.y -= self.speedY;
  121. if (Math.ceil(self.character.x) >= self.points.x[mapPosition + 1]) { // Reached next map position
  122. mapMove = false;
  123. mapPosition++; // Set new next position
  124. }
  125. }
  126. if (!mapMove) {
  127. endUpdate = true;
  128. }
  129. }
  130. game.render.all();
  131. if (endUpdate) {
  132. game.animation.stop(self.character.animation[0]);
  133. self.loadGame();
  134. }
  135. },
  136. /**
  137. * Calls game state
  138. */
  139. loadGame: function () {
  140. if (audioStatus) game.audio.beepSound.play();
  141. if (mapPosition <= 4) game.state.start('' + gameType);
  142. else game.state.start('end');
  143. },
  144. /**
  145. * Called by mouse click event
  146. *
  147. * @param {object} mouseEvent contains the mouse click coordinates
  148. */
  149. onInputDown: function (mouseEvent) {
  150. const x = game.math.getMouse(mouseEvent).x;
  151. const y = game.math.getMouse(mouseEvent).y;
  152. navigationIcons.onInputDown(x, y);
  153. },
  154. /**
  155. * Called by mouse move event
  156. *
  157. * @param {object} mouseEvent contains the mouse move coordinates
  158. */
  159. onInputOver: function (mouseEvent) {
  160. const x = game.math.getMouse(mouseEvent).x;
  161. const y = game.math.getMouse(mouseEvent).y;
  162. navigationIcons.onInputOver(x, y);
  163. }
  164. };
  165. /** [ENDING STATE] Ending screen shown when the player has completed all 4 levels and therefore completed the game.
  166. *
  167. * @namespace
  168. */
  169. const endState = {
  170. /**
  171. * Main code
  172. */
  173. create: function () {
  174. self.preAnimate = false;
  175. self.animate = true;
  176. // Background color
  177. game.add.geom.rect(0, 0, context.canvas.width, context.canvas.height, undefined, 0, colors.blueBckg, 1);
  178. // Background
  179. game.add.image(0, 0, 'bgimage');
  180. // Clouds
  181. game.add.image(300, 100, 'cloud');
  182. game.add.image(660, 80, 'cloud');
  183. game.add.image(110, 85, 'cloud', 0.8);
  184. // Floor
  185. for (let i = 0; i < 9; i++) { game.add.image(i * 100, context.canvas.height - 100, 'floor'); }
  186. // Progress bar
  187. game.add.geom.rect(660, 10, 4 * 37.5, 35, undefined, 0, colors.intenseGreen, 0.5); // Progress
  188. game.add.geom.rect(661, 11, 149, 34, colors.blue, 3, undefined, 1); // Box
  189. game.add.text(820, 38, '100%', textStyles.h2_blue).align = 'left';
  190. game.add.text(650, 38, game.lang.difficulty + ' ' + gameDifficulty, textStyles.h2_blue).align = 'right';
  191. game.add.image(360, 545, 'tree4', 0.7).anchor(0, 1);
  192. // Level character
  193. switch (gameType) {
  194. case 'circleOne':
  195. this.preAnimate = true;
  196. this.animate = false;
  197. // School
  198. game.add.image(600, 222, 'school', 0.7);
  199. // Kid
  200. this.character = game.add.sprite(0, -152, 'kid_run', 0, 0.7);
  201. this.character.anchor(0.5, 0.5);
  202. this.character.animation = ['move', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3];
  203. // Balloon
  204. this.balloon = game.add.image(0, -260, 'balloon');
  205. this.balloon.anchor(0.5, 0.5);
  206. this.basket = game.add.image(0, -150, 'balloon_basket');
  207. this.basket.anchor(0.5, 0.5);
  208. break;
  209. case 'squareTwo':
  210. // School
  211. game.add.image(600, 222, 'school', 0.7);
  212. // Kid
  213. this.character = game.add.sprite(0, 460, 'kid_run', 6, 0.7);
  214. this.character.anchor(0.5, 0.5);
  215. this.character.animation = ['move', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3];
  216. break;
  217. case 'squareOne':
  218. // Farm
  219. game.add.image(650, 260, 'farm', 1.1);
  220. // Tractor
  221. this.character = game.add.sprite(0, 490, 'tractor', 0, 0.7);
  222. this.character.anchor(0.5, 0.5);
  223. if (gameOperation == 'Plus') {
  224. this.character.animation = ['move', [0, 1, 2, 3, 4], 4];
  225. } else {
  226. this.character.curFrame = 10;
  227. this.character.animation = ['move', [10, 11, 12, 13, 14], 4];
  228. }
  229. break;
  230. }
  231. if (this.animate) game.animation.play(this.character.animation[0]);
  232. game.add.image(30, 585, 'tree4', 0.85).anchor(0, 1);
  233. },
  234. /**
  235. * Game loop
  236. */
  237. update: function () {
  238. // Balloon falling
  239. if (self.preAnimate) {
  240. if (self.character.y < 460) {
  241. self.balloon.y += 2;
  242. self.basket.y += 2;
  243. self.character.y += 2;
  244. self.balloon.x++;
  245. self.basket.x++;
  246. self.character.x++;
  247. } else {
  248. self.preAnimate = false;
  249. self.animate = true;
  250. game.animation.play(self.character.animation[0]);
  251. }
  252. }
  253. // Character running
  254. if (self.animate) {
  255. if (self.character.x <= 700) {
  256. self.character.x += 2;
  257. } else {
  258. self.animate = false;
  259. game.animation.stop(self.character.animation[0]);
  260. // FOR MOODLE
  261. if (!moodle) {
  262. completedLevels = 0;
  263. game.state.start('menu');
  264. } else {
  265. // FOR MOODLE
  266. parent.location.reload(true);
  267. }
  268. }
  269. }
  270. game.render.all();
  271. }
  272. };