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