map.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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, defaultWidth, defaultHeight, 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 = 4 * 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 (gameTypeString == '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 (gameTypeString == '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('' + gameTypeString);
  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. navigationIcons.onInputDown(mouseEvent.offsetX, mouseEvent.offsetY);
  151. },
  152. /**
  153. * Called by mouse move event
  154. *
  155. * @param {object} mouseEvent contains the mouse move coordinates
  156. */
  157. onInputOver: function (mouseEvent) {
  158. navigationIcons.onInputOver(mouseEvent.offsetX, mouseEvent.offsetY);
  159. }
  160. };
  161. /** [ENDING STATE] Ending screen shown when the player has completed all 4 levels and therefore completed the game.
  162. *
  163. * @namespace
  164. */
  165. const endState = {
  166. /**
  167. * Main code
  168. */
  169. create: function () {
  170. self.preAnimate = false;
  171. self.animate = true;
  172. // Background color
  173. game.add.geom.rect(0, 0, defaultWidth, defaultHeight, undefined, 0, colors.blueBckg, 1);
  174. // Background
  175. game.add.image(0, 0, 'bgimage');
  176. // Clouds
  177. game.add.image(300, 100, 'cloud');
  178. game.add.image(660, 80, 'cloud');
  179. game.add.image(110, 85, 'cloud', 0.8);
  180. // Floor
  181. for (let i = 0; i < 9; i++) { game.add.image(i * 100, defaultHeight - 100, 'floor'); }
  182. // Progress bar
  183. game.add.geom.rect(660, 10, 4 * 37.5, 35, undefined, 0, colors.intenseGreen, 0.5); // Progress
  184. game.add.geom.rect(661, 11, 149, 34, colors.blue, 3, undefined, 1); // Box
  185. game.add.text(820, 38, '100%', textStyles.h2_blue).align = 'left';
  186. game.add.text(650, 38, game.lang.difficulty + ' ' + gameDifficulty, textStyles.h2_blue).align = 'right';
  187. game.add.image(360, 545, 'tree4', 0.7).anchor(0, 1);
  188. // Level character
  189. switch (gameTypeString) {
  190. case 'circleOne':
  191. this.preAnimate = true;
  192. this.animate = false;
  193. // School
  194. game.add.image(600, 222, 'school', 0.7);
  195. // Kid
  196. this.character = game.add.sprite(0, -152, 'kid_run', 0, 0.7);
  197. this.character.anchor(0.5, 0.5);
  198. this.character.animation = ['move', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3];
  199. // Balloon
  200. this.balloon = game.add.image(0, -260, 'balloon');
  201. this.balloon.anchor(0.5, 0.5);
  202. this.basket = game.add.image(0, -150, 'balloon_basket');
  203. this.basket.anchor(0.5, 0.5);
  204. break;
  205. case 'squareTwo':
  206. // School
  207. game.add.image(600, 222, 'school', 0.7);
  208. // Kid
  209. this.character = game.add.sprite(0, 460, 'kid_run', 6, 0.7);
  210. this.character.anchor(0.5, 0.5);
  211. this.character.animation = ['move', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3];
  212. break;
  213. case 'squareOne':
  214. // Farm
  215. game.add.image(650, 260, 'farm', 1.1);
  216. // Tractor
  217. this.character = game.add.sprite(0, 490, 'tractor', 0, 0.7);
  218. this.character.anchor(0.5, 0.5);
  219. if (gameOperation == 'Plus') {
  220. this.character.animation = ['move', [0, 1, 2, 3, 4], 4];
  221. } else {
  222. this.character.curFrame = 10;
  223. this.character.animation = ['move', [10, 11, 12, 13, 14], 4];
  224. }
  225. break;
  226. }
  227. if (this.animate) game.animation.play(this.character.animation[0]);
  228. game.add.image(30, 585, 'tree4', 0.85).anchor(0, 1);
  229. },
  230. /**
  231. * Game loop
  232. */
  233. update: function () {
  234. // Balloon falling
  235. if (self.preAnimate) {
  236. if (self.character.y < 460) {
  237. self.balloon.y += 2;
  238. self.basket.y += 2;
  239. self.character.y += 2;
  240. self.balloon.x++;
  241. self.basket.x++;
  242. self.character.x++;
  243. } else {
  244. self.preAnimate = false;
  245. self.animate = true;
  246. game.animation.play(self.character.animation[0]);
  247. }
  248. }
  249. // Character running
  250. if (self.animate) {
  251. if (self.character.x <= 700) {
  252. self.character.x += 2;
  253. } else {
  254. self.animate = false;
  255. game.animation.stop(self.character.animation[0]);
  256. // FOR MOODLE
  257. if (!moodle) {
  258. completedLevels = 0;
  259. game.state.start('menu');
  260. }
  261. }
  262. }
  263. game.render.all();
  264. }
  265. };