customMenu.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. 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. const curGame = info.all[gameType];
  42. let x = 150;
  43. let y = 200 - 40;
  44. let width = 5;
  45. let height = 280 + 80;
  46. let offsetW = 600 / 6;
  47. let offsetH, infoIcon;
  48. // Label 'Game Modes'
  49. game.add.text(x + offsetW - 12, y, game.lang.game_modes, textStyles.h2_blue_2);
  50. infoIcon = game.add.image(x + 2 * offsetW - 30, y - 40, 'info', 0.5, 0.4);
  51. infoIcon.anchor(0.5, 0.5);
  52. infoIcon.iconType = 'infoIcon';
  53. infoIcon.id = 'gameMode';
  54. this.menuIcons.push(infoIcon);
  55. // Label 'Operations'
  56. game.add.text(x + 3 * offsetW, y, game.lang.operations, textStyles.h2_blue_2);
  57. infoIcon = game.add.image(x + 4 * offsetW - 30, y - 40, 'info', 0.5, 0.4);
  58. infoIcon.anchor(0.5, 0.5);
  59. infoIcon.iconType = 'infoIcon';
  60. infoIcon.id = 'gameOperation';
  61. this.menuIcons.push(infoIcon);
  62. // Label 'Difficulties'
  63. game.add.text(x + 5 * offsetW, y, game.lang.difficulties, textStyles.h2_blue_2);
  64. infoIcon = game.add.image(x + 6 * offsetW - 30, y - 40, 'info', 0.5, 0.4);
  65. infoIcon.anchor(0.5, 0.5);
  66. infoIcon.iconType = 'infoIcon';
  67. infoIcon.id = 'gameDifficulty';
  68. this.menuIcons.push(infoIcon);
  69. // Horizontal line
  70. game.add.geom.rect(x - 25, y + 10, 600 + 50, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  71. // Vertical lines
  72. game.add.geom.rect(x + 2 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  73. game.add.geom.rect(x + 4 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  74. // --------------------------- TURN ON/OFF FRACTION LABELS / RECTANGLE GUIDE
  75. // Horizontal line
  76. game.add.geom.rect(x + 4 * offsetW, y + 136, 200 + 25, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  77. // Label 'Show Fractions / Auxiliar rectangles'
  78. game.add.text(x + 5 * offsetW, y + 102, game.lang.show, textStyles.h4_blue_2);
  79. infoIcon = game.add.image(x + 6 * offsetW + 20, y + 102, 'info', 0.5, 0.4);
  80. infoIcon.anchor(0.5, 0.5);
  81. infoIcon.iconType = 'infoIcon';
  82. infoIcon.id = 'gameMisc';
  83. this.menuIcons.push(infoIcon);
  84. let auxText;
  85. if (gameType == 'squareTwo') {
  86. auxText = game.lang.aux_rectangle;
  87. game.add.text(x + 5 * offsetW + 10, y + 102 + 24, auxText, textStyles.h4_blue_2);
  88. } else {
  89. auxText = game.lang.title;
  90. game.add.text(x + 5 * offsetW, y + 102 + 24, auxText, textStyles.h2_blue_2);
  91. }
  92. // Selection box
  93. y += 40;
  94. const frame = (fractionLabel) ? 1 : 0;
  95. const selectionBox = game.add.sprite(x + 5 * offsetW, y + 102 + 24 - 2, 'select', frame, 0.11);
  96. selectionBox.anchor(0.5, 0.5);
  97. selectionBox.iconType = 'selectionBox';
  98. this.menuIcons.push(selectionBox);
  99. // --------------------------- GAME MODE ICONS
  100. x = 150 + offsetW;
  101. y = baseY;
  102. offsetH = this.getOffset(height, curGame.gameMode.length);
  103. for (let i = 0; i < curGame.gameModeUrl.length; i++, y += offsetH) {
  104. const icon = game.add.sprite(x, y, curGame.gameModeUrl[i], 0, iconScale, 1);
  105. icon.anchor(0.5, 0.5);
  106. icon.gameMode = curGame.gameMode[i];
  107. icon.iconType = 'gameMode';
  108. if (i == 0) {
  109. gameMode = icon.gameMode;
  110. icon.curFrame = 1;
  111. }
  112. this.menuIcons.push(icon);
  113. }
  114. // --------------------------- GAME OPERATION ICONS
  115. x += 2 * offsetW;
  116. y = baseY;
  117. offsetH = this.getOffset(height, curGame.gameOperation.length);
  118. let icon;
  119. // Placing math operation icons
  120. for (let i = 0; i < curGame.gameOperation.length; i++, y += offsetH) {
  121. icon = game.add.sprite(x, y, curGame.gameOperationUrl[i], 0, iconScale, 1);
  122. icon.anchor(0.5, 0.5);
  123. icon.gameOperation = curGame.gameOperation[i];
  124. icon.iconType = 'gameOperation';
  125. if (i == 0) {
  126. gameOperation = icon.gameOperation;
  127. icon.curFrame = 1;
  128. }
  129. this.menuIcons.push(icon);
  130. }
  131. // --------------------------- DIFFICULTY ICONS
  132. x = (gameType == 'squareOne') ? 625 : 585;
  133. y = baseY - 25;
  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 = defaultWidth - 100;
  154. y = defaultHeight - 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('myModal');
  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: '<center> <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> </center>'
  198. }
  199. },
  200. gameOperation: {
  201. title: '<b>' + game.lang.operation_math + '</b>',
  202. body: game.lang.infoBox_oper,
  203. img: '<center> <table> <tr style="border-bottom: 5px solid white"> <td> <img width=50 src="' + game.image['operation_plus'].src + '"> ' + game.lang.plus +
  204. ' </td> <td> <img width=50 src="' + game.image['operation_mixed'].src + '"> ' + game.lang.mixed +
  205. ' </td> </tr> <tr> <td><img width=50 src="' + game.image['operation_minus'].src + '"> ' + game.lang.minus +
  206. ' &nbsp; </td> <td> <img width=50 src="' + game.image['operation_equals'].src + '"> ' + game.lang.equals + ' </td> </tr> <table> <center>',
  207. },
  208. gameDifficulty: {
  209. squareOne: {
  210. title: '<b>' + game.lang.difficulties + '</b>',
  211. body: game.lang.infoBox_diff + ' ' + game.lang.infoBox_diff_obs,
  212. img: '<table> <tr> <td> <b>' + game.lang.difficulty + ':</b> 1' +
  213. ' </td> <td> <b>' + game.lang.difficulty + ':</b> 3' +
  214. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['s1-diff-1'].src + '"> ' +
  215. ' </td> <td style="border-left: 4px solid white"> <img width=100% src="' + game.image['s1-diff-3'].src + '"> </td> </tr> </table> <br>' +
  216. game.lang.infoBox_diff_aux + '<center> <img width=50% src="' + game.image['map-s1'].src + '"> </center>'
  217. },
  218. circleOne: {
  219. title: '<b>' + game.lang.difficulties + '</b>',
  220. body: game.lang.infoBox_diff + ' ' + game.lang.infoBox_diff_obs,
  221. img: '<center> <table> <tr> <td style="border-right: 4px solid white"> <b>' + game.lang.difficulty + ':</b> 1' +
  222. ' </td> <td> <b>' + game.lang.difficulty + ':</b> 5' +
  223. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['c1-diff-1'].src + '"> ' +
  224. ' </td> <td style="border-left: 4px solid white"> <img width=100% src="' + game.image['c1-diff-5'].src + '"> </td> </tr> </table> <center> <br>' +
  225. game.lang.infoBox_diff_aux + '<center> <img width=50% src="' + game.image['map-c1s2'].src + '"> </center>'
  226. },
  227. squareTwo: {
  228. title: '<b>' + game.lang.difficulties + '</b>',
  229. body: game.lang.infoBox_diff,
  230. img: '<table> <tr> <td> <b>' + game.lang.difficulty + ':</b> 1' +
  231. ' </td> <td> <b>' + game.lang.difficulty + ':</b> 5' +
  232. ' </td> </tr> <tr> <td> <img width=100% src="' + game.image['s2-diff-1'].src + '"> ' +
  233. ' </td> <td style="border-left: 4px solid white"> <img width=100% src="' + game.image['s2-diff-5'].src + '"> </td> </tr> </table> <br>' +
  234. game.lang.infoBox_diff_aux + '<center> <img width=50% src="' + game.image['map-c1s2'].src + '"> </center>'
  235. },
  236. },
  237. gameMisc: {
  238. squareOne: {
  239. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  240. body: game.lang.infoBox_misc_label,
  241. img: '<center> <img width=80% src="' + game.image['s1-label'].src + '"> </center>',
  242. },
  243. circleOne: {
  244. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  245. body: game.lang.infoBox_misc_label,
  246. img: '<center> <img width=60% src="' + game.image['c1-label'].src + '"> </center>',
  247. },
  248. squareTwo: {
  249. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  250. body: game.lang.infoBox_misc_rect,
  251. img: '<center> <img width=100% src="' + game.image['s2-label'].src + '"> </center>',
  252. }
  253. }
  254. };
  255. // ------------- EVENTS
  256. game.event.add('click', this.onInputDown);
  257. game.event.add('mousemove', this.onInputOver);
  258. }
  259. },
  260. /**
  261. * Displays game menu information boxes.
  262. */
  263. showInfoBox: function (icon) {
  264. self.infoBox.style.display = 'block';
  265. const element = (icon.id == 'gameOperation') ? self.infoBoxContent[icon.id] : self.infoBoxContent[icon.id][gameType];
  266. let msg = '<h3>' + element.title + '</h3>'
  267. + '<p align=justify>' + element.body + '</p>'
  268. + element.img;
  269. document.getElementById('infobox-content').innerHTML = msg;
  270. },
  271. /**
  272. * Saves information selected by the player
  273. *
  274. * @param {object} icon selected icon
  275. */
  276. load: function (icon) {
  277. if (audioStatus) game.audio.beepSound.play();
  278. const type = icon.iconType;
  279. switch (type) {
  280. case 'gameMode': gameMode = icon.gameMode; break;
  281. case 'gameOperation': gameOperation = icon.gameOperation; break;
  282. case 'difficulty': gameDifficulty = icon.difficulty; break;
  283. case 'infoIcon': self.showInfoBox(icon); break;
  284. case 'selectionBox':
  285. if (icon.curFrame == 0) {
  286. icon.curFrame = 1;
  287. fractionLabel = true;
  288. } else {
  289. icon.curFrame = 0;
  290. fractionLabel = false;
  291. }
  292. game.render.all();
  293. break;
  294. case 'enter':
  295. if (debugMode) {
  296. console.log('------------------------------'+
  297. '\nGame State: ' + gameType +
  298. '\nGame Mode: ' + gameMode +
  299. '\n------------------------------');
  300. }
  301. mapPosition = 0; // Map position
  302. mapMove = true; // Move no next point
  303. completedLevels = 0; // Reset the game progress when entering a new level
  304. game.state.start('map');
  305. break;
  306. }
  307. },
  308. /**
  309. * Calculate spacing for icons on the menu screen
  310. *
  311. * @param {number} width width of the available part of the screen
  312. * @param {number} numberOfIcons number or icons to be put on the screen
  313. *
  314. * @returns {number} correct spacing between icons
  315. */
  316. getOffset: function (width, numberOfIcons) {
  317. return width / (numberOfIcons + 1);
  318. },
  319. /**
  320. * Called by mouse click event
  321. *
  322. * @param {object} mouseEvent contains the mouse click coordinates
  323. */
  324. onInputDown: function (mouseEvent) {
  325. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  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 = mouseEvent.offsetX, y = mouseEvent.offsetY;
  360. let overIcon;
  361. // Check if pointer is over an icon
  362. for (let i in self.menuIcons) {
  363. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  364. overIcon = i;
  365. break;
  366. }
  367. }
  368. // Update gui
  369. if (overIcon) { // If pointer is over icon
  370. document.body.style.cursor = 'pointer';
  371. self.menuIcons.forEach(cur => {
  372. if (cur.iconType == self.menuIcons[overIcon].iconType) { // If its in the same icon category
  373. if (cur == self.menuIcons[overIcon]) { // If its the icon the pointer is over
  374. if (cur.iconType == 'enter') self.enterText.style = textStyles.h3__white;
  375. cur.scale = cur.originalScale * 1.1;
  376. } else {
  377. cur.scale = cur.originalScale;
  378. }
  379. }
  380. });
  381. } else { // If pointer is not over icon
  382. if (self.enterText) self.enterText.style = textStyles.h4_white;
  383. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  384. document.body.style.cursor = 'auto';
  385. }
  386. // Check navigation icons
  387. navigationIcons.onInputOver(x, y);
  388. game.render.all();
  389. }
  390. }