map.js 10 KB

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