menu_custom.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [CUSTOM MENU STATE] Screen where the user can customise the selected game - game mode, math operation, level of difficulty.
  5. *
  6. * @namespace
  7. */
  8. const customMenuState = {
  9. /**
  10. * Preloads media for current state
  11. */
  12. preload: function () {
  13. // LOADING MEDIA
  14. game.load.sprite(url[gameName].sprite);
  15. game.load.image(url[gameName].image);
  16. },
  17. /**
  18. * Main code
  19. */
  20. create: function () {
  21. // FOR MOODLE
  22. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  23. // Student role
  24. game.state.start('map');
  25. } else {
  26. renderBackground();
  27. // Overtitle : Selected game
  28. game.add.text(
  29. context.canvas.width / 2,
  30. 60,
  31. game.lang.game.toUpperCase() + ': ' + menuState.menuIcons,
  32. { ...textStyles.h3_, fill: colors.maroon }
  33. );
  34. // Title : Customize the selected game
  35. game.add.text(context.canvas.width / 2, 120, game.lang.custom_game, {
  36. ...textStyles.h1_,
  37. fill: colors.green,
  38. });
  39. // Subtitle : <game mode>
  40. this.lbl_description = game.add.text(
  41. context.canvas.width / 2,
  42. 170,
  43. '',
  44. textStyles.h2_
  45. );
  46. // Loads navigation icons
  47. navigation.add.left(['back'], 'menu');
  48. navigation.add.right(['audio', 'lang']);
  49. const curGame = gameList[gameId];
  50. this.menuIcons = [];
  51. let offsetW = game.math.getOffset(getFrameInfo().width, 5);
  52. let offsetH = game.math.getOffset(
  53. getFrameInfo().height,
  54. curGame.gameMode.length
  55. );
  56. let x = getFrameInfo().x;
  57. let y = getFrameInfo().y;
  58. this.renderSectionTitles(x, y, offsetW, offsetH);
  59. this.renderCheckBox(x, y, offsetW, offsetH, curGame);
  60. this.renderModeSection(x, y, offsetW, offsetH, curGame);
  61. this.renderOperationSection(x, y, offsetW, offsetH, curGame);
  62. this.renderDifficultySection(x, y, offsetW, offsetH, curGame);
  63. this.renderEnterSection(x, y);
  64. this.setInfoBoxes();
  65. // ------------- EVENTS
  66. game.event.add('click', this.onInputDown);
  67. game.event.add('mousemove', this.onInputOver);
  68. if (isDebugMode && debugState.customMenu.skip) {
  69. // programmatically customize a game
  70. const { mode, operation, difficulty, label } =
  71. debugState.customMenu.getData();
  72. gameMode = mode;
  73. gameOperation = operation;
  74. gameDifficulty = difficulty || 1;
  75. showFractions = label || true;
  76. curMapPosition = 0; // Map position
  77. canGoToNextMapPosition = true; // Move no next point
  78. completedLevels = 0; // Reset the game progress when entering a new level
  79. game.state.start('map');
  80. }
  81. }
  82. },
  83. /**
  84. * Saves information selected by the player
  85. *
  86. * @param {object} icon selected icon
  87. */
  88. load: function (icon) {
  89. const type = icon.iconType;
  90. if (audioStatus) game.audio.popSound.play();
  91. switch (type) {
  92. case 'gameMode':
  93. gameMode = icon.gameMode;
  94. break;
  95. case 'gameOperation':
  96. gameOperation = icon.gameOperation;
  97. break;
  98. case 'difficulty':
  99. gameDifficulty = icon.difficulty;
  100. break;
  101. case 'infoIcon':
  102. self.showInfoBox(icon);
  103. break;
  104. case 'selectionBox':
  105. if (icon.curFrame == 0) {
  106. icon.curFrame = 1;
  107. showFractions = true;
  108. } else {
  109. icon.curFrame = 0;
  110. showFractions = false;
  111. }
  112. game.render.all();
  113. break;
  114. case 'enter':
  115. if (isDebugMode) {
  116. console.log(
  117. '------------------------------' +
  118. '\nGame Mode: ' +
  119. gameMode +
  120. '\nGame Operation: ' +
  121. gameOperation +
  122. '\nGame Difficulty: ' +
  123. gameDifficulty +
  124. '\nDisplay Fraction Labels: ' +
  125. showFractions +
  126. '\n------------------------------'
  127. );
  128. }
  129. curMapPosition = 0; // Map position
  130. canGoToNextMapPosition = true; // Move no next point
  131. completedLevels = 0; // Reset the game progress when entering a new level
  132. game.state.start('map');
  133. break;
  134. }
  135. },
  136. /**
  137. * Called by mouse click event
  138. *
  139. * @param {object} mouseEvent contains the mouse click coordinates
  140. */
  141. onInputDown: function (mouseEvent) {
  142. const x = game.math.getMouse(mouseEvent).x;
  143. const y = game.math.getMouse(mouseEvent).y;
  144. let overIcon;
  145. // Check if clicked on an icon
  146. for (let i in self.menuIcons) {
  147. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  148. overIcon = i;
  149. break;
  150. }
  151. }
  152. // Update gui
  153. if (overIcon) {
  154. // If has clicked on an icon
  155. document.body.style.cursor = 'pointer';
  156. self.menuIcons.forEach((cur) => {
  157. if (cur.iconType == self.menuIcons[overIcon].iconType) {
  158. // If its in the same icon category
  159. if (cur == self.menuIcons[overIcon]) {
  160. // If its the clicked icon
  161. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation')
  162. cur.curFrame = 1;
  163. else if (cur.iconType == 'difficulty') cur.curFrame = 0;
  164. } else {
  165. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation')
  166. cur.curFrame = 0;
  167. else if (cur.iconType == 'difficulty') cur.curFrame = 1;
  168. }
  169. }
  170. });
  171. self.load(self.menuIcons[overIcon]);
  172. } else document.body.style.cursor = 'auto';
  173. navigation.onInputDown(x, y);
  174. game.render.all();
  175. },
  176. /**
  177. * Called by mouse move event
  178. *
  179. * @param {object} mouseEvent contains the mouse move coordinates
  180. */
  181. onInputOver: function (mouseEvent) {
  182. const x = game.math.getMouse(mouseEvent).x;
  183. const y = game.math.getMouse(mouseEvent).y;
  184. let overIcon;
  185. // Check if pointer is over an icon
  186. for (let i in self.menuIcons) {
  187. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  188. overIcon = i;
  189. break;
  190. }
  191. }
  192. // Update gui
  193. if (overIcon) {
  194. // If pointer is over icon
  195. document.body.style.cursor = 'pointer';
  196. self.showTitle(self.menuIcons[overIcon]);
  197. self.menuIcons.forEach((cur) => {
  198. if (cur.iconType == self.menuIcons[overIcon].iconType) {
  199. // If its in the same icon category
  200. if (cur == self.menuIcons[overIcon]) {
  201. // If its the icon the pointer is over
  202. if (cur.iconType == 'enter')
  203. self.enterText.style = textStyles.btnLg;
  204. cur.scale = cur.initialScale * 1.1;
  205. } else {
  206. cur.scale = cur.initialScale;
  207. }
  208. }
  209. });
  210. } else {
  211. // If pointer is not over icon
  212. self.clearTitle();
  213. if (self.enterText) self.enterText.style = textStyles.btn;
  214. self.menuIcons.forEach((cur) => {
  215. cur.scale = cur.initialScale;
  216. });
  217. document.body.style.cursor = 'auto';
  218. }
  219. // Check navigation icons
  220. navigation.onInputOver(x, y);
  221. game.render.all();
  222. },
  223. renderSectionTitles: function (x, y, offsetW, offsetH) {
  224. let infoIcon;
  225. // Label 'Game Modes'
  226. game.add.text(x + offsetW, y, game.lang.game_modes, textStyles.h2_);
  227. infoIcon = game.add.image(x + 2 * offsetW - 30, y - 20, 'info', 0.7, 0.8);
  228. infoIcon.anchor(0.5, 0.5);
  229. infoIcon.iconType = 'infoIcon';
  230. infoIcon.id = 'gameMode';
  231. infoIcon.description = 'info_description';
  232. self.menuIcons.push(infoIcon);
  233. // Label 'Operations'
  234. game.add.text(x + 3 * offsetW, y, game.lang.operations, textStyles.h2_);
  235. infoIcon = game.add.image(x + 4 * offsetW - 30, y - 20, 'info', 0.7, 0.8);
  236. infoIcon.anchor(0.5, 0.5);
  237. infoIcon.iconType = 'infoIcon';
  238. infoIcon.id = 'gameOperation';
  239. infoIcon.description = 'info_description';
  240. self.menuIcons.push(infoIcon);
  241. // Label 'Difficulties'
  242. game.add.text(x + 5 * offsetW, y, game.lang.difficulties, textStyles.h2_);
  243. infoIcon = game.add.image(x + 6 * offsetW - 30, y - 20, 'info', 0.7, 0.8);
  244. infoIcon.anchor(0.5, 0.5);
  245. infoIcon.iconType = 'infoIcon';
  246. infoIcon.id = 'gameDifficulty';
  247. infoIcon.description = 'info_description';
  248. self.menuIcons.push(infoIcon);
  249. // Label 'Show Fractions'
  250. gameList[gameId].assets.customMenu.auxiliarTitle(x, y, offsetW, offsetH);
  251. },
  252. renderCheckBox: function (x, y, offsetW, offsetH, curGame) {
  253. y += 60;
  254. const frame = showFractions ? 1 : 0;
  255. const selectionBox = game.add.sprite(
  256. x + 5 * offsetW,
  257. y + offsetH + 90,
  258. 'select_input',
  259. frame,
  260. 1.4
  261. );
  262. selectionBox.anchor(0.5, 0.5);
  263. selectionBox.iconType = 'selectionBox';
  264. selectionBox.description = curGame.assets.customMenu.gameLabelDescription;
  265. self.menuIcons.push(selectionBox);
  266. },
  267. renderModeSection: function (x, y, offsetW, offsetH, curGame) {
  268. x = getFrameInfo().x + offsetW;
  269. y = getFrameInfo().y + offsetH / 2;
  270. if (!gameMode) gameMode = gameList[gameId].gameMode[0];
  271. for (
  272. let i = 0;
  273. i < curGame.assets.customMenu.gameModeBtn.length;
  274. i++, y += offsetH
  275. ) {
  276. const icon = game.add.sprite(
  277. x,
  278. y,
  279. curGame.assets.customMenu.gameModeBtn[i],
  280. 0,
  281. 1,
  282. 1
  283. );
  284. icon.anchor(0.5, 0.5);
  285. icon.description = curGame.assets.customMenu.gameModeDescription[i];
  286. icon.gameMode = curGame.gameMode[i];
  287. icon.iconType = 'gameMode';
  288. if (gameList[gameId].gameMode[i] == gameMode) {
  289. icon.curFrame = 1;
  290. }
  291. self.menuIcons.push(icon);
  292. }
  293. },
  294. renderOperationSection: function (x, y, offsetW, offsetH, curGame) {
  295. x += 3 * offsetW;
  296. y = getFrameInfo().y + offsetH / 2;
  297. offsetH = game.math.getOffset(
  298. getFrameInfo().height,
  299. curGame.gameOperation.length - 1
  300. );
  301. if (!gameOperation) gameOperation = gameList[gameId].gameOperation[0];
  302. let icon;
  303. // Placing math operation icons
  304. for (let i = 0; i < curGame.gameOperation.length; i++, y += offsetH) {
  305. icon = game.add.sprite(
  306. x,
  307. y,
  308. curGame.assets.customMenu.gameOperationBtn[i],
  309. 0,
  310. 1,
  311. 1
  312. );
  313. icon.anchor(0.5, 0.5);
  314. icon.description = curGame.assets.customMenu.gameOperationDescription[i];
  315. icon.gameOperation = curGame.gameOperation[i];
  316. icon.iconType = 'gameOperation';
  317. if (gameList[gameId].gameOperation[i] == gameOperation) {
  318. icon.curFrame = 1;
  319. }
  320. self.menuIcons.push(icon);
  321. }
  322. },
  323. renderDifficultySection: function (x, y, offsetW, offsetH, curGame) {
  324. x = getFrameInfo().x - 50 + 5 * offsetW;
  325. offsetH = game.math.getOffset(
  326. getFrameInfo().height,
  327. curGame.gameMode.length
  328. );
  329. y = getFrameInfo().y + offsetH / 3;
  330. if (!gameDifficulty) gameDifficulty = 1;
  331. if (gameName === 'squareTwo') x -= 70;
  332. for (let i = 0; i < curGame.gameDifficulty; i++) {
  333. // Parameters
  334. const curX = x + (50 + 10) * i;
  335. const icon = game.add.sprite(curX, y - 5, 'btn_square', 1, 0.8);
  336. icon.anchor(0.5, 0.5);
  337. icon.description = curGame.assets.customMenu.gameDifficultyDescription[i];
  338. icon.difficulty = i + 1;
  339. icon.iconType = 'difficulty';
  340. if (i == gameDifficulty - 1) {
  341. icon.curFrame = 0;
  342. }
  343. self.menuIcons.push(icon);
  344. // Difficulty numbers
  345. game.add.text(curX, y + 7, i + 1, textStyles.h4_);
  346. }
  347. },
  348. renderEnterSection: function (x, y) {
  349. // FOR MOODLE
  350. if (!moodle) {
  351. x = context.canvas.width - 150;
  352. y = context.canvas.height - 180;
  353. const enterIcon = game.add.image(x, y, 'bush', 2);
  354. enterIcon.anchor(0.5, 0.5);
  355. enterIcon.iconType = 'enter';
  356. self.menuIcons.push(enterIcon);
  357. self.enterText = game.add.text(x, y, game.lang.continue, textStyles.btn);
  358. }
  359. },
  360. setInfoBoxes: function () {
  361. // --------------------------- INFO BOX
  362. self.infoBox = document.querySelector('.ifr-modal');
  363. // When the user clicks on the 'x', close the modal
  364. document.querySelector('.ifr-modal__closeButton').onclick = function () {
  365. self.infoBox.style.display = 'none';
  366. };
  367. // When the user clicks anywhere outside of the modal, close it
  368. window.onclick = function (event) {
  369. if (event.target == self.infoBox) {
  370. self.infoBox.style.display = 'none';
  371. }
  372. };
  373. self.gameOperationContent = {
  374. title: game.lang.operation_math,
  375. body: game.lang.infoBox_oper,
  376. img: `<table class="table">
  377. <tr>
  378. <td><img width=50 src="${game.image['op_plus'].src}"></td>
  379. <td><img width=50 src="${game.image['op_mix'].src}"></td>
  380. <td><img width=50 src="${game.image['op_min'].src}"></td>
  381. <td><img width=50 src="${game.image['op_eq'].src}"></td>
  382. </tr>
  383. <tr>
  384. <td class="text-center">${game.lang.plus}</td>
  385. <td class="text-center">${game.lang.mixed}</td>
  386. <td class="text-center">${game.lang.minus}</td>
  387. <td class="text-center">${game.lang.equals}</td>
  388. </tr>
  389. </table>`,
  390. };
  391. },
  392. /**
  393. * Displays game menu information boxes.
  394. */
  395. showInfoBox: function (icon) {
  396. self.infoBox.style.display = 'block';
  397. const data =
  398. icon.id == 'gameOperation'
  399. ? self.gameOperationContent
  400. : gameList[gameId].assets.customMenu.infoBox()[icon.id];
  401. const content = `<h2>${data.title}</h2>
  402. <div>${data.body}</div>
  403. ${data.img}`;
  404. document.querySelector('.ifr-modal__infobox').innerHTML = content;
  405. },
  406. /**
  407. * Display the description the game mode on screen
  408. *
  409. * @param {object} icon icon for the game mode
  410. */
  411. showTitle: function (icon) {
  412. if (icon.iconType !== 'enter')
  413. self.lbl_description.name = game.lang[icon.description];
  414. },
  415. /**
  416. * Remove the description the game mode from screen
  417. */
  418. clearTitle: function () {
  419. self.lbl_description.name = '';
  420. },
  421. };