menu_custom.js 13 KB

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