customMenu.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. const iconScale = 0.7;
  20. const baseY = 270 - 40;
  21. this.menuIcons = [];
  22. // Background color
  23. game.add.graphic.rect(0, 0, 900, 600, undefined, 0, colors.blueBckg, 1);
  24. // Floor
  25. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, 501, 'floor'); }
  26. // Overtitle : Selected game
  27. game.add.text(defaultWidth / 2, 40, game.lang.game + ": " + menuState.menuIcons, textStyles.h4_brown);
  28. // Title : Customize the selected game
  29. game.add.text(defaultWidth / 2, 80, game.lang.custom_game, textStyles.h1_green);
  30. // Loads navigation icons
  31. navigationIcons.func_addIcons(
  32. true, false, false,
  33. true, true,
  34. 'menu', false);
  35. let x = 150;
  36. let y = 200 - 40;
  37. let width = 5;
  38. let height = 280 + 80;
  39. let offsetW = 600 / 6;
  40. let offsetH, infoIcon;
  41. // Label 'Game Mode'
  42. game.add.text(x + offsetW - 12, y, game.lang.game_mode, textStyles.h2_blue_2);
  43. infoIcon = game.add.image(x + 2 * offsetW - 30, y - 40, 'info', 0.5, 0.6);
  44. infoIcon.anchor(0.5, 0.5);
  45. infoIcon.iconType = 'infoIcon';
  46. infoIcon.id = 'gameMode';
  47. this.menuIcons.push(infoIcon);
  48. // Label 'Operation'
  49. game.add.text(x + 3 * offsetW, y, game.lang.operation, textStyles.h2_blue_2);
  50. infoIcon = game.add.image(x + 4 * offsetW - 30, y - 40, 'info', 0.5, 0.6);
  51. infoIcon.anchor(0.5, 0.5);
  52. infoIcon.iconType = 'infoIcon';
  53. infoIcon.id = 'gameOperation';
  54. this.menuIcons.push(infoIcon);
  55. // Label 'Difficulty'
  56. game.add.text(x + 5 * offsetW, y, game.lang.difficulty, textStyles.h2_blue_2);
  57. infoIcon = game.add.image(x + 6 * offsetW - 30, y - 40, 'info', 0.5, 0.6);
  58. infoIcon.anchor(0.5, 0.5);
  59. infoIcon.iconType = 'infoIcon';
  60. infoIcon.id = 'gameDifficulty';
  61. this.menuIcons.push(infoIcon);
  62. // Horizontal line
  63. game.add.graphic.rect(x - 25, y + 10, 600 + 50, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  64. // Vertical lines
  65. game.add.graphic.rect(x + 2 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  66. game.add.graphic.rect(x + 4 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  67. // --------------------------- TURN ON/OFF FRACTION LABELS / RECTANGLE GUIDE
  68. // Horizontal line
  69. game.add.graphic.rect(x + 4 * offsetW, y + 136, 200 + 25, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  70. // Label 'Show Fractions / Auxiliar rectangles'
  71. game.add.text(x + 5 * offsetW, y + 102, game.lang.show, textStyles.h4_blue_2);
  72. infoIcon = game.add.image(x + 6 * offsetW + 20, y + 102, 'info', 0.5, 0.6);
  73. infoIcon.anchor(0.5, 0.5);
  74. infoIcon.iconType = 'infoIcon';
  75. infoIcon.id = 'gameMisc';
  76. this.menuIcons.push(infoIcon);
  77. let auxText;
  78. if (gameTypeString == 'squareTwo') {
  79. auxText = game.lang.aux_rectangle;
  80. game.add.text(x + 5 * offsetW + 10, y + 102 + 24, auxText, textStyles.h4_blue_2);
  81. } else {
  82. auxText = game.lang.title;
  83. game.add.text(x + 5 * offsetW, y + 102 + 24, auxText, textStyles.h2_blue_2);
  84. }
  85. // Selection box
  86. y += 40;
  87. const frame = (fractionLabel) ? 1 : 0;
  88. const selectionBox = game.add.sprite(x + 5 * offsetW, y + 102 + 24 - 2, 'select', frame, 0.11);
  89. selectionBox.anchor(0.5, 0.5);
  90. selectionBox.iconType = 'selectionBox';
  91. this.menuIcons.push(selectionBox);
  92. // --------------------------- GAME MODE ICONS
  93. x = 150 + offsetW;
  94. y = baseY;
  95. offsetH = this.func_getOffset(height, info[gameTypeString].gameModeType.length);
  96. for (let i = 0; i < info[gameTypeString].gameModeTypeUrl.length; i++, y += offsetH) {
  97. const icon = game.add.sprite(x, y, info[gameTypeString].gameModeTypeUrl[i], 0, iconScale, 1);
  98. icon.anchor(0.5, 0.5);
  99. icon.gameModeType = info[gameTypeString].gameModeType[i];
  100. icon.iconType = 'gameMode';
  101. if (i == 0) {
  102. gameModeType = icon.gameModeType;
  103. icon.curFrame = 1;
  104. }
  105. this.menuIcons.push(icon);
  106. }
  107. // --------------------------- GAME OPERATION ICONS
  108. x += 2 * offsetW;
  109. y = baseY;
  110. offsetH = this.func_getOffset(height, info[gameTypeString].gameOperationType.length);
  111. let icon;
  112. let aux = [];
  113. aux['squareOne'] = [
  114. ['operation_plus', 'Plus'],
  115. ['operation_minus', 'Minus']
  116. ];
  117. aux['circleOne'] = [
  118. ['operation_plus', 'Plus'],
  119. ['operation_minus', 'Minus'],
  120. ['operation_mixed', 'Mixed']
  121. ];
  122. aux['squareTwo'] = [
  123. ['operation_equals', 'Equals'],
  124. ];
  125. // Placing math operation icons
  126. for (let i = 0; i < aux[gameTypeString].length; i++, y += offsetH) {
  127. icon = game.add.sprite(x, y, aux[gameTypeString][i][0], 0, iconScale, 1);
  128. icon.anchor(0.5, 0.5);
  129. icon.gameOperationType = aux[gameTypeString][i][1];
  130. icon.iconType = 'gameOperation';
  131. if (i == 0) {
  132. gameOperationType = icon.gameOperationType;
  133. icon.curFrame = 1;
  134. }
  135. this.menuIcons.push(icon);
  136. }
  137. // --------------------------- DIFFICULTY ICONS
  138. x = (gameTypeString == 'squareOne') ? 625 : 585;
  139. y = baseY - 25;
  140. for (let i = 0; i < info[gameTypeString].gameDifficulty; i++) {
  141. // Parameters
  142. const curX = x + (30 + 10) * i;
  143. // Difficulty menuIcons
  144. const icon = game.add.graphic.rect(curX, y, 30, 30, undefined, 0, colors.gray, 1);
  145. icon.anchor(0.5, 0.5);
  146. icon.difficulty = i + 1;
  147. icon.iconType = 'difficulty';
  148. if (i == 0) {
  149. gameDifficulty = icon.difficulty;
  150. icon.fillColor = colors.blue;
  151. }
  152. this.menuIcons.push(icon);
  153. // Difficulty numbers
  154. game.add.text(curX, y + 7, i + 1, textStyles.h4_white);
  155. }
  156. // --------------------------- ENTER ICON
  157. x = defaultWidth - 100;
  158. y = defaultHeight - 110;
  159. const enterIcon = game.add.image(x, y, 'bush');
  160. enterIcon.anchor(0.5, 0.5);
  161. enterIcon.iconType = 'enter';
  162. this.menuIcons.push(enterIcon);
  163. this.enterText = game.add.text(x, y, game.lang.continue, textStyles.h4_white);
  164. // --------------------------- INFO BOX
  165. this.infoBox = document.getElementById('myModal');
  166. // When the user clicks on the 'x', close the modal
  167. document.getElementsByClassName('close')[0].onclick = function () {
  168. self.infoBox.style.display = 'none';
  169. }
  170. // When the user clicks anywhere outside of the modal, close it
  171. window.onclick = function (event) {
  172. if (event.target == self.infoBox) {
  173. self.infoBox.style.display = 'none';
  174. }
  175. }
  176. this.infoBoxContent = {
  177. gameMode: {
  178. title: '<b>' + game.lang.game_mode + '</b>',
  179. body: game.lang.infoBox_mode,
  180. img: '' // '<center> <img width=300 src="./assets/img/info-box/s1-A.png"> <img width=300 src="./assets/img/info-box/s1-B.png"> </center>'
  181. },
  182. gameOperation: {
  183. title: '<b>' + game.lang.operation + '</b>',
  184. body: game.lang.infoBox_oper,
  185. img: '<center> <img width=50 src="./assets/img/info-box/operation_plus.png"> ' + game.lang.plus +
  186. ' <img width=50 src="./assets/img/info-box/operation_minus.png"> ' + game.lang.minus +
  187. ' <img width=50 src="./assets/img/info-box/operation_mixed.png"> ' + game.lang.mixed +
  188. ' <img width=50 src="./assets/img/info-box/operation_equals.png"> ' + game.lang.equals + ' </center>',
  189. },
  190. gameDifficulty: {
  191. title: '<b>' + game.lang.difficulty + '</b>',
  192. body: game.lang.infoBox_diff,
  193. img: '<center> <img width=150 src="./assets/img/info-box/dif-1.png"><img width=150 src="./assets/img/info-box/dif-5.png">'
  194. },
  195. gameMisc: {
  196. title: '<b>' + game.lang.show + ' ' + auxText + '</b>',
  197. body: game.lang.infoBox_misc,
  198. img: '' //'<center> <img width=300 src="./assets/img/info-box/c1-A.png"> <img width=300 src="./assets/img/info-box/c1-B.png"> </center>',
  199. }
  200. };
  201. // ------------- EVENTS
  202. game.event.add('click', this.func_onInputDown);
  203. game.event.add('mousemove', this.func_onInputOver);
  204. },
  205. /**
  206. * Displays game menu information boxes.
  207. */
  208. func_showInfoBox: function (icon) {
  209. self.infoBox.style.display = 'block';
  210. let msg = '<h3>' + self.infoBoxContent[icon.id].title + '</h3>'
  211. + '<p>' + self.infoBoxContent[icon.id].body + '</p>'
  212. + self.infoBoxContent[icon.id].img;
  213. document.getElementById('infobox-content').innerHTML = msg;
  214. },
  215. /**
  216. * Saves information selected by the player
  217. *
  218. * @param {object} icon selected icon
  219. */
  220. func_load: function (icon) {
  221. if (audioStatus) game.audio.beepSound.play();
  222. const type = icon.iconType;
  223. switch (type) {
  224. case 'gameMode': gameModeType = icon.gameModeType; break;
  225. case 'gameOperation': gameOperationType = icon.gameOperationType; break;
  226. case 'difficulty': gameDifficulty = icon.difficulty; break;
  227. case 'infoIcon': self.func_showInfoBox(icon); break;
  228. case 'selectionBox':
  229. if (icon.curFrame == 0) {
  230. icon.curFrame = 1;
  231. fractionLabel = true;
  232. } else {
  233. icon.curFrame = 0;
  234. fractionLabel = false;
  235. }
  236. game.render.all();
  237. break;
  238. case 'enter':
  239. if (debugMode) console.log('Game State: ' + gameTypeString + ', ' + gameModeType);
  240. mapPosition = 0; // Map position
  241. mapMove = true; // Move no next point
  242. completedLevels = 0; // Reset the game progress when entering a new level
  243. game.state.start('map');
  244. break;
  245. }
  246. },
  247. /**
  248. * Calculate spacing for icons on the menu screen
  249. *
  250. * @param {number} width width of the available part of the screen
  251. * @param {number} numberOfIcons number or icons to be put on the screen
  252. * @returns {number}
  253. */
  254. func_getOffset: function (width, numberOfIcons) {
  255. return width / (numberOfIcons + 1);
  256. },
  257. /**
  258. * Called by mouse click event
  259. *
  260. * @param {object} mouseEvent contains the mouse click coordinates
  261. */
  262. func_onInputDown: function (mouseEvent) {
  263. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  264. let overIcon;
  265. // Check if clicked on an icon
  266. for (let i in self.menuIcons) {
  267. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  268. overIcon = i;
  269. break;
  270. }
  271. }
  272. // Update gui
  273. if (overIcon) { // if has clicked on an icon
  274. document.body.style.cursor = 'pointer';
  275. self.menuIcons.forEach(cur => {
  276. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  277. if (cur == self.menuIcons[overIcon]) { // if its the clicked icon
  278. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 1;
  279. else if (cur.iconType == 'difficulty') cur.fillColor = colors.blue;
  280. } else {
  281. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 0;
  282. else if (cur.iconType == 'difficulty') cur.fillColor = colors.gray;
  283. }
  284. }
  285. });
  286. self.func_load(self.menuIcons[overIcon]);
  287. } else document.body.style.cursor = 'auto';
  288. navigationIcons.func_onInputDown(x, y);
  289. game.render.all();
  290. },
  291. /**
  292. * Called by mouse move event
  293. *
  294. * @param {object} mouseEvent contains the mouse move coordinates
  295. */
  296. func_onInputOver: function (mouseEvent) {
  297. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  298. let overIcon;
  299. // Check if pointer is over an icon
  300. for (let i in self.menuIcons) {
  301. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  302. overIcon = i;
  303. break;
  304. }
  305. }
  306. // Update gui
  307. if (overIcon) { // if pointer is over icon
  308. document.body.style.cursor = 'pointer';
  309. self.menuIcons.forEach(cur => {
  310. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  311. if (cur == self.menuIcons[overIcon]) { // if its the icon the pointer is over
  312. if (cur.iconType == 'enter') self.enterText.style = textStyles.h3__white;
  313. cur.scale = cur.originalScale * 1.1;
  314. } else {
  315. cur.scale = cur.originalScale;
  316. }
  317. }
  318. });
  319. } else { // if pointer is not over icon
  320. self.enterText.style = textStyles.h4_white;
  321. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  322. document.body.style.cursor = 'auto';
  323. }
  324. // Check navigation icons
  325. navigationIcons.func_onInputOver(x, y);
  326. game.render.all();
  327. },
  328. }