customMenu.js 12 KB

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