map.js 8.9 KB

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