map.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. renderBackground('plain');
  14. // Calls function that loads navigation icons
  15. // FOR MOODLE
  16. if (moodle) {
  17. navigationIcons.add(
  18. false,
  19. false,
  20. false, // Left icons
  21. false,
  22. false, // Right icons
  23. false,
  24. false
  25. );
  26. } else {
  27. navigationIcons.add(
  28. true,
  29. true,
  30. false, // Left icons
  31. false,
  32. false, // Right icons
  33. 'customMenu',
  34. false
  35. );
  36. }
  37. // console.log('debugState');
  38. const xAdjust = 0;
  39. const yAdjust = 200;
  40. let xInitial = 90 + 40;
  41. let yInitial = 486 + 20;
  42. let xOffset = 114 * 1.5;
  43. let yOffset = -64 * 1.5;
  44. this.points = {
  45. x: [
  46. xInitial + xOffset * 0 + xAdjust,
  47. xInitial + xOffset * 1 + xAdjust,
  48. xInitial + xOffset * 2 + xAdjust * 2,
  49. xInitial + xOffset * 3 + xAdjust * 3,
  50. xInitial + xOffset * 4 + xAdjust * 4,
  51. xInitial + xOffset * 5 + xAdjust * 5,
  52. ],
  53. y: [
  54. yInitial + yOffset * 0 + yAdjust,
  55. yInitial + yOffset * 1 + yAdjust,
  56. yInitial + yOffset * 2 + yAdjust,
  57. yInitial + yOffset * 3 + yAdjust,
  58. yInitial + yOffset * 4 + yAdjust,
  59. yInitial + yOffset * 5 + yAdjust,
  60. ], // origem, placa 1, placa 2, placa 3, destino
  61. };
  62. xOffset = 60;
  63. yOffset = 100;
  64. const rocks = {
  65. x: [
  66. 156 + xOffset,
  67. 276 + xOffset * 2,
  68. 441 + xOffset * 4,
  69. 590 + xOffset * 3,
  70. 275 + xOffset,
  71. 452 + xOffset * 3,
  72. 712 + xOffset * 4.5,
  73. ],
  74. y: [
  75. 309 + yOffset,
  76. 259 + yOffset,
  77. 156 + yOffset - 50,
  78. 136 + yOffset - 75,
  79. 543 + yOffset * 2.5,
  80. 419 + yOffset * 2,
  81. 316 + yOffset * 1.8,
  82. ],
  83. type: [1, 2, 1, 2, 1, 2, 2],
  84. };
  85. yOffset = 100;
  86. const trees = {
  87. x: [
  88. 105 + 50,
  89. 214 + 100,
  90. 354 + 200,
  91. 364 + 150,
  92. 570 + 200,
  93. 600 + 200,
  94. 740 + 310,
  95. 779 + 300,
  96. ],
  97. y: [
  98. 341 + yOffset,
  99. 219 + yOffset - 40,
  100. 180 + yOffset - 50,
  101. 520 + yOffset * 2.5,
  102. 550 + yOffset * 2.5,
  103. 392 + yOffset * 2,
  104. 488 + yOffset * -1,
  105. 286 + yOffset * 4,
  106. ],
  107. type: [2, 4, 3, 4, 1, 2, 4, 4],
  108. };
  109. const hOffset = gameFrame().y - 200;
  110. const wOffset = gameFrame().x;
  111. for (let i = 0, cur = this.points; i < cur.x.length; i++) {
  112. cur.x[i] += wOffset;
  113. cur.y[i] += hOffset;
  114. }
  115. for (let i = 0, cur = rocks; i < cur.x.length; i++) {
  116. cur.x[i] += wOffset;
  117. cur.y[i] += hOffset;
  118. }
  119. for (let i = 0, cur = trees; i < cur.x.length; i++) {
  120. cur.x[i] += wOffset;
  121. cur.y[i] += hOffset;
  122. }
  123. // Map
  124. game.add.image(wOffset, hOffset + 40, 'bg_map', 1.5);
  125. // Progress bar
  126. const percentText = completedLevels * 25;
  127. let y = 20;
  128. if (completedLevels >= 4)
  129. game.add.geom.rect(
  130. context.canvas.width - 240,
  131. y,
  132. 4 * 37.5,
  133. 35,
  134. undefined,
  135. 0,
  136. colors.greenNeon,
  137. 0.5
  138. );
  139. else
  140. game.add.geom.rect(
  141. context.canvas.width - 240,
  142. y,
  143. completedLevels * 37.5,
  144. 35,
  145. undefined,
  146. 0,
  147. colors.yellow,
  148. 0.9
  149. );
  150. game.add.geom.rect(
  151. context.canvas.width - 240 + 1,
  152. y + 1,
  153. 149,
  154. 34,
  155. colors.blue,
  156. 3,
  157. undefined,
  158. 1
  159. );
  160. // Status Box
  161. game.add.text(
  162. context.canvas.width - 240 + 160,
  163. y + 33,
  164. percentText + '%',
  165. textStyles.h2_blueDark
  166. ).align = 'left';
  167. game.add.text(
  168. context.canvas.width - 240 - 10,
  169. y + 33,
  170. game.lang.difficulty + ' ' + gameDifficulty,
  171. textStyles.h2_blueDark
  172. ).align = 'right';
  173. // Map positions
  174. gameList[gameId].assets.mapStart();
  175. gameList[gameId].assets.mapEnd();
  176. // Rocks and bushes
  177. for (let i in rocks.type) {
  178. if (rocks.type[i] == 1) {
  179. game.add.image(rocks.x[i], rocks.y[i], 'rock', 0.6).anchor(0.5, 0.95);
  180. } else {
  181. game.add.image(rocks.x[i], rocks.y[i], 'bush', 0.7).anchor(0.5, 0.95);
  182. }
  183. }
  184. // Trees
  185. for (let i in trees.type) {
  186. game.add
  187. .image(trees.x[i], trees.y[i], 'tree_' + trees.type[i], 0.9)
  188. .anchor(0.5, 0.95);
  189. }
  190. // Map positions
  191. for (let i = 1; i < this.points.x.length - 1; i++) {
  192. const aux =
  193. i < curMapPosition || (canGoToNextMapPosition && i == curMapPosition)
  194. ? 'place_on'
  195. : 'place_off';
  196. // Map road positions - game levels
  197. game.add
  198. .image(this.points.x[i], this.points.y[i], aux, 0.45)
  199. .anchor(0.5, 0.5);
  200. // Map road signs - game level number
  201. game.add
  202. .image(this.points.x[i] - 20, this.points.y[i] - 100, 'sign', 0.6)
  203. .anchor(0.5, 1);
  204. game.add.text(
  205. this.points.x[i] - 20,
  206. this.points.y[i] - 125,
  207. i,
  208. textStyles.h2_white
  209. );
  210. }
  211. // Character
  212. this.character = gameList[gameId].assets.mapCharacter(gameOperation);
  213. this.character.animation =
  214. gameList[gameId].assets.mapCharacterAnimation(gameOperation);
  215. //this.character.anchor(0.5, 1);
  216. game.animation.play(this.character.animation[0]);
  217. this.moveCounter = 0;
  218. const speed = 60;
  219. const xA = this.points.x[curMapPosition];
  220. const yA = this.points.y[curMapPosition];
  221. const xB = this.points.x[curMapPosition + 1];
  222. const yB = this.points.y[curMapPosition + 1];
  223. self.speedX = (xB - xA) / speed;
  224. self.speedY = (yA - yB) / speed;
  225. game.event.add('click', this.onInputDown);
  226. game.event.add('mousemove', this.onInputOver);
  227. },
  228. /**
  229. * Game loop
  230. */
  231. update: function () {
  232. let endUpdate = false;
  233. self.moveCounter++;
  234. if (isDebugMode && debugState.end.status) {
  235. curMapPosition = 4;
  236. }
  237. if (isDebugMode && debugState.map.status) {
  238. // programmatically skip map
  239. if (debugState.map.stop) {
  240. self.moveCounter--;
  241. } else {
  242. curMapPosition++;
  243. self.loadGame();
  244. }
  245. }
  246. if (self.moveCounter > 60) {
  247. // Wait 1 second before moving or staring a game
  248. if (canGoToNextMapPosition) {
  249. // Move character on screen for 1 second
  250. self.character.x += self.speedX;
  251. self.character.y -= self.speedY;
  252. if (Math.ceil(self.character.x) >= self.points.x[curMapPosition + 1]) {
  253. // Reached next map position
  254. canGoToNextMapPosition = false;
  255. curMapPosition++; // Set new next position
  256. }
  257. }
  258. if (!canGoToNextMapPosition) {
  259. endUpdate = true;
  260. }
  261. }
  262. game.render.all();
  263. if (endUpdate) {
  264. game.animation.stop(self.character.animation[0]);
  265. self.loadGame();
  266. }
  267. },
  268. /**
  269. * Calls game state
  270. */
  271. loadGame: function () {
  272. if (curMapPosition <= 4) game.state.start('' + gameName);
  273. else game.state.start('end');
  274. },
  275. /**
  276. * Called by mouse click event
  277. *
  278. * @param {object} mouseEvent contains the mouse click coordinates
  279. */
  280. onInputDown: function (mouseEvent) {
  281. const x = game.math.getMouse(mouseEvent).x;
  282. const y = game.math.getMouse(mouseEvent).y;
  283. navigationIcons.onInputDown(x, y);
  284. },
  285. /**
  286. * Called by mouse move event
  287. *
  288. * @param {object} mouseEvent contains the mouse move coordinates
  289. */
  290. onInputOver: function (mouseEvent) {
  291. const x = game.math.getMouse(mouseEvent).x;
  292. const y = game.math.getMouse(mouseEvent).y;
  293. navigationIcons.onInputOver(x, y);
  294. },
  295. };