menu_custom.js 13 KB

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