menu_custom.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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_game = 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);
  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. self.menuIcons.push(infoIcon);
  232. // Label 'Operations'
  233. game.add.text(x + 3 * offsetW, y, game.lang.operations, textStyles.h2_);
  234. infoIcon = game.add.image(x + 4 * offsetW - 30, y - 20, 'info', 0.7, 0.8);
  235. infoIcon.anchor(0.5, 0.5);
  236. infoIcon.iconType = 'infoIcon';
  237. infoIcon.id = 'gameOperation';
  238. self.menuIcons.push(infoIcon);
  239. // Label 'Difficulties'
  240. game.add.text(x + 5 * offsetW, y, game.lang.difficulties, textStyles.h2_);
  241. infoIcon = game.add.image(x + 6 * offsetW - 30, y - 20, 'info', 0.7, 0.8);
  242. infoIcon.anchor(0.5, 0.5);
  243. infoIcon.iconType = 'infoIcon';
  244. infoIcon.id = 'gameDifficulty';
  245. self.menuIcons.push(infoIcon);
  246. // Label 'Show Fractions'
  247. gameList[gameId].assets.customMenu.auxiliarTitle(x, y, offsetW, offsetH);
  248. },
  249. renderCheckBox: function (x, y, offsetW, offsetH) {
  250. y += 60;
  251. const frame = showFractions ? 1 : 0;
  252. const selectionBox = game.add.sprite(
  253. x + 5 * offsetW,
  254. y + offsetH + 90,
  255. 'select_input',
  256. frame,
  257. 1.4
  258. );
  259. selectionBox.anchor(0.5, 0.5);
  260. selectionBox.iconType = 'selectionBox';
  261. self.menuIcons.push(selectionBox);
  262. },
  263. renderModeSection: function (x, y, offsetW, offsetH, curGame) {
  264. x = getFrameInfo().x + offsetW;
  265. y = getFrameInfo().y + offsetH / 2;
  266. if (!gameMode) gameMode = gameList[gameId].gameMode[0];
  267. for (
  268. let i = 0;
  269. i < curGame.assets.customMenu.gameModeBtn.length;
  270. i++, y += offsetH
  271. ) {
  272. const icon = game.add.sprite(
  273. x,
  274. y,
  275. curGame.assets.customMenu.gameModeBtn[i],
  276. 0,
  277. 1,
  278. 1
  279. );
  280. icon.anchor(0.5, 0.5);
  281. icon.gameModeDescription = curGame.assets.customMenu.gameModeDescription[i];
  282. icon.gameMode = curGame.gameMode[i];
  283. icon.iconType = 'gameMode';
  284. if (gameList[gameId].gameMode[i] == gameMode) {
  285. icon.curFrame = 1;
  286. }
  287. self.menuIcons.push(icon);
  288. }
  289. },
  290. renderOperationSection: function (x, y, offsetW, offsetH, curGame) {
  291. x += 3 * offsetW;
  292. y = getFrameInfo().y + offsetH / 2;
  293. offsetH = game.math.getOffset(
  294. getFrameInfo().height,
  295. curGame.gameOperation.length - 1
  296. );
  297. if (!gameOperation) gameOperation = gameList[gameId].gameOperation[0];
  298. let icon;
  299. // Placing math operation icons
  300. for (let i = 0; i < curGame.gameOperation.length; i++, y += offsetH) {
  301. icon = game.add.sprite(
  302. x,
  303. y,
  304. curGame.assets.customMenu.gameOperationBtn[i],
  305. 0,
  306. 1,
  307. 1
  308. );
  309. icon.anchor(0.5, 0.5);
  310. icon.gameOperation = curGame.gameOperation[i];
  311. icon.iconType = 'gameOperation';
  312. if (gameList[gameId].gameOperation[i] == gameOperation) {
  313. icon.curFrame = 1;
  314. }
  315. self.menuIcons.push(icon);
  316. }
  317. },
  318. renderDifficultySection: function (x, y, offsetW, offsetH, curGame) {
  319. x = getFrameInfo().x - 50 + 5 * offsetW;
  320. offsetH = game.math.getOffset(
  321. getFrameInfo().height,
  322. curGame.gameMode.length
  323. );
  324. y = getFrameInfo().y + offsetH / 3;
  325. if (!gameDifficulty) gameDifficulty = 1;
  326. if (gameName === 'squareTwo') x -= 70;
  327. for (let i = 0; i < curGame.gameDifficulty; i++) {
  328. // Parameters
  329. const curX = x + (50 + 10) * i;
  330. const icon = game.add.sprite(curX, y - 5, 'btn_square', 1, 0.8);
  331. icon.anchor(0.5, 0.5);
  332. icon.difficulty = i + 1;
  333. icon.iconType = 'difficulty';
  334. if (i == gameDifficulty - 1) {
  335. icon.curFrame = 0;
  336. }
  337. self.menuIcons.push(icon);
  338. // Difficulty numbers
  339. game.add.text(curX, y + 7, i + 1, textStyles.h4_);
  340. }
  341. },
  342. renderEnterSection: function (x, y) {
  343. // FOR MOODLE
  344. if (!moodle) {
  345. x = context.canvas.width - 150;
  346. y = context.canvas.height - 180;
  347. const enterIcon = game.add.image(x, y, 'bush', 2);
  348. enterIcon.anchor(0.5, 0.5);
  349. enterIcon.iconType = 'enter';
  350. self.menuIcons.push(enterIcon);
  351. self.enterText = game.add.text(x, y, game.lang.continue, textStyles.btn);
  352. }
  353. },
  354. setInfoBoxes: function () {
  355. // --------------------------- INFO BOX
  356. self.infoBox = document.querySelector('.ifr-modal');
  357. // When the user clicks on the 'x', close the modal
  358. document.querySelector('.ifr-modal__closeButton').onclick = function () {
  359. self.infoBox.style.display = 'none';
  360. };
  361. // When the user clicks anywhere outside of the modal, close it
  362. window.onclick = function (event) {
  363. if (event.target == self.infoBox) {
  364. self.infoBox.style.display = 'none';
  365. }
  366. };
  367. self.gameOperationContent = {
  368. title: game.lang.operation_math,
  369. body: game.lang.infoBox_oper,
  370. img: `<table class="table">
  371. <tr>
  372. <td><img width=50 src="${game.image['op_plus'].src}"></td>
  373. <td><img width=50 src="${game.image['op_mix'].src}"></td>
  374. <td><img width=50 src="${game.image['op_min'].src}"></td>
  375. <td><img width=50 src="${game.image['op_eq'].src}"></td>
  376. </tr>
  377. <tr>
  378. <td class="text-center">${game.lang.plus}</td>
  379. <td class="text-center">${game.lang.mixed}</td>
  380. <td class="text-center">${game.lang.minus}</td>
  381. <td class="text-center">${game.lang.equals}</td>
  382. </tr>
  383. </table>`,
  384. };
  385. },
  386. /**
  387. * Displays game menu information boxes.
  388. */
  389. showInfoBox: function (icon) {
  390. self.infoBox.style.display = 'block';
  391. const data =
  392. icon.id == 'gameOperation'
  393. ? self.gameOperationContent
  394. : gameList[gameId].assets.customMenu.infoBox()[icon.id];
  395. const content = `<h2>${data.title}</h2>
  396. <div>${data.body}</div>
  397. ${data.img}`;
  398. document.querySelector('.ifr-modal__infobox').innerHTML = content;
  399. },
  400. /**
  401. * Display the description the game mode on screen
  402. *
  403. * @param {object} icon icon for the game mode
  404. */
  405. showTitle: function (icon) {
  406. self.lbl_game.name = game.lang[icon.gameModeDescription];
  407. },
  408. /**
  409. * Remove the description the game mode from screen
  410. */
  411. clearTitle: function () {
  412. self.lbl_game.name = '';
  413. },
  414. };