menu.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. let infoIcon;
  37. // --------------------------- GAME ICONS
  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. icon.iconType = 'game';
  45. this.menuIcons.push(icon);
  46. // "more information" button
  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. infoIcon.id = icon.gameType;
  51. this.menuIcons.push(infoIcon);
  52. }
  53. // --------------------------- INFO BOX
  54. this.infoBox = document.getElementById('myModal');
  55. // When the user clicks on the 'x', close the modal
  56. document.getElementsByClassName('close')[0].onclick = function () {
  57. self.infoBox.style.display = 'none';
  58. }
  59. // When the user clicks anywhere outside of the modal, close it
  60. window.onclick = function (event) {
  61. if (event.target == self.infoBox) {
  62. self.infoBox.style.display = 'none';
  63. }
  64. }
  65. this.infoBoxContent = {
  66. squareOne: {
  67. title: '<b>' + game.lang.game.toLowerCase() + ':</b> ' + game.lang.square + ' I',
  68. body: game.lang.infoBox_squareOne,
  69. img: '<center> <img width=300 src="./assets/img/info-box/s1-A.png"> <img width=300 src="./assets/img/info-box/s1-B.png"> </center>'
  70. },
  71. squareTwo: {
  72. title: '<b>' + game.lang.game.toLowerCase() + ':</b> ' + game.lang.square + ' II',
  73. body: game.lang.infoBox_squareTwo,
  74. img: '<center> <img width=400 src="./assets/img/info-box/s2.png"> </center>',
  75. },
  76. circleOne: {
  77. title: '<b>' + game.lang.game.toLowerCase() + ':</b> ' + game.lang.circle + ' I',
  78. body: game.lang.infoBox_circleOne,
  79. img: '<center> <img width=300 src="./assets/img/info-box/c1-A.png"> <img width=300 src="./assets/img/info-box/c1-B.png"> </center>',
  80. }
  81. };
  82. // ------------- EVENTS
  83. game.event.add('click', this.func_onInputDown);
  84. game.event.add('mousemove', this.func_onInputOver);
  85. },
  86. /**
  87. * Displays game menu information boxes.
  88. */
  89. func_showInfoBox: function (icon) {
  90. self.infoBox.style.display = 'block';
  91. let msg = '<h3>' + self.infoBoxContent[icon.id].title + '</h3>'
  92. + '<p>' + self.infoBoxContent[icon.id].body + '</p>'
  93. + self.infoBoxContent[icon.id].img;
  94. document.getElementById('infobox-content').innerHTML = msg;
  95. },
  96. /**
  97. * Saves info from selected game and goes to next state
  98. *
  99. * @param {object} icon clicked icon
  100. */
  101. func_load: function (icon) {
  102. if (audioStatus) game.audio.beepSound.play();
  103. switch (icon.iconType) {
  104. case 'infoIcon': self.func_showInfoBox(icon); break;
  105. case 'game':
  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. break;
  117. default: console.error("Game error: Problem with selected icon.");
  118. }
  119. },
  120. /**
  121. * Display the name of the game on screen
  122. *
  123. * @param {object} icon icon for the game
  124. */
  125. func_showTitle: function (icon) {
  126. let title = game.lang[icon.gameShape];
  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. * Called by mouse click event
  142. *
  143. * @param {object} mouseEvent contains the mouse click coordinates
  144. */
  145. func_onInputDown: function (mouseEvent) {
  146. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  147. // Check menu icons
  148. for (let i in self.menuIcons) {
  149. // If mouse is within the bounds of an icon
  150. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  151. // Click first valid icon
  152. self.func_load(self.menuIcons[i]);
  153. break;
  154. }
  155. }
  156. // Check navigation icons
  157. navigationIcons.func_onInputDown(x, y);
  158. game.render.all();
  159. },
  160. /**
  161. * Called by mouse move event
  162. *
  163. * @param {object} mouseEvent contains the mouse move coordinates
  164. */
  165. func_onInputOver: function (mouseEvent) {
  166. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  167. let overIcon;
  168. // Check menu icons
  169. for (let i in self.menuIcons) {
  170. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  171. overIcon = i;
  172. break;
  173. }
  174. }
  175. // Update gui
  176. if (overIcon) { // if pointer is over icon
  177. document.body.style.cursor = 'pointer';
  178. if (self.menuIcons[overIcon].iconType == 'game') self.func_showTitle(self.menuIcons[overIcon]);
  179. self.menuIcons.forEach(cur => {
  180. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  181. if (cur == self.menuIcons[overIcon]) { // if its the icon the pointer is over
  182. cur.scale = cur.originalScale * 1.1;
  183. } else {
  184. cur.scale = cur.originalScale;
  185. }
  186. }
  187. });
  188. } else { // if pointer is not over icon
  189. self.func_clearTitle();
  190. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  191. document.body.style.cursor = 'auto';
  192. }
  193. // Check navigation icons
  194. navigationIcons.func_onInputOver(x, y);
  195. game.render.all();
  196. }
  197. };