map.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. let x0 = 306;
  14. let y0 = 161;
  15. this.scene = {
  16. rocks: {
  17. x: [x0 + 172, x0 + 604, x0 + 353],
  18. y: [y0 + 319, y0 + 88, y0 + 667, ,],
  19. },
  20. bushes: {
  21. x: [x0 + 344, x0 + 822, x0 + 1006, x0 + 613],
  22. y: [y0 + 224, y0 + 43, y0 + 310, y0 + 464],
  23. },
  24. trees: {
  25. x: [
  26. x0 + 26,
  27. x0 + 776,
  28. x0 + 401,
  29. x0 + 1006,
  30. x0 + 204,
  31. x0 + 1065,
  32. x0 + 435,
  33. x0 + 728,
  34. ],
  35. y: [
  36. y0 + 174,
  37. y0 + 250,
  38. y0 - 67,
  39. y0 + 417,
  40. y0 + 16,
  41. y0 + 116,
  42. y0 + 435,
  43. y0 + 511,
  44. ],
  45. type: [2, 2, 3, 4, 4, 4, 4, 1],
  46. },
  47. roadPoints: {
  48. x: [x0 + 78, x0 + 251, x0 + 424, x0 + 598, x0 + 770, x0 + 943],
  49. y: [y0 + 629, y0 + 536, y0 + 443, y0 + 347, y0 + 252, y0 + 159],
  50. },
  51. };
  52. this.waitUserAction = false;
  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. // Progress bar
  132. const percentText = completedLevels * 25;
  133. x0 = 300;
  134. y0 = 20;
  135. game.add.geom.rect(
  136. context.canvas.width - x0,
  137. y0,
  138. completedLevels * 37.5,
  139. 35,
  140. undefined,
  141. 0,
  142. completedLevels >= 4 ? colors.greenNeon : colors.yellow,
  143. 1
  144. );
  145. game.add.geom.rect(
  146. context.canvas.width - x0 + 1,
  147. y0 + 1,
  148. 149,
  149. 34,
  150. colors.blue,
  151. 3,
  152. undefined,
  153. 1
  154. );
  155. // Status Box
  156. game.add.text(
  157. context.canvas.width - x0 + 160,
  158. y0 + 33,
  159. percentText + '%',
  160. textStyles.h2_
  161. ).align = 'left';
  162. game.add.text(
  163. context.canvas.width - x0 - 10,
  164. y0 + 33,
  165. game.lang.difficulty + ' ' + gameDifficulty,
  166. textStyles.h2_
  167. ).align = 'right';
  168. // feedback
  169. this.modalBg = game.add.geom.rect(
  170. 0,
  171. 0,
  172. context.canvas.width,
  173. context.canvas.height,
  174. undefined,
  175. 0,
  176. colors.white,
  177. 0
  178. );
  179. this.continueButton = game.add.geom.rect(
  180. context.canvas.width / 2,
  181. context.canvas.height / 2,
  182. 300,
  183. 100,
  184. undefined,
  185. 0,
  186. colors.green,
  187. 0
  188. );
  189. this.continueButton.anchor(0.5, 0.5);
  190. // continue
  191. // try again?
  192. this.continueText = game.add.text(
  193. context.canvas.width / 2,
  194. context.canvas.height / 2 + 16,
  195. game.lang.continue,
  196. textStyles.btn
  197. );
  198. this.continueText.alpha = 0;
  199. // FOR MOODLE
  200. if (moodle) {
  201. navigationIcons.add(
  202. false,
  203. false,
  204. false, // Left icons
  205. false,
  206. false, // Right icons
  207. false,
  208. false
  209. );
  210. } else {
  211. navigationIcons.add(
  212. true,
  213. true,
  214. false, // Left icons
  215. false,
  216. false, // Right icons
  217. 'customMenu',
  218. false
  219. );
  220. }
  221. game.event.add('click', this.onInputDown);
  222. game.event.add('mousemove', this.onInputOver);
  223. },
  224. /**
  225. * Game loop
  226. */
  227. update: function () {
  228. self.moveCounter++;
  229. if (isDebugMode && debugState.end.status) {
  230. curMapPosition = 4;
  231. }
  232. if (isDebugMode && debugState.map.status) {
  233. // programmatically skip map
  234. curMapPosition++;
  235. self.loadGame();
  236. }
  237. if (self.moveCounter > 60) {
  238. // Wait 1 second before moving or staring a game
  239. if (canGoToNextMapPosition) {
  240. // Move character on screen for 1 second
  241. self.character.x += self.speedX;
  242. self.character.y -= self.speedY;
  243. if (
  244. Math.ceil(self.character.x) >=
  245. self.scene.roadPoints.x[curMapPosition + 1]
  246. ) {
  247. // Reached next map position
  248. canGoToNextMapPosition = false;
  249. curMapPosition++; // Set new next position
  250. }
  251. }
  252. if (!canGoToNextMapPosition) {
  253. self.waitUserAction = true;
  254. self.modalBg.alpha = 0.25;
  255. self.continueText.alpha = 1;
  256. self.continueButton.alpha = 1;
  257. //endUpdate = true;
  258. }
  259. }
  260. game.render.all();
  261. },
  262. /**
  263. * Calls game state
  264. */
  265. loadGame: function () {
  266. if (curMapPosition <= 4) game.state.start('' + gameName);
  267. else game.state.start('end');
  268. },
  269. /**
  270. * Called by mouse click event
  271. *
  272. * @param {object} mouseEvent contains the mouse click coordinates
  273. */
  274. onInputDown: function (mouseEvent) {
  275. const x = game.math.getMouse(mouseEvent).x;
  276. const y = game.math.getMouse(mouseEvent).y;
  277. if (game.math.isOverIcon(x, y, self.continueButton)) {
  278. self.loadGame();
  279. }
  280. navigationIcons.onInputDown(x, y);
  281. },
  282. /**
  283. * Called by mouse move event
  284. *
  285. * @param {object} mouseEvent contains the mouse move coordinates
  286. */
  287. onInputOver: function (mouseEvent) {
  288. const x = game.math.getMouse(mouseEvent).x;
  289. const y = game.math.getMouse(mouseEvent).y;
  290. let overIcon;
  291. if (game.math.isOverIcon(x, y, self.continueButton)) {
  292. overIcon = true;
  293. }
  294. // Update gui
  295. if (overIcon) {
  296. // If pointer is over icon
  297. document.body.style.cursor = 'pointer';
  298. self.continueButton.scale = self.continueButton.originalScale * 1.1;
  299. self.continueText.style = textStyles.btnLg;
  300. } else {
  301. // If pointer is not over icon
  302. self.continueButton.scale = self.continueButton.originalScale * 1;
  303. self.continueText.style = textStyles.btn;
  304. document.body.style.cursor = 'auto';
  305. }
  306. navigationIcons.onInputOver(x, y);
  307. },
  308. };