menu.js 14 KB

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