map.js 9.2 KB

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