customMenu.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /**
  2. * SECUNDARY MENU STATE: player can select game mode, math operation and overall game difficulty
  3. *
  4. * @namespace
  5. */
  6. const customMenuState = {
  7. /**
  8. * Preloads media for current state
  9. */
  10. preload: function () {
  11. // LOADING MEDIA
  12. game.load.sprite(url[gameTypeString].sprite);
  13. game.load.image(url[gameTypeString].image);
  14. },
  15. /**
  16. * Main code
  17. */
  18. create: function () {
  19. // FOR MOODLE
  20. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  21. game.state.start('map');
  22. } else {
  23. const iconScale = 0.7;
  24. const baseY = 270 - 40;
  25. this.menuIcons = [];
  26. // Background color
  27. game.add.geom.rect(0, 0, defaultWidth, defaultHeight, undefined, 0, colors.blueBckg, 1);
  28. // Floor
  29. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, defaultHeight - 100, 'floor'); }
  30. // Overtitle : Selected game
  31. game.add.text(defaultWidth / 2, 40, game.lang.game.toUpperCase() + ": " + menuState.menuIcons, textStyles.h4_brown);
  32. // Title : Customize the selected game
  33. game.add.text(defaultWidth / 2, 80, game.lang.custom_game, textStyles.h1_green);
  34. // Loads navigation icons
  35. navigationIcons.func_addIcons(
  36. true, false, false,
  37. true, true,
  38. 'menu', false);
  39. let x = 150;
  40. let y = 200 - 40;
  41. let width = 5;
  42. let height = 280 + 80;
  43. let offsetW = 600 / 6;
  44. let offsetH, infoIcon;
  45. // Label 'Game Modes'
  46. game.add.text(x + offsetW - 12, y, game.lang.game_modes, textStyles.h2_blue_2);
  47. infoIcon = game.add.image(x + 2 * offsetW - 30, y - 40, 'info', 0.5, 0.4);
  48. infoIcon.anchor(0.5, 0.5);
  49. infoIcon.iconType = 'infoIcon';
  50. infoIcon.id = 'gameMode';
  51. this.menuIcons.push(infoIcon);
  52. // Label 'Operations'
  53. game.add.text(x + 3 * offsetW, y, game.lang.operations, textStyles.h2_blue_2);
  54. infoIcon = game.add.image(x + 4 * offsetW - 30, y - 40, 'info', 0.5, 0.4);
  55. infoIcon.anchor(0.5, 0.5);
  56. infoIcon.iconType = 'infoIcon';
  57. infoIcon.id = 'gameOperation';
  58. this.menuIcons.push(infoIcon);
  59. // Label 'Difficulties'
  60. game.add.text(x + 5 * offsetW, y, game.lang.difficulties, textStyles.h2_blue_2);
  61. infoIcon = game.add.image(x + 6 * offsetW - 30, y - 40, 'info', 0.5, 0.4);
  62. infoIcon.anchor(0.5, 0.5);
  63. infoIcon.iconType = 'infoIcon';
  64. infoIcon.id = 'gameDifficulty';
  65. this.menuIcons.push(infoIcon);
  66. // Horizontal line
  67. game.add.geom.rect(x - 25, y + 10, 600 + 50, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  68. // Vertical lines
  69. game.add.geom.rect(x + 2 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  70. game.add.geom.rect(x + 4 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  71. // --------------------------- TURN ON/OFF FRACTION LABELS / RECTANGLE GUIDE
  72. // Horizontal line
  73. game.add.geom.rect(x + 4 * offsetW, y + 136, 200 + 25, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  74. // Label 'Show Fractions / Auxiliar rectangles'
  75. game.add.text(x + 5 * offsetW, y + 102, game.lang.show, textStyles.h4_blue_2);
  76. infoIcon = game.add.image(x + 6 * offsetW + 20, y + 102, '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. let auxText;
  82. if (gameTypeString == 'squareTwo') {
  83. auxText = game.lang.aux_rectangle;
  84. game.add.text(x + 5 * offsetW + 10, y + 102 + 24, auxText, textStyles.h4_blue_2);
  85. } else {
  86. auxText = game.lang.title;
  87. game.add.text(x + 5 * offsetW, y + 102 + 24, auxText, textStyles.h2_blue_2);
  88. }
  89. // Selection box
  90. y += 40;
  91. const frame = (fractionLabel) ? 1 : 0;
  92. const selectionBox = game.add.sprite(x + 5 * offsetW, y + 102 + 24 - 2, 'select', frame, 0.11);
  93. selectionBox.anchor(0.5, 0.5);
  94. selectionBox.iconType = 'selectionBox';
  95. this.menuIcons.push(selectionBox);
  96. // --------------------------- GAME MODE ICONS
  97. x = 150 + offsetW;
  98. y = baseY;
  99. offsetH = this.func_getOffset(height, info[gameTypeString].gameMode.length);
  100. for (let i = 0; i < info[gameTypeString].gameModeUrl.length; i++, y += offsetH) {
  101. const icon = game.add.sprite(x, y, info[gameTypeString].gameModeUrl[i], 0, iconScale, 1);
  102. icon.anchor(0.5, 0.5);
  103. icon.gameMode = info[gameTypeString].gameMode[i];
  104. icon.iconType = 'gameMode';
  105. if (i == 0) {
  106. gameMode = icon.gameMode;
  107. icon.curFrame = 1;
  108. }
  109. this.menuIcons.push(icon);
  110. }
  111. // --------------------------- GAME OPERATION ICONS
  112. x += 2 * offsetW;
  113. y = baseY;
  114. offsetH = this.func_getOffset(height, info[gameTypeString].gameOperation.length);
  115. let icon;
  116. let aux = [];
  117. aux['squareOne'] = [
  118. ['operation_plus', 'Plus'],
  119. ['operation_minus', 'Minus']
  120. ];
  121. aux['circleOne'] = [
  122. ['operation_plus', 'Plus'],
  123. ['operation_minus', 'Minus'],
  124. ['operation_mixed', 'Mixed']
  125. ];
  126. aux['squareTwo'] = [
  127. ['operation_equals', 'Equals'],
  128. ];
  129. // Placing math operation icons
  130. for (let i = 0; i < aux[gameTypeString].length; i++, y += offsetH) {
  131. icon = game.add.sprite(x, y, aux[gameTypeString][i][0], 0, iconScale, 1);
  132. icon.anchor(0.5, 0.5);
  133. icon.gameOperation = aux[gameTypeString][i][1];
  134. icon.iconType = 'gameOperation';
  135. if (i == 0) {
  136. gameOperation = icon.gameOperation;
  137. icon.curFrame = 1;
  138. }
  139. this.menuIcons.push(icon);
  140. }
  141. // --------------------------- DIFFICULTY ICONS
  142. x = (gameTypeString == 'squareOne') ? 625 : 585;
  143. y = baseY - 25;
  144. for (let i = 0; i < info[gameTypeString].gameDifficulty; i++) {
  145. // Parameters
  146. const curX = x + (30 + 10) * i;
  147. // Difficulty menuIcons
  148. const icon = game.add.geom.rect(curX, y, 30, 30, undefined, 0, colors.gray, 1);
  149. icon.anchor(0.5, 0.5);
  150. icon.difficulty = i + 1;
  151. icon.iconType = 'difficulty';
  152. if (i == 0) {
  153. gameDifficulty = icon.difficulty;
  154. icon.fillColor = colors.blue;
  155. }
  156. this.menuIcons.push(icon);
  157. // Difficulty numbers
  158. game.add.text(curX, y + 7, i + 1, textStyles.h4_white);
  159. }
  160. // --------------------------- ENTER ICON
  161. // FOR MOODLE
  162. if (!moodle) {
  163. x = defaultWidth - 100;
  164. y = defaultHeight - 110;
  165. const enterIcon = game.add.image(x, y, 'bush');
  166. enterIcon.anchor(0.5, 0.5);
  167. enterIcon.iconType = 'enter';
  168. this.menuIcons.push(enterIcon);
  169. this.enterText = game.add.text(x, y, game.lang.continue, textStyles.h4_white);
  170. }
  171. // --------------------------- INFO BOX
  172. this.infoBox = document.getElementById('myModal');
  173. // When the user clicks on the 'x', close the modal
  174. document.getElementsByClassName('close')[0].onclick = function () {
  175. self.infoBox.style.display = 'none';
  176. }
  177. // When the user clicks anywhere outside of the modal, close it
  178. window.onclick = function (event) {
  179. if (event.target == self.infoBox) {
  180. self.infoBox.style.display = 'none';
  181. }
  182. }
  183. this.infoBoxContent = {
  184. gameMode: {
  185. squareOne: {
  186. title: '<b>' + game.lang.game_modes + '</b>',
  187. body: game.lang.infoBox_mode,
  188. img: '<table> <tr> <td> <b>A)</b> ' + game.lang.infoBox_mode_s1_A +
  189. ' </td> <td> <b>B)</b> ' + game.lang.infoBox_mode_s1_B +
  190. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['s1-A-h'].src + '"> ' +
  191. ' </td> <td> <img width=100% src="' + game.image['s1-B-h'].src + '"> </td> </tr> <table>'
  192. },
  193. circleOne: {
  194. title: '<b>' + game.lang.game_modes + '</b>',
  195. body: game.lang.infoBox_mode,
  196. img: '<table> <tr style="border-bottom: 5px solid white"> <td width=70%> <img width=100% src="' + game.image['c1-A-h'].src + '">' +
  197. ' </td> <td> &nbsp; <b>A)</b> ' + game.lang.infoBox_mode_c1_A +
  198. ' </td> </tr> </tr> <td> <img width=100% src="' + game.image['c1-B-h'].src + '"> ' +
  199. ' </td> <td> &nbsp; <b>B)</b> ' + game.lang.infoBox_mode_c1_B + '</td> </tr> <table>'
  200. },
  201. squareTwo: {
  202. title: '<b>' + game.lang.game_modes + '</b>',
  203. body: game.lang.infoBox_mode,
  204. img: '<center> <table> <tr> <td> <b>A)</b> ' + game.lang.infoBox_mode_s2_A +
  205. ' </td> <td> <b>B)</b> ' + game.lang.infoBox_mode_s2_B +
  206. ' </td> </tr> <tr> <td> <img width=98% src="' + game.image['s2-A-h'].src + '"> ' +
  207. ' </td> <td> <img width=98% src="' + game.image['s2-B-h'].src + '"> </td> </tr> <table> </center>'
  208. }
  209. },
  210. gameOperation: {
  211. title: '<b>' + game.lang.operation_math + '</b>',
  212. body: game.lang.infoBox_oper,
  213. img: '<center> <table> <tr style="border-bottom: 5px solid white"> <td> <img width=50 src="' + game.image['operation_plus'].src + '"> ' + game.lang.plus +
  214. ' </td> <td> <img width=50 src="' + game.image['operation_mixed'].src + '"> ' + game.lang.mixed +
  215. ' </td> </tr> <tr> <td><img width=50 src="' + game.image['operation_minus'].src + '"> ' + game.lang.minus +
  216. ' &nbsp; </td> <td> <img width=50 src="' + game.image['operation_equals'].src + '"> ' + game.lang.equals + ' </td> </tr> <table> <center>',
  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: '<center> <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: '<center> <img width=80% src="' + game.image['s1-label'].src + '"> </center>',
  252. },
  253. circleOne: {
  254. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  255. body: game.lang.infoBox_misc_label,
  256. img: '<center> <img width=60% src="' + game.image['c1-label'].src + '"> </center>',
  257. },
  258. squareTwo: {
  259. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  260. body: game.lang.infoBox_misc_rect,
  261. img: '<center> <img width=100% src="' + game.image['s2-label'].src + '"> </center>',
  262. }
  263. }
  264. };
  265. // ------------- EVENTS
  266. game.event.add('click', this.func_onInputDown);
  267. game.event.add('mousemove', this.func_onInputOver);
  268. }
  269. },
  270. /**
  271. * Displays game menu information boxes.
  272. */
  273. func_showInfoBox: function (icon) {
  274. self.infoBox.style.display = 'block';
  275. const element = (icon.id == 'gameOperation') ? self.infoBoxContent[icon.id] : self.infoBoxContent[icon.id][gameTypeString];
  276. let msg = '<h3>' + element.title + '</h3>'
  277. + '<p>' + 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. func_load: function (icon) {
  287. if (audioStatus) game.audio.beepSound.play();
  288. const type = icon.iconType;
  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.func_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) console.log('Game State: ' + gameTypeString + ', ' + gameMode);
  306. mapPosition = 0; // Map position
  307. mapMove = true; // Move no next point
  308. completedLevels = 0; // Reset the game progress when entering a new level
  309. game.state.start('map');
  310. break;
  311. }
  312. },
  313. /**
  314. * Calculate spacing for icons on the menu screen
  315. *
  316. * @param {number} width width of the available part of the screen
  317. * @param {number} numberOfIcons number or icons to be put on the screen
  318. * @returns {number}
  319. */
  320. func_getOffset: function (width, numberOfIcons) {
  321. return width / (numberOfIcons + 1);
  322. },
  323. /**
  324. * Called by mouse click event
  325. *
  326. * @param {object} mouseEvent contains the mouse click coordinates
  327. */
  328. func_onInputDown: function (mouseEvent) {
  329. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  330. let overIcon;
  331. // Check if clicked on an icon
  332. for (let i in self.menuIcons) {
  333. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  334. overIcon = i;
  335. break;
  336. }
  337. }
  338. // Update gui
  339. if (overIcon) { // if has clicked on an icon
  340. document.body.style.cursor = 'pointer';
  341. self.menuIcons.forEach(cur => {
  342. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  343. if (cur == self.menuIcons[overIcon]) { // if its the clicked icon
  344. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 1;
  345. else if (cur.iconType == 'difficulty') cur.fillColor = colors.blue;
  346. } else {
  347. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 0;
  348. else if (cur.iconType == 'difficulty') cur.fillColor = colors.gray;
  349. }
  350. }
  351. });
  352. self.func_load(self.menuIcons[overIcon]);
  353. } else document.body.style.cursor = 'auto';
  354. navigationIcons.func_onInputDown(x, y);
  355. game.render.all();
  356. },
  357. /**
  358. * Called by mouse move event
  359. *
  360. * @param {object} mouseEvent contains the mouse move coordinates
  361. */
  362. func_onInputOver: function (mouseEvent) {
  363. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  364. let overIcon;
  365. // Check if pointer is over an icon
  366. for (let i in self.menuIcons) {
  367. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  368. overIcon = i;
  369. break;
  370. }
  371. }
  372. // Update gui
  373. if (overIcon) { // if pointer is over icon
  374. document.body.style.cursor = 'pointer';
  375. self.menuIcons.forEach(cur => {
  376. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  377. if (cur == self.menuIcons[overIcon]) { // if its the icon the pointer is over
  378. if (cur.iconType == 'enter') self.enterText.style = textStyles.h3__white;
  379. cur.scale = cur.originalScale * 1.1;
  380. } else {
  381. cur.scale = cur.originalScale;
  382. }
  383. }
  384. });
  385. } else { // if pointer is not over icon
  386. self.enterText.style = textStyles.h4_white;
  387. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  388. document.body.style.cursor = 'auto';
  389. }
  390. // Check navigation icons
  391. navigationIcons.func_onInputOver(x, y);
  392. game.render.all();
  393. },
  394. }