map.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. let mapState = {
  3. create: function(){},
  4. update: function(){},
  5. ---------------------------- end of phaser functions
  6. func_loadGame: function(){},
  7. }
  8. let endState = {
  9. create: function(){},
  10. update: function(){},
  11. ---------------------------- end of phaser functions
  12. };
  13. */
  14. // MAP SCREEN: game map where character advances as he passes a level
  15. let mapState = {
  16. create: function () {
  17. // Background
  18. game.add.image(0, 40, 'bgmap');
  19. // Calls function that loads navigation icons
  20. iconSettings.func_addIcons(true, false,
  21. true, true, false,
  22. false, false,
  23. 'difficulty', false);
  24. // Progress bar
  25. let percentText = completedLevels * 25;
  26. let percentBlocks = completedLevels;
  27. for (let p = 0; p < percentBlocks; p++) {
  28. let block = game.add.image(660 + p * 37.5, 10, 'block');
  29. block.scale.setTo(2.6, 1);
  30. }
  31. game.add.text(820, 10, percentText + '%', textStyles.subtitle2);
  32. game.add.text(650, 10, lang.difficulty + ' ' + levelDifficulty, textStyles.subtitle2).anchor.setTo(1, 0);
  33. game.add.image(660, 10, 'pgbar');
  34. // Road
  35. this.points = {
  36. 'x': [90, 204, 318, 432, 546, 660],
  37. 'y': [486, 422, 358, 294, 230, 166]
  38. };
  39. if (currentGameState == "gameSquareOne") {
  40. //Garage
  41. let garage = game.add.image(this.points.x[0], this.points.y[0], 'garage');
  42. garage.scale.setTo(0.4);
  43. garage.anchor.setTo(0.5, 1);
  44. //Farm
  45. let farm = game.add.image(this.points.x[5], this.points.y[5], 'farm');
  46. farm.scale.setTo(0.6);
  47. farm.anchor.setTo(0.1, 0.7);
  48. } else {
  49. //House
  50. let house = game.add.image(this.points.x[0], this.points.y[0], 'house');
  51. house.scale.setTo(0.7);
  52. house.anchor.setTo(0.7, 0.8);
  53. //School
  54. let school = game.add.image(this.points.x[5], this.points.y[5], 'school');
  55. school.scale.setTo(0.35);
  56. school.anchor.setTo(0.2, 0.7);
  57. }
  58. //Trees and Rocks
  59. const rocks = {
  60. 'x': [156, 275, 276, 441, 452, 590, 712],
  61. 'y': [309, 543, 259, 156, 419, 136, 316]
  62. }
  63. const r_types = [1, 1, 2, 1, 2, 2, 2];
  64. for (let i = 0; i < r_types.length; i++) {
  65. if (r_types[i] == 1) {
  66. let sprite = game.add.image(rocks.x[i], rocks.y[i], 'rock');
  67. sprite.scale.setTo(0.32);
  68. sprite.anchor.setTo(0.5, 0.95);
  69. } else if (r_types[i] == 2) {
  70. let sprite = game.add.image(rocks.x[i], rocks.y[i], 'birch');
  71. sprite.scale.setTo(0.4);
  72. sprite.anchor.setTo(0.5, 0.95);
  73. }
  74. }
  75. const trees = {
  76. 'x': [105, 214, 354, 364, 570, 600, 740, 779],
  77. 'y': [341, 219, 180, 520, 550, 392, 488, 286]
  78. }
  79. const t_types = [2, 4, 3, 4, 1, 2, 4, 4];
  80. for (let i = 0; i < t_types.length; i++) {
  81. const sprite = game.add.image(trees.x[i], trees.y[i], 'tree' + t_types[i]);
  82. sprite.scale.setTo(0.6);
  83. sprite.anchor.setTo(0.5, 0.95);
  84. }
  85. // Map positions
  86. for (let p = 1; p < this.points.x.length - 1; p++) {
  87. const aux = (p < mapPosition || (mapCanMove && p == mapPosition)) ? 'place_b' : 'place_a';
  88. const place = game.add.image(this.points.x[p], this.points.y[p], aux);
  89. place.anchor.setTo(0.5, 0.5);
  90. place.scale.setTo(0.3);
  91. const sign = game.add.image(this.points.x[p] - 20, this.points.y[p] - 60, 'sign');
  92. sign.anchor.setTo(0.5, 1);
  93. sign.scale.setTo(0.4);
  94. if (p > 0 && p < this.points.x.length - 1) {
  95. const text = game.add.text(this.points.x[p] - 23, this.points.y[p] - 84, p, textStyles.difficultyLabel);
  96. text.anchor.setTo(0.35, 0.5);
  97. }
  98. }
  99. if (currentGameState == "gameSquareOne") {
  100. this.character = game.add.sprite(this.points.x[mapPosition], this.points.y[mapPosition], 'tractor');
  101. if (sublevelType == 'Plus') {
  102. this.character.animations.add('walk', [0, 1, 2, 3, 4]);
  103. this.character.scale.setTo(0.5);
  104. } else {
  105. this.character.animations.add('walk', [5, 6, 7, 8, 9]);
  106. this.character.scale.setTo(-0.5, 0.5);
  107. }
  108. this.character.animations.play('walk', 5, true);
  109. } else {
  110. this.character = game.add.sprite(this.points.x[mapPosition], this.points.y[mapPosition], 'kid_run');
  111. this.character.animations.add('run');
  112. this.character.animations.play('run', 6, true);
  113. this.character.scale.setTo(0.5);
  114. }
  115. this.character.anchor.setTo(0.5, 1);
  116. this.character.angle -= 25;
  117. game.physics.arcade.enable(this.character);
  118. // Delay to next level
  119. this.count = 0;
  120. this.wait = 60;
  121. },
  122. update: function () {
  123. // Wait 2 seconds before moving or staring a game
  124. this.count++;
  125. if (this.count <= this.wait) return;
  126. if (!mapCanMove) {
  127. this.func_loadGame();
  128. }
  129. // If momevent is enabled, move to next point from actual
  130. if (mapCanMove) {
  131. game.physics.arcade.moveToXY(
  132. this.character,
  133. this.points.x[mapPosition + 1],
  134. this.points.y[mapPosition + 1],
  135. 100
  136. );
  137. // I kid/tractor reached the end, stop movement
  138. if (Math.ceil(this.character.x) == this.points.x[mapPosition + 1] || Math.ceil(this.character.y) == this.points.y[mapPosition + 1]) {
  139. mapCanMove = false;
  140. mapPosition += 1; //Update position
  141. }
  142. }
  143. },
  144. //MapLoading function
  145. func_loadGame: function () {
  146. if (audioStatus) sound.beepSound.play();
  147. if (mapPosition <= 4) game.state.start(currentGameState);
  148. else game.state.start('end');
  149. }
  150. };
  151. // ENDING SCREEN: animation after a full level is completed
  152. let endState = {
  153. create: function () {
  154. // Background
  155. game.add.image(0, 0, 'bgimage');
  156. //Clouds
  157. game.add.image(300, 100, 'cloud');
  158. game.add.image(660, 80, 'cloud');
  159. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  160. //Floor
  161. for (let i = 0; i < 9; i++) {
  162. game.add.image(i * 100, 501, 'floor');
  163. }
  164. // Progress bar
  165. for (let p = 0; p < 5; p++) {
  166. let block = game.add.image(660 + p * 30, 10, 'block');
  167. block.scale.setTo(2, 1); //Scaling to double width
  168. }
  169. game.add.text(820, 10, '100%', textStyles.subtitle2);
  170. game.add.text(650, 10, lang.difficulty + ' ' + levelDifficulty, textStyles.subtitle2).anchor.setTo(1, 0);
  171. game.add.image(660, 10, 'pgbar');
  172. game.add.sprite(30, 280, 'tree4');
  173. game.add.sprite(360, 250, 'tree2');
  174. if (currentGameState == 'gameCircleOne') {
  175. //School and trees
  176. game.add.sprite(600, 222, 'school').scale.setTo(0.7);
  177. //kid
  178. this.kid = game.add.sprite(0, -152, 'kid_run');
  179. this.kid.anchor.setTo(0.5, 0.5);
  180. this.kid.scale.setTo(0.7);
  181. this.kid.animations.add('walk', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
  182. //globo
  183. this.balloon = game.add.sprite(0, -260, 'balloon');
  184. this.balloon.anchor.setTo(0.5, 0.5);
  185. this.basket = game.add.sprite(0, -150, 'balloon_basket');
  186. this.basket.anchor.setTo(0.5, 0.5);
  187. } else if (currentGameState == 'gameSquareTwo') {
  188. //School and trees
  189. game.add.sprite(600, 222, 'school').scale.setTo(0.7);
  190. //kid
  191. this.kid = game.add.sprite(0, 460, 'kid_run');
  192. this.kid.anchor.setTo(0.5, 0.5);
  193. this.kid.scale.setTo(0.7);
  194. this.kid.animations.add('walk', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
  195. this.kid.animations.play('walk', 6, true);
  196. } else if (currentGameState == 'gameSquareOne') {
  197. //Farm and trees
  198. game.add.sprite(650, 260, 'farm').scale.setTo(1.1);
  199. //tractor
  200. this.tractor = game.add.sprite(0, 490, 'tractor');
  201. this.tractor.anchor.setTo(0.5, 0.5);
  202. this.tractor.scale.setTo(0.8);
  203. if (sublevelType == 'Plus') this.tractor.animations.add('tractor_run', [0, 1, 2, 3, 4]);
  204. else {
  205. this.tractor.animations.add('tractor_run', [5, 6, 7, 8, 9]);
  206. this.tractor.scale.x *= -1;
  207. }
  208. this.tractor.animations.play('tractor_run', 5, true);
  209. } else {
  210. if (debugMode) console.log("Error! Name of the game state is not valid!");
  211. }
  212. },
  213. update: function () {
  214. if (currentGameState == 'gameCircleOne') {
  215. if (this.kid.y >= 460) {
  216. this.kid.animations.play('walk', 6, true);
  217. if (this.kid.x <= 700) {
  218. this.kid.x += 2;
  219. } else {
  220. completedLevels = 0;
  221. game.state.start('menu');
  222. }
  223. } else {
  224. this.balloon.y += 2;
  225. this.basket.y += 2;
  226. this.kid.y += 2;
  227. this.balloon.x += 1;
  228. this.basket.x += 1;
  229. this.kid.x += 1;
  230. }
  231. } else if (currentGameState == 'gameSquareTwo') {
  232. if (this.kid.x <= 700) {
  233. this.kid.x += 2;
  234. } else {
  235. completedLevels = 0;
  236. game.state.start('menu');
  237. }
  238. } else if (currentGameState == 'gameSquareOne') {
  239. if (this.tractor.x <= 700) {
  240. this.tractor.x += 2;
  241. } else {
  242. completedLevels = 0;
  243. game.state.start('menu');
  244. }
  245. } else {
  246. if (debugMode) console.log("Error! Name of the game state is not valid!");
  247. }
  248. },
  249. }