map.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. const x0 = 306;
  14. const y0 = 161;
  15. this.waitUserAction = false;
  16. this.scene = {
  17. rocks: {
  18. x: [x0 + 172, x0 + 604, x0 + 353],
  19. y: [y0 + 319, y0 + 88, y0 + 667, ,],
  20. },
  21. bushes: {
  22. x: [x0 + 344, x0 + 822, x0 + 1006, x0 + 613],
  23. y: [y0 + 224, y0 + 43, y0 + 310, y0 + 464],
  24. },
  25. trees: {
  26. x: [
  27. x0 + 26,
  28. x0 + 776,
  29. x0 + 401,
  30. x0 + 1006,
  31. x0 + 204,
  32. x0 + 1065,
  33. x0 + 435,
  34. x0 + 728,
  35. ],
  36. y: [
  37. y0 + 174,
  38. y0 + 250,
  39. y0 - 67,
  40. y0 + 417,
  41. y0 + 16,
  42. y0 + 116,
  43. y0 + 435,
  44. y0 + 511,
  45. ],
  46. type: [2, 2, 3, 4, 4, 4, 4, 1],
  47. },
  48. roadPoints: {
  49. x: [x0 + 78, x0 + 251, x0 + 424, x0 + 598, x0 + 770, x0 + 943],
  50. y: [y0 + 629, y0 + 536, y0 + 443, y0 + 347, y0 + 252, y0 + 159],
  51. },
  52. };
  53. renderBackground('plain');
  54. // Map
  55. game.add.image(x0, y0, 'bg_map', 1.5);
  56. // Buildings
  57. gameList[gameId].assets.map.startBuilding();
  58. gameList[gameId].assets.map.endBuilding();
  59. // Rocks
  60. for (let i in this.scene.rocks.x) {
  61. game.add.image(
  62. this.scene.rocks.x[i],
  63. this.scene.rocks.y[i],
  64. 'rock',
  65. 0.48
  66. );
  67. }
  68. // Bushes
  69. for (let i in this.scene.bushes.x) {
  70. game.add.image(
  71. this.scene.bushes.x[i],
  72. this.scene.bushes.y[i],
  73. 'bush',
  74. 0.59
  75. );
  76. }
  77. // Trees
  78. for (let i in this.scene.trees.x) {
  79. game.add.image(
  80. this.scene.trees.x[i],
  81. this.scene.trees.y[i],
  82. 'tree_' + this.scene.trees.type[i],
  83. 0.9
  84. );
  85. }
  86. //Road points
  87. for (let i = 1; i < this.scene.roadPoints.x.length - 1; i++) {
  88. const frame =
  89. i < curMapPosition || (canGoToNextMapPosition && i == curMapPosition)
  90. ? 1
  91. : 0;
  92. // Road points (game levels)
  93. game.add.sprite(
  94. this.scene.roadPoints.x[i],
  95. this.scene.roadPoints.y[i],
  96. 'map_place',
  97. frame,
  98. 0.45
  99. );
  100. // Road signs
  101. game.add.image(
  102. this.scene.roadPoints.x[i] - 40,
  103. this.scene.roadPoints.y[i] - 154,
  104. 'sign',
  105. 0.7
  106. );
  107. game.add.text(
  108. this.scene.roadPoints.x[i],
  109. this.scene.roadPoints.y[i] - 104,
  110. i,
  111. {
  112. ...textStyles.h2_,
  113. fill: colors.white,
  114. }
  115. );
  116. }
  117. // Character
  118. this.character = gameList[gameId].assets.map.character(gameOperation);
  119. // Character animation
  120. this.character.animation =
  121. gameList[gameId].assets.map.characterAnimation(gameOperation);
  122. game.animation.play(this.character.animation[0]);
  123. this.moveCounter = 0;
  124. const speed = 60;
  125. const xA = this.scene.roadPoints.x[curMapPosition];
  126. const yA = this.scene.roadPoints.y[curMapPosition];
  127. const xB = this.scene.roadPoints.x[curMapPosition + 1];
  128. const yB = this.scene.roadPoints.y[curMapPosition + 1];
  129. self.speedX = (xB - xA) / speed;
  130. self.speedY = (yA - yB) / speed;
  131. self.renderProgressBar();
  132. // feedback
  133. this.modalBg = game.add.geom.rect(
  134. 0,
  135. 0,
  136. context.canvas.width,
  137. context.canvas.height,
  138. colors.black,
  139. 0
  140. );
  141. this.continueButton = game.add.geom.rect(
  142. context.canvas.width / 2,
  143. context.canvas.height / 2,
  144. 300,
  145. 100,
  146. colors.green,
  147. 0
  148. );
  149. this.continueButton.anchor(0.5, 0.5);
  150. // continue
  151. // try again?
  152. this.continueText = game.add.text(
  153. context.canvas.width / 2,
  154. context.canvas.height / 2 + 16,
  155. game.lang.continue,
  156. textStyles.btn
  157. );
  158. this.continueText.alpha = 0;
  159. // FOR MOODLE
  160. if (moodle) {
  161. } else {
  162. navigation.add.left(['back', 'menu'], 'customMenu');
  163. }
  164. game.event.add('click', this.onInputDown);
  165. game.event.add('mousemove', this.onInputOver);
  166. },
  167. /**
  168. * Game loop
  169. */
  170. update: function () {
  171. self.moveCounter++;
  172. if (isDebugMode && debugState.end.skip) {
  173. curMapPosition = 4;
  174. }
  175. if (isDebugMode && debugState.map.skip) {
  176. // programmatically skip map
  177. curMapPosition++;
  178. self.loadGame();
  179. }
  180. if (self.moveCounter > 60) {
  181. // Wait 1 second before moving or staring a game
  182. if (canGoToNextMapPosition) {
  183. // Move character on screen for 1 second
  184. self.character.x += self.speedX;
  185. self.character.y -= self.speedY;
  186. if (
  187. Math.ceil(self.character.x) >=
  188. self.scene.roadPoints.x[curMapPosition + 1]
  189. ) {
  190. // Reached next map position
  191. canGoToNextMapPosition = false;
  192. curMapPosition++; // Set new next position
  193. }
  194. }
  195. if (!canGoToNextMapPosition) {
  196. self.waitUserAction = true;
  197. self.modalBg.alpha = 0.25;
  198. self.continueText.alpha = 1;
  199. self.continueButton.alpha = 1;
  200. //endUpdate = true;
  201. }
  202. }
  203. game.render.all();
  204. },
  205. /**
  206. * Calls game state
  207. */
  208. loadGame: function () {
  209. if (curMapPosition <= 4) game.state.start('' + gameName);
  210. else game.state.start('end');
  211. },
  212. /**
  213. * Called by mouse click event
  214. *
  215. * @param {object} mouseEvent contains the mouse click coordinates
  216. */
  217. onInputDown: function (mouseEvent) {
  218. const x = game.math.getMouse(mouseEvent).x;
  219. const y = game.math.getMouse(mouseEvent).y;
  220. if (game.math.isOverIcon(x, y, self.continueButton)) {
  221. if (audioStatus) game.audio.popSound.play();
  222. self.loadGame();
  223. }
  224. navigation.onInputDown(x, y);
  225. },
  226. /**
  227. * Called by mouse move event
  228. *
  229. * @param {object} mouseEvent contains the mouse move coordinates
  230. */
  231. onInputOver: function (mouseEvent) {
  232. const x = game.math.getMouse(mouseEvent).x;
  233. const y = game.math.getMouse(mouseEvent).y;
  234. let overIcon;
  235. if (game.math.isOverIcon(x, y, self.continueButton)) {
  236. overIcon = true;
  237. }
  238. // Update gui
  239. if (overIcon) {
  240. // If pointer is over icon
  241. document.body.style.cursor = 'pointer';
  242. self.continueButton.scale = self.continueButton.initialScale * 1.1;
  243. self.continueText.style = textStyles.btnLg;
  244. } else {
  245. // If pointer is not over icon
  246. self.continueButton.scale = self.continueButton.initialScale * 1;
  247. self.continueText.style = textStyles.btn;
  248. document.body.style.cursor = 'auto';
  249. }
  250. navigation.onInputOver(x, y);
  251. },
  252. renderProgressBar: function () {
  253. const percentText = completedLevels * 25;
  254. const x0 = 300;
  255. const y0 = 20;
  256. // Bar content
  257. for (let i = 0; i < completedLevels; i++) {
  258. game.add.image(
  259. context.canvas.width - x0 + 37.5 * i,
  260. y0,
  261. 'progress_bar_tile'
  262. );
  263. }
  264. // Bar wrapper
  265. game.add.geom.rect(
  266. context.canvas.width - x0 + 1,
  267. y0 + 1,
  268. 150, //149,
  269. 35, //34,
  270. 'transparent',
  271. 1,
  272. colors.blue,
  273. 3
  274. );
  275. // percentage label
  276. game.add.text(
  277. context.canvas.width - x0 + 160,
  278. y0 + 33,
  279. percentText + '%',
  280. textStyles.h2_
  281. ).align = 'left';
  282. // Difficulty label
  283. game.add.text(
  284. context.canvas.width - x0 - 10,
  285. y0 + 33,
  286. game.lang.difficulty + ' ' + gameDifficulty,
  287. textStyles.h2_
  288. ).align = 'right';
  289. },
  290. };