menu.js 7.2 KB

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