map.js 10 KB

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