customMenu.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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[gameType].sprite);
  15. game.load.image(url[gameType].image);
  16. },
  17. /**
  18. * Main code
  19. */
  20. create: function () {
  21. // FOR MOODLE
  22. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'false') { // Student role
  23. game.state.start('map');
  24. } else {
  25. // Background color
  26. game.add.geom.rect(0, 0, context.canvas.width, context.canvas.height, undefined, 0, colors.blueBckg, 1);
  27. // Floor
  28. for (let i = 0; i < context.canvas.width / 100; i++) { game.add.image(i * 100, context.canvas.height - 100, 'floor'); }
  29. // Overtitle : Selected game
  30. game.add.text(context.canvas.width / 2, 40, game.lang.game.toUpperCase() + ": " + menuState.menuIcons, textStyles.h4_brown);
  31. // Title : Customize the selected game
  32. game.add.text(context.canvas.width / 2, 80, game.lang.custom_game, textStyles.h1_green);
  33. // Loads navigation icons
  34. navigationIcons.add(
  35. true, false, false,
  36. true, true,
  37. 'menu', false);
  38. let infoIcon;
  39. const iconScale = 0.7;
  40. const curGame = info.all[gameType];
  41. this.menuIcons = [];
  42. let x = gameFrame().x;
  43. let y = gameFrame().y;
  44. let offsetW = game.math.getOffset(gameFrame().width, 5);
  45. let offsetH = game.math.getOffset(gameFrame().height, curGame.gameMode.length);
  46. const line_thickness = 5;
  47. // Label 'Game Modes'
  48. game.add.text(x + offsetW, y, game.lang.game_modes, textStyles.h2_blue_2);
  49. infoIcon = game.add.image(x + 2 * offsetW - 30, y - 20, 'info', 0.5, 0.4);
  50. infoIcon.anchor(0.5, 0.5);
  51. infoIcon.iconType = 'infoIcon';
  52. infoIcon.id = 'gameMode';
  53. this.menuIcons.push(infoIcon);
  54. // Label 'Operations'
  55. game.add.text(x + 3 * offsetW, y, game.lang.operations, textStyles.h2_blue_2);
  56. infoIcon = game.add.image(x + 4 * offsetW - 30, y - 20, 'info', 0.5, 0.4);
  57. infoIcon.anchor(0.5, 0.5);
  58. infoIcon.iconType = 'infoIcon';
  59. infoIcon.id = 'gameOperation';
  60. this.menuIcons.push(infoIcon);
  61. // Label 'Difficulties'
  62. game.add.text(x + 5 * offsetW, y, game.lang.difficulties, textStyles.h2_blue_2);
  63. infoIcon = game.add.image(x + 6 * offsetW - 30, y - 20, 'info', 0.5, 0.4);
  64. infoIcon.anchor(0.5, 0.5);
  65. infoIcon.iconType = 'infoIcon';
  66. infoIcon.id = 'gameDifficulty';
  67. this.menuIcons.push(infoIcon);
  68. // Horizontal line
  69. game.add.geom.rect(x - 25, y + 10, gameFrame().width, line_thickness, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  70. // Vertical lines
  71. game.add.geom.rect(x + 2 * offsetW, y - 25, line_thickness, gameFrame().height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  72. game.add.geom.rect(x + 4 * offsetW, y - 25, line_thickness, gameFrame().height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  73. // --------------------------- TURN ON/OFF FRACTION LABELS / RECTANGLE GUIDE
  74. // Horizontal line
  75. game.add.geom.rect(x + 4 * offsetW, y + offsetH, gameFrame().width / 3 - 4 * line_thickness, line_thickness, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  76. infoIcon = game.add.image(x + 6 * offsetW - 30, y + offsetH - 20, 'info', 0.5, 0.4);
  77. infoIcon.anchor(0.5, 0.5);
  78. infoIcon.iconType = 'infoIcon';
  79. infoIcon.id = 'gameMisc';
  80. this.menuIcons.push(infoIcon);
  81. // Label 'Show Fractions / Auxiliar rectangles'
  82. game.add.text(x + 5 * offsetW, y + offsetH - 48, game.lang.show, textStyles.h4_blue_2);
  83. let auxText;
  84. if (gameType == 'squareTwo') {
  85. auxText = game.lang.aux_rectangle;
  86. game.add.text(x + 5 * offsetW + 10, y + offsetH - 24, auxText, textStyles.h4_blue_2);
  87. } else {
  88. auxText = game.lang.title;
  89. game.add.text(x + 5 * offsetW, y + offsetH - 24, auxText, textStyles.h2_blue_2);
  90. }
  91. // Selection box
  92. y += 40;
  93. const frame = (fractionLabel) ? 1 : 0;
  94. const selectionBox = game.add.sprite(x + 5 * offsetW, y + offsetH, 'select', frame, 0.11);
  95. selectionBox.anchor(0.5, 0.5);
  96. selectionBox.iconType = 'selectionBox';
  97. this.menuIcons.push(selectionBox);
  98. // --------------------------- GAME MODE ICONS
  99. x = gameFrame().x + offsetW;
  100. y = gameFrame().y + offsetH / 2;
  101. for (let i = 0; i < curGame.gameModeUrl.length; i++, y += offsetH) {
  102. const icon = game.add.sprite(x, y, curGame.gameModeUrl[i], 0, iconScale, 1);
  103. icon.anchor(0.5, 0.5);
  104. icon.gameMode = curGame.gameMode[i];
  105. icon.iconType = 'gameMode';
  106. if (i == 0) {
  107. gameMode = icon.gameMode;
  108. icon.curFrame = 1;
  109. }
  110. this.menuIcons.push(icon);
  111. }
  112. // --------------------------- GAME OPERATION ICONS
  113. x += 2 * offsetW;
  114. y = gameFrame().y + offsetH / 2;
  115. offsetH = game.math.getOffset(gameFrame().height, curGame.gameOperation.length);
  116. let icon;
  117. // Placing math operation icons
  118. for (let i = 0; i < curGame.gameOperation.length; i++, y += offsetH) {
  119. icon = game.add.sprite(x, y, curGame.gameOperationUrl[i], 0, iconScale, 1);
  120. icon.anchor(0.5, 0.5);
  121. icon.gameOperation = curGame.gameOperation[i];
  122. icon.iconType = 'gameOperation';
  123. if (i == 0) {
  124. gameOperation = icon.gameOperation;
  125. icon.curFrame = 1;
  126. }
  127. this.menuIcons.push(icon);
  128. }
  129. // --------------------------- DIFFICULTY ICONS
  130. x = gameFrame().x - 50 + 5 * offsetW
  131. offsetH = game.math.getOffset(gameFrame().height, curGame.gameMode.length);
  132. y = gameFrame().y + offsetH / 3;
  133. if (gameType != 'squareOne') x -= 40;
  134. for (let i = 0; i < curGame.gameDifficulty; i++) {
  135. // Parameters
  136. const curX = x + (30 + 10) * i;
  137. // Difficulty menuIcons
  138. const icon = game.add.geom.rect(curX, y, 30, 30, undefined, 0, colors.gray, 1);
  139. icon.anchor(0.5, 0.5);
  140. icon.difficulty = i + 1;
  141. icon.iconType = 'difficulty';
  142. if (i == 0) {
  143. gameDifficulty = icon.difficulty;
  144. icon.fillColor = colors.blue;
  145. }
  146. this.menuIcons.push(icon);
  147. // Difficulty numbers
  148. game.add.text(curX, y + 7, i + 1, textStyles.h4_white);
  149. }
  150. // --------------------------- ENTER ICON
  151. // FOR MOODLE
  152. if (!moodle) {
  153. x = context.canvas.width - 100;
  154. y = context.canvas.height - 110;
  155. const enterIcon = game.add.image(x, y, 'bush');
  156. enterIcon.anchor(0.5, 0.5);
  157. enterIcon.iconType = 'enter';
  158. this.menuIcons.push(enterIcon);
  159. this.enterText = game.add.text(x, y, game.lang.continue, textStyles.h4_white);
  160. }
  161. // --------------------------- INFO BOX
  162. this.infoBox = document.getElementById('my-modal');
  163. // When the user clicks on the 'x', close the modal
  164. document.getElementsByClassName('close')[0].onclick = function () {
  165. self.infoBox.style.display = 'none';
  166. }
  167. // When the user clicks anywhere outside of the modal, close it
  168. window.onclick = function (event) {
  169. if (event.target == self.infoBox) {
  170. self.infoBox.style.display = 'none';
  171. }
  172. }
  173. this.infoBoxContent = {
  174. gameMode: {
  175. squareOne: {
  176. title: '<b>' + game.lang.game_modes + '</b>',
  177. body: game.lang.infoBox_mode,
  178. img: '<table> <tr> <td> <b>A)</b> ' + game.lang.infoBox_mode_s1_A +
  179. ' </td> <td> <b>B)</b> ' + game.lang.infoBox_mode_s1_B +
  180. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['s1-A-h'].src + '"> ' +
  181. ' </td> <td> <img width=100% src="' + game.image['s1-B-h'].src + '"> </td> </tr> <table>'
  182. },
  183. circleOne: {
  184. title: '<b>' + game.lang.game_modes + '</b>',
  185. body: game.lang.infoBox_mode,
  186. img: '<table> <tr style="border-bottom: 5px solid white"> <td width=70%> <img width=100% src="' + game.image['c1-A-h'].src + '">' +
  187. ' </td> <td> &nbsp; <b>A)</b> ' + game.lang.infoBox_mode_c1_A +
  188. ' </td> </tr> </tr> <td> <img width=100% src="' + game.image['c1-B-h'].src + '"> ' +
  189. ' </td> <td> &nbsp; <b>B)</b> ' + game.lang.infoBox_mode_c1_B + '</td> </tr> <table>'
  190. },
  191. squareTwo: {
  192. title: '<b>' + game.lang.game_modes + '</b>',
  193. body: game.lang.infoBox_mode,
  194. img: '<table> <tr> <td> <b>A)</b> ' + game.lang.infoBox_mode_s2_A +
  195. ' </td> <td> <b>B)</b> ' + game.lang.infoBox_mode_s2_B +
  196. ' </td> </tr> <tr> <td> <img width=98% src="' + game.image['s2-A-h'].src + '"> ' +
  197. ' </td> <td> <img width=98% src="' + game.image['s2-B-h'].src + '"> </td> </tr> <table>'
  198. }
  199. },
  200. gameOperation: {
  201. title: '<b>' + game.lang.operation_math + '</b>',
  202. body: game.lang.infoBox_oper,
  203. img:
  204. '<table class="table">' +
  205. '<tr>' +
  206. '<td> <img width=50 src="' + game.image['operation_plus'].src + '"> </td>' +
  207. '<td> <img width=50 src="' + game.image['operation_mixed'].src + '"> </td>' +
  208. '<td> <img width=50 src="' + game.image['operation_minus'].src + '"> </td>' +
  209. '<td> <img width=50 src="' + game.image['operation_equals'].src + '"> </td>' +
  210. '</tr> <tr>' +
  211. '<td class="text-center">' + game.lang.plus + '</td>' +
  212. '<td class="text-center">' + game.lang.mixed + '</td>' +
  213. '<td class="text-center">' + game.lang.minus + '</td>' +
  214. '<td class="text-center">' + game.lang.equals + '</td>' +
  215. '</tr>' +
  216. '</table>'
  217. },
  218. gameDifficulty: {
  219. squareOne: {
  220. title: '<b>' + game.lang.difficulties + '</b>',
  221. body: game.lang.infoBox_diff + ' ' + game.lang.infoBox_diff_obs,
  222. img: '<table> <tr> <td> <b>' + game.lang.difficulty + ':</b> 1' +
  223. ' </td> <td> <b>' + game.lang.difficulty + ':</b> 3' +
  224. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['s1-diff-1'].src + '"> ' +
  225. ' </td> <td style="border-left: 4px solid white"> <img width=100% src="' + game.image['s1-diff-3'].src + '"> </td> </tr> </table> <br>' +
  226. game.lang.infoBox_diff_aux + '<center> <img width=50% src="' + game.image['map-s1'].src + '"> </center>'
  227. },
  228. circleOne: {
  229. title: '<b>' + game.lang.difficulties + '</b>',
  230. body: game.lang.infoBox_diff + ' ' + game.lang.infoBox_diff_obs,
  231. img: '<table> <tr> <td style="border-right: 4px solid white"> <b>' + game.lang.difficulty + ':</b> 1' +
  232. ' </td> <td> <b>' + game.lang.difficulty + ':</b> 5' +
  233. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['c1-diff-1'].src + '"> ' +
  234. ' </td> <td style="border-left: 4px solid white"> <img width=100% src="' + game.image['c1-diff-5'].src + '"> </td> </tr> </table> <center> <br>' +
  235. game.lang.infoBox_diff_aux + '<center> <img width=50% src="' + game.image['map-c1s2'].src + '"> </center>'
  236. },
  237. squareTwo: {
  238. title: '<b>' + game.lang.difficulties + '</b>',
  239. body: game.lang.infoBox_diff,
  240. img: '<table> <tr> <td> <b>' + game.lang.difficulty + ':</b> 1' +
  241. ' </td> <td> <b>' + game.lang.difficulty + ':</b> 5' +
  242. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['s2-diff-1'].src + '"> ' +
  243. ' </td> <td style="border-left: 4px solid white"> <img width=100% src="' + game.image['s2-diff-5'].src + '"> </td> </tr> </table> <br>' +
  244. game.lang.infoBox_diff_aux + '<center> <img width=50% src="' + game.image['map-c1s2'].src + '"> </center>'
  245. },
  246. },
  247. gameMisc: {
  248. squareOne: {
  249. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  250. body: game.lang.infoBox_misc_label,
  251. img: '<img class="mx-auto" width=80% src="' + game.image['s1-label'].src + '">',
  252. },
  253. circleOne: {
  254. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  255. body: game.lang.infoBox_misc_label,
  256. img: '<img class="mx-auto" width=60% src="' + game.image['c1-label'].src + '">',
  257. },
  258. squareTwo: {
  259. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  260. body: game.lang.infoBox_misc_rect,
  261. img: '<img class="mx-auto" width=100% src="' + game.image['s2-label'].src + '">',
  262. }
  263. }
  264. };
  265. // ------------- EVENTS
  266. game.event.add('click', this.onInputDown);
  267. game.event.add('mousemove', this.onInputOver);
  268. }
  269. },
  270. /**
  271. * Displays game menu information boxes.
  272. */
  273. showInfoBox: function (icon) {
  274. self.infoBox.style.display = 'block';
  275. const element = (icon.id == 'gameOperation') ? self.infoBoxContent[icon.id] : self.infoBoxContent[icon.id][gameType];
  276. let msg = '<h3>' + element.title + '</h3>'
  277. + '<p align=justify>' + element.body + '</p>'
  278. + element.img;
  279. document.getElementById('infobox-content').innerHTML = msg;
  280. },
  281. /**
  282. * Saves information selected by the player
  283. *
  284. * @param {object} icon selected icon
  285. */
  286. load: function (icon) {
  287. const type = icon.iconType;
  288. if (audioStatus) game.audio.popSound.play();
  289. switch (type) {
  290. case 'gameMode': gameMode = icon.gameMode; break;
  291. case 'gameOperation': gameOperation = icon.gameOperation; break;
  292. case 'difficulty': gameDifficulty = icon.difficulty; break;
  293. case 'infoIcon': self.showInfoBox(icon); break;
  294. case 'selectionBox':
  295. if (icon.curFrame == 0) {
  296. icon.curFrame = 1;
  297. fractionLabel = true;
  298. } else {
  299. icon.curFrame = 0;
  300. fractionLabel = false;
  301. }
  302. game.render.all();
  303. break;
  304. case 'enter':
  305. if (debugMode) {
  306. console.log('------------------------------' +
  307. '\nGame State: ' + gameType +
  308. '\nGame Mode: ' + gameMode +
  309. '\n------------------------------');
  310. }
  311. mapPosition = 0; // Map position
  312. mapMove = true; // Move no next point
  313. completedLevels = 0; // Reset the game progress when entering a new level
  314. game.state.start('map');
  315. break;
  316. }
  317. },
  318. /**
  319. * Called by mouse click event
  320. *
  321. * @param {object} mouseEvent contains the mouse click coordinates
  322. */
  323. onInputDown: function (mouseEvent) {
  324. const x = game.math.getMouse(mouseEvent).x;
  325. const y = game.math.getMouse(mouseEvent).y;
  326. let overIcon;
  327. // Check if clicked on an icon
  328. for (let i in self.menuIcons) {
  329. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  330. overIcon = i;
  331. break;
  332. }
  333. }
  334. // Update gui
  335. if (overIcon) { // If has clicked on an icon
  336. document.body.style.cursor = 'pointer';
  337. self.menuIcons.forEach(cur => {
  338. if (cur.iconType == self.menuIcons[overIcon].iconType) { // If its in the same icon category
  339. if (cur == self.menuIcons[overIcon]) { // If its the clicked icon
  340. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 1;
  341. else if (cur.iconType == 'difficulty') cur.fillColor = colors.blue;
  342. } else {
  343. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 0;
  344. else if (cur.iconType == 'difficulty') cur.fillColor = colors.gray;
  345. }
  346. }
  347. });
  348. self.load(self.menuIcons[overIcon]);
  349. } else document.body.style.cursor = 'auto';
  350. navigationIcons.onInputDown(x, y);
  351. game.render.all();
  352. },
  353. /**
  354. * Called by mouse move event
  355. *
  356. * @param {object} mouseEvent contains the mouse move coordinates
  357. */
  358. onInputOver: function (mouseEvent) {
  359. const x = game.math.getMouse(mouseEvent).x;
  360. const y = game.math.getMouse(mouseEvent).y;
  361. let overIcon;
  362. // Check if pointer is over an icon
  363. for (let i in self.menuIcons) {
  364. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  365. overIcon = i;
  366. break;
  367. }
  368. }
  369. // Update gui
  370. if (overIcon) { // If pointer is over icon
  371. document.body.style.cursor = 'pointer';
  372. self.menuIcons.forEach(cur => {
  373. if (cur.iconType == self.menuIcons[overIcon].iconType) { // If its in the same icon category
  374. if (cur == self.menuIcons[overIcon]) { // If its the icon the pointer is over
  375. if (cur.iconType == 'enter') self.enterText.style = textStyles.h3__white;
  376. cur.scale = cur.originalScale * 1.1;
  377. } else {
  378. cur.scale = cur.originalScale;
  379. }
  380. }
  381. });
  382. } else { // If pointer is not over icon
  383. if (self.enterText) self.enterText.style = textStyles.h4_white;
  384. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  385. document.body.style.cursor = 'auto';
  386. }
  387. // Check navigation icons
  388. navigationIcons.onInputOver(x, y);
  389. game.render.all();
  390. }
  391. }