menu.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // INFO ICONS
  35. this.menuIcons = [];
  36. this.activeIcons = this.menuIcons;
  37. let infoIcon;
  38. // --------------------------- GAME ICONS
  39. const offset = defaultWidth / (info.gameType.length + 1);
  40. for (let i = 0, x = offset; i < info.gameType.length; i++, x += offset) {
  41. const icon = game.add.image(x, defaultHeight / 2 - 70, info.gameTypeUrl[i], 1);
  42. icon.anchor(0.5, 0.5);
  43. icon.gameShape = info.gameShape[i];
  44. icon.gameType = info.gameType[i];
  45. icon.iconType = 'game';
  46. this.menuIcons.push(icon);
  47. infoIcon = game.add.image(x + 70, defaultHeight / 2 - 70 - 80, 'info', 0.6);
  48. infoIcon.anchor(0.5,0.5);
  49. infoIcon.iconType = 'infoIcon';
  50. this.menuIcons.push(infoIcon);
  51. }
  52. // --------------------------- INFORMATION BOX
  53. let cur;
  54. this.infoBoxElements = []; // grouped to be displayed/hidden when info box is oppened/closed
  55. cur = game.add.graphic.rect(0, 0, defaultWidth, defaultHeight, undefined, 0, colors.black, 0.6);
  56. cur.alpha = 0;
  57. cur.originalAlpha = 0.6;
  58. this.infoBoxElements.push(cur);
  59. cur = game.add.graphic.rect(100, 100, defaultWidth - 200, defaultHeight - 200, colors.blue, 2, colors.blueBckg, 1);
  60. cur.alpha = 0;
  61. this.infoBoxElements.push(cur);
  62. this.closeIcon = game.add.image(defaultWidth - 128, 125, 'close', 0.12);
  63. this.closeIcon.anchor(0.5,0.5);
  64. this.closeIcon.alpha = 0;
  65. this.closeIcon.iconType = 'infoBox';
  66. this.infoBoxElements.push(this.closeIcon);
  67. // ------------- EVENTS
  68. game.event.add('click', this.func_onInputDown);
  69. game.event.add('mousemove', this.func_onInputOver);
  70. },
  71. /**
  72. * Saves info from selected game and goes to next state
  73. *
  74. * @param {object} icon clicked icon
  75. */
  76. func_load: function (icon) {
  77. if (audioStatus) game.audio.beepSound.play();
  78. switch (icon.iconType) {
  79. case 'infoIcon': self.func_showInfoBox(); break;
  80. case 'infoBox': self.func_closeInfoBox(); break;
  81. case 'game':
  82. gameShape = icon.gameShape;
  83. gameTypeString = icon.gameType;
  84. switch (gameTypeString) {
  85. case 'squareOne': gameType = squareOne; break;
  86. case 'squareTwo': gameType = squareTwo; break;
  87. case 'circleOne': gameType = circleOne; break;
  88. default: console.error('Game error: the name of the game is not valid');
  89. }
  90. self.menuIcons = self.lbl_game.name;
  91. game.state.start('customMenu');
  92. break;
  93. default: console.error("Game error: Problem with selected icon.");
  94. }
  95. },
  96. /**
  97. * Display the name of the game on screen
  98. *
  99. * @param {object} icon icon for the game
  100. */
  101. func_showTitle: function (icon) {
  102. let title;
  103. switch (icon.gameShape) {
  104. case 'circle': title = game.lang.circle; break;
  105. case 'square': title = game.lang.square; break;
  106. }
  107. const type = icon.gameType.substring(icon.gameType.length - 3);
  108. switch (type) {
  109. case 'One': title += ' I'; break;
  110. case 'Two': title += ' II'; break;
  111. }
  112. self.lbl_game.name = title;
  113. },
  114. /**
  115. * Remove the name of the game from screen
  116. */
  117. func_clearTitle: function () {
  118. self.lbl_game.name = '';
  119. },
  120. /**
  121. * Displays game menu information boxes.
  122. */
  123. func_showInfoBox: function () {
  124. navigationIcons.func_addIcons( // Turn off navigation icons
  125. false, false, false,
  126. false, false,
  127. false, false);
  128. self.infoBoxElements.forEach( cur => { cur.alpha = (cur.originalAlpha) ? cur.originalAlpha : 1; }); // Make info box visible
  129. self.activeIcons = [self.closeIcon]; // Update activeIcons to info box icons
  130. },
  131. /**
  132. * Closes game menu information boxes.
  133. */
  134. func_closeInfoBox: function () {
  135. navigationIcons.func_addIcons( // Turn on navigation icons
  136. false, false, false,
  137. true, true,
  138. false, false);
  139. self.infoBoxElements.forEach( cur => { cur.alpha = 0; }); // Make info box invisible
  140. self.activeIcons = self.menuIcons; // Update activeIcons to custom menu icons
  141. },
  142. /**
  143. * Called by mouse click event
  144. *
  145. * @param {object} mouseEvent contains the mouse click coordinates
  146. */
  147. func_onInputDown: function (mouseEvent) {
  148. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  149. // Check menu icons
  150. for (let i in self.activeIcons) {
  151. // If mouse is within the bounds of an icon
  152. if (game.math.isOverIcon(x, y, self.activeIcons[i])) {
  153. // Click first valid icon
  154. self.func_load(self.activeIcons[i]);
  155. break;
  156. }
  157. }
  158. // Check navigation icons
  159. navigationIcons.func_onInputDown(x, y);
  160. game.render.all();
  161. },
  162. /**
  163. * Called by mouse move event
  164. *
  165. * @param {object} mouseEvent contains the mouse move coordinates
  166. */
  167. func_onInputOver: function (mouseEvent) {
  168. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  169. let overIcon;
  170. // Check menu icons
  171. for (let i in self.activeIcons) {
  172. if (game.math.isOverIcon(x, y, self.activeIcons[i])) {
  173. overIcon = i;
  174. break;
  175. }
  176. }
  177. // Update gui
  178. if (overIcon) { // if pointer is over icon
  179. document.body.style.cursor = 'pointer';
  180. self.activeIcons.forEach(cur => {
  181. if (cur.iconType == self.activeIcons[overIcon].iconType) { // if its in the same icon category
  182. if (cur == self.activeIcons[overIcon]) { // if its the icon the pointer is over
  183. if (cur.iconType == 'game') self.func_showTitle(cur);
  184. cur.scale = cur.originalScale * 1.1;
  185. } else {
  186. if (cur.iconType == 'game') self.func_clearTitle(cur);
  187. cur.scale = cur.originalScale;
  188. }
  189. }
  190. });
  191. } else { // if pointer is not over icon
  192. self.activeIcons.forEach(cur => { cur.scale = cur.originalScale; });
  193. document.body.style.cursor = 'auto';
  194. }
  195. // Check navigation icons
  196. navigationIcons.func_onInputOver(x, y);
  197. game.render.all();
  198. }
  199. };