menu_custom.js 14 KB

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