menu.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /**
  2. * MAIN MENU STATE: main menu - player can select the game he wants to play
  3. *
  4. * @namespace
  5. */
  6. const menuState = {
  7. /**
  8. * Preloads media for current state
  9. */
  10. preload: function () {
  11. // LOADING MEDIA
  12. game.load.image(url.menu.image);
  13. game.load.sprite(url.menu.sprite);
  14. },
  15. /**
  16. * Main code
  17. */
  18. create: function () {
  19. // Background color
  20. game.add.graphic.rect(0, 0, 900, 600, undefined, 0, colors.blueBckg, 1);
  21. // Floor
  22. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, 501, 'floor'); }
  23. // Overtitle: Welcome, <player name>!
  24. game.add.text(defaultWidth / 2, 40, game.lang.welcome + ', ' + playerName + '!', textStyles.h4_brown);
  25. // Title : Select a game
  26. game.add.text(defaultWidth / 2, 80, game.lang.menu_title, textStyles.h1_green);
  27. // Subtitle : <game mode>
  28. this.lbl_game = game.add.text(defaultWidth / 2, 110, '', textStyles.h2_blue_2);
  29. // Loads navigation icons
  30. navigationIcons.func_addIcons(
  31. false, false, false,
  32. true, true,
  33. false, false);
  34. // --------------------------- GAME ICONS
  35. this.menuIcons = [];
  36. const offset = defaultWidth / (info.gameType.length + 1);
  37. for (let i = 0, x = offset; i < info.gameType.length; i++, x += offset) {
  38. const icon = game.add.image(x, defaultHeight / 2 - 70, info.gameTypeUrl[i], 1);
  39. icon.anchor(0.5, 0.5);
  40. icon.gameShape = info.gameShape[i];
  41. icon.gameType = info.gameType[i];
  42. this.menuIcons.push(icon);
  43. }
  44. // ------------- EVENTS
  45. game.event.add('click', this.func_onInputDown);
  46. game.event.add('mousemove', this.func_onInputOver);
  47. },
  48. /* EVENTS */
  49. /**
  50. * Called by mouse click event
  51. *
  52. * @param {object} mouseEvent contains the mouse click coordinates
  53. */
  54. func_onInputDown: function (mouseEvent) {
  55. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  56. // Check menu icons
  57. for (let i in self.menuIcons) {
  58. // If mouse is within the bounds of an icon
  59. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  60. // Click first valid icon
  61. self.func_load(self.menuIcons[i]);
  62. break;
  63. }
  64. }
  65. // Check navigation icons
  66. navigationIcons.func_onInputDown(x, y);
  67. },
  68. /**
  69. * Called by mouse move event
  70. *
  71. * @param {object} mouseEvent contains the mouse move coordinates
  72. */
  73. func_onInputOver: function (mouseEvent) {
  74. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  75. let flag = false;
  76. // Check menu icons
  77. self.menuIcons.forEach(cur => {
  78. if (game.math.isOverIcon(x, y, cur)) {
  79. cur.scale = 1.08;
  80. self.func_showTitle(cur);
  81. flag = true;
  82. } else {
  83. cur.scale = 1;
  84. }
  85. });
  86. if (flag) {
  87. document.body.style.cursor = 'pointer';
  88. } else {
  89. self.func_clearTitle();
  90. document.body.style.cursor = 'auto';
  91. }
  92. // Check navigation icons
  93. navigationIcons.func_onInputOver(x, y);
  94. game.render.all();
  95. },
  96. /* GAME FUNCTIONS */
  97. /**
  98. * Saves info from selected game and goes to next state
  99. *
  100. * @param {object} icon clicked icon
  101. */
  102. func_load: function (icon) {
  103. if (audioStatus) game.audio.beepSound.play();
  104. gameShape = icon.gameShape;
  105. gameTypeString = icon.gameType;
  106. switch (gameTypeString) {
  107. case 'squareOne': gameType = squareOne; break;
  108. case 'squareTwo': gameType = squareTwo; break;
  109. case 'circleOne': gameType = circleOne; break;
  110. default: console.error('Game error: the name of the game is not valid');
  111. }
  112. self.menuIcons = self.lbl_game.name;
  113. game.state.start('customMenu');
  114. },
  115. /**
  116. * Display the name of the game on screen
  117. *
  118. * @param {object} icon icon for the game
  119. */
  120. func_showTitle: function (icon) {
  121. let title;
  122. switch (icon.gameShape) {
  123. case 'circle': title = game.lang.circle; break;
  124. case 'square': title = game.lang.square; break;
  125. }
  126. const type = icon.gameType.substring(icon.gameType.length - 3);
  127. switch (type) {
  128. case 'One': title += ' I'; break;
  129. case 'Two': title += ' II'; break;
  130. }
  131. self.lbl_game.name = title;
  132. },
  133. /**
  134. * Remove the name of the game from screen
  135. */
  136. func_clearTitle: function () {
  137. self.lbl_game.name = '';
  138. },
  139. };
  140. /**
  141. * SECUNDARY MENU STATE: player can select game mode, math operation and overall game difficulty
  142. *
  143. * @namespace
  144. */
  145. const customMenuState = {
  146. /**
  147. * Preloads media for current state
  148. */
  149. preload: function () {
  150. // LOADING MEDIA
  151. game.load.sprite(url[gameTypeString].sprite);
  152. game.load.image(url[gameTypeString].image);
  153. },
  154. /**
  155. * Main code
  156. */
  157. create: function () {
  158. const iconScale = 0.7;
  159. const baseY = 270 - 40;
  160. this.menuIcons = [];
  161. // Background color
  162. game.add.graphic.rect(0, 0, 900, 600, undefined, 0, colors.blueBckg, 1);
  163. // Floor
  164. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, 501, 'floor'); }
  165. // Overtitle : Selected game
  166. game.add.text(defaultWidth / 2, 40, game.lang.game + ": " + menuState.menuIcons, textStyles.h4_brown);
  167. // Title : Customize the selected game
  168. game.add.text(defaultWidth / 2, 80, game.lang.custom_game, textStyles.h1_green);
  169. // Loads navigation icons
  170. navigationIcons.func_addIcons(
  171. true, false, false,
  172. true, true,
  173. 'menu', false);
  174. let x = 150;
  175. let y = 200 - 40;
  176. let width = 5;
  177. let height = 280 + 80;
  178. let offsetW = 600 / 6;
  179. let offsetH;
  180. // Label 'Game Mode'
  181. game.add.text(x + offsetW - 12, y, game.lang.game_mode, textStyles.h2_blue_2);
  182. // Label 'Operation'
  183. game.add.text(x + 3 * offsetW, y, game.lang.operation, textStyles.h2_blue_2);
  184. // Label 'Difficulty'
  185. game.add.text(x + 5 * offsetW, y, game.lang.difficulty, textStyles.h2_blue_2);
  186. // Horizontal line
  187. game.add.graphic.rect(x - 25, y + 10, 600 + 50, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  188. // Vertical lines
  189. game.add.graphic.rect(x + 2 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  190. game.add.graphic.rect(x + 4 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5, 0);
  191. // --------------------------- TURN ON/OFF FRACTION LABELS / RECTANGLE GUIDE
  192. // Horizontal line
  193. game.add.graphic.rect(x + 4 * offsetW, y + 136, 200 + 25, width, undefined, 0, colors.blueMenuLine).anchor(0, 0.5);
  194. // Label 'Show Fractions / Auxiliar rectangles'
  195. game.add.text(x + 5 * offsetW, y + 102, game.lang.show, textStyles.h4_blue_2);
  196. if (gameTypeString == 'squareTwo') {
  197. game.add.text(x + 5 * offsetW + 10, y + 102 + 24, game.lang.aux_rectangle, textStyles.h4_blue_2);
  198. } else {
  199. game.add.text(x + 5 * offsetW, y + 102 + 24, game.lang.title, textStyles.h2_blue_2);
  200. }
  201. // Selection box
  202. y += 40;
  203. const frame = (fractionLabel) ? 1 : 0;
  204. const selectionBox = game.add.sprite(x + 5 * offsetW, y + 102 + 24 - 2, 'select', frame, 0.11);
  205. selectionBox.anchor(0.5, 0.5);
  206. selectionBox.iconType = 'selectionBox';
  207. this.menuIcons.push(selectionBox);
  208. // --------------------------- GAME MODE ICONS
  209. x = 150 + offsetW;
  210. y = baseY;
  211. offsetH = this.func_getOffset(height, info[gameTypeString].gameModeType.length);
  212. for (let i = 0; i < info[gameTypeString].gameModeTypeUrl.length; i++, y += offsetH) {
  213. const icon = game.add.sprite(x, y, info[gameTypeString].gameModeTypeUrl[i], 0, iconScale, 1);
  214. icon.anchor(0.5, 0.5);
  215. icon.gameModeType = info[gameTypeString].gameModeType[i];
  216. icon.iconType = 'gameMode';
  217. if (i == 0) {
  218. gameModeType = icon.gameModeType;
  219. icon.curFrame = 1;
  220. }
  221. this.menuIcons.push(icon);
  222. }
  223. // --------------------------- GAME OPERATION ICONS
  224. x += 2 * offsetW;
  225. y = baseY;
  226. offsetH = this.func_getOffset(height, info[gameTypeString].gameOperationType.length);
  227. let icon;
  228. let aux = [];
  229. aux['squareOne'] = [
  230. ['operation_plus', 'Plus'],
  231. ['operation_minus', 'Minus']
  232. ];
  233. aux['circleOne'] = [
  234. ['operation_plus', 'Plus'],
  235. ['operation_minus', 'Minus'],
  236. ['operation_mixed', 'Mixed']
  237. ];
  238. aux['squareTwo'] = [
  239. ['operation_equals', 'Equals'],
  240. ];
  241. // Placing math operation icons
  242. for (let i = 0; i < aux[gameTypeString].length; i++, y += offsetH) {
  243. icon = game.add.sprite(x, y, aux[gameTypeString][i][0], 0, iconScale, 1);
  244. icon.anchor(0.5, 0.5);
  245. icon.gameOperationType = aux[gameTypeString][i][1];
  246. icon.iconType = 'gameOperation';
  247. if (i == 0) {
  248. gameOperationType = icon.gameOperationType;
  249. icon.curFrame = 1;
  250. }
  251. this.menuIcons.push(icon);
  252. }
  253. // --------------------------- DIFFICULTY ICONS
  254. x = (gameTypeString == 'squareOne') ? 625 : 585;
  255. y = baseY - 25;
  256. for (let i = 0; i < info[gameTypeString].gameDifficulty; i++) {
  257. // Parameters
  258. const curX = x + (30 + 10) * i;
  259. // Difficulty menuIcons
  260. const icon = game.add.graphic.rect(curX, y, 30, 30, undefined, 0, colors.gray, 1);
  261. icon.anchor(0.5, 0.5);
  262. icon.difficulty = i + 1;
  263. icon.iconType = 'difficulty';
  264. if (i == 0) {
  265. gameDifficulty = icon.difficulty;
  266. icon.fillColor = colors.blue;
  267. }
  268. this.menuIcons.push(icon);
  269. // Difficulty numbers
  270. game.add.text(curX, y + 7, i + 1, textStyles.h4_white);
  271. }
  272. // --------------------------- ENTER ICON
  273. x = defaultWidth - 100;
  274. y = defaultHeight - 110;
  275. const enterIcon = game.add.image(x, y, 'bush');
  276. enterIcon.anchor(0.5, 0.5);
  277. enterIcon.iconType = 'enter';
  278. this.menuIcons.push(enterIcon);
  279. this.enterText = game.add.text(x, y, game.lang.continue, textStyles.h4_white);
  280. // ------------- EVENTS
  281. game.event.add('click', this.func_onInputDown);
  282. game.event.add('mousemove', this.func_onInputOver);
  283. },
  284. /* GAME FUNCTIONS */
  285. /**
  286. * Saves information selected by the player
  287. *
  288. * @param {object} icon selected icon
  289. */
  290. func_load: function (icon) {
  291. if (audioStatus) game.audio.beepSound.play();
  292. const type = icon.iconType;
  293. switch (type) {
  294. case 'gameMode': gameModeType = icon.gameModeType; break;
  295. case 'gameOperation': gameOperationType = icon.gameOperationType; break;
  296. case 'difficulty': gameDifficulty = icon.difficulty; break;
  297. case 'selectionBox':
  298. if (icon.curFrame == 0) {
  299. icon.curFrame = 1;
  300. fractionLabel = true;
  301. } else {
  302. icon.curFrame = 0;
  303. fractionLabel = false;
  304. }
  305. game.render.all();
  306. break;
  307. case 'enter':
  308. if (debugMode) console.log('Game State: ' + gameTypeString + ', ' + gameModeType);
  309. mapPosition = 0; // Map position
  310. mapMove = true; // Move no next point
  311. completedLevels = 0; // Reset the game progress when entering a new level
  312. game.state.start('map');
  313. break;
  314. }
  315. },
  316. /**
  317. * Calculate spacing for icons on the menu screen
  318. *
  319. * @param {number} width width of the available part of the screen
  320. * @param {number} numberOfIcons number or icons to be put on the screen
  321. * @returns {number}
  322. */
  323. func_getOffset: function (width, numberOfIcons) {
  324. return width / (numberOfIcons + 1);
  325. },
  326. /* EVENTS */
  327. /**
  328. * Called by mouse click event
  329. *
  330. * @param {object} mouseEvent contains the mouse click coordinates
  331. */
  332. func_onInputDown: function (mouseEvent) {
  333. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  334. let overIcon;
  335. // Check if clicked on an icon
  336. for (let i in self.menuIcons) {
  337. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  338. overIcon = i;
  339. break;
  340. }
  341. }
  342. // Update gui
  343. if (overIcon) { // if has clicked on an icon
  344. document.body.style.cursor = 'pointer';
  345. self.menuIcons.forEach(cur => {
  346. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  347. if (cur == self.menuIcons[overIcon]) { // if its the clicked icon
  348. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 1;
  349. else if (cur.iconType == 'difficulty') cur.fillColor = colors.blue;
  350. } else {
  351. if (cur.iconType == 'gameMode' || cur.iconType == 'gameOperation') cur.curFrame = 0;
  352. else if (cur.iconType == 'difficulty') cur.fillColor = colors.gray;
  353. }
  354. }
  355. });
  356. self.func_load(self.menuIcons[overIcon]);
  357. } else document.body.style.cursor = 'auto';
  358. navigationIcons.func_onInputDown(x, y);
  359. game.render.all();
  360. },
  361. /**
  362. * Called by mouse move event
  363. *
  364. * @param {object} mouseEvent contains the mouse move coordinates
  365. */
  366. func_onInputOver: function (mouseEvent) {
  367. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  368. let overIcon;
  369. // Check if pointer is over an icon
  370. for (let i in self.menuIcons) {
  371. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  372. overIcon = i;
  373. break;
  374. }
  375. }
  376. // Update gui
  377. if (overIcon) { // if pointer is over icon
  378. document.body.style.cursor = 'pointer';
  379. self.menuIcons.forEach(cur => {
  380. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  381. if (cur == self.menuIcons[overIcon]) { // if its the icon the pointer is over
  382. if (cur.iconType == 'enter') self.enterText.style = textStyles.h3__white;
  383. cur.scale = cur.originalScale * 1.1;
  384. } else {
  385. cur.scale = cur.originalScale;
  386. }
  387. }
  388. });
  389. } else { // if pointer is not over icon
  390. self.enterText.style = textStyles.h4_white;
  391. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  392. document.body.style.cursor = 'auto';
  393. }
  394. // Check navigation icons
  395. navigationIcons.func_onInputOver(x, y);
  396. game.render.all();
  397. },
  398. }