menu.js 15 KB

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