menu.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // FOR MOODLE
  20. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  21. playerName = 'Aluno'; // TODO pegar o nome do aluno no bd do moodle
  22. getiLMContent();
  23. } else {
  24. // FOR MOODLE
  25. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'true') playerName = 'Professor';
  26. // Background color
  27. game.add.geom.rect(0, 0, defaultWidth, defaultHeight, undefined, 0, colors.blueBckg, 1);
  28. // Floor
  29. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, defaultHeight - 100, 'floor'); }
  30. // Overtitle: Welcome, <player name>!
  31. game.add.text(defaultWidth / 2, 40, game.lang.welcome + ', ' + playerName + '!', textStyles.h4_brown);
  32. // Title : Select a game
  33. game.add.text(defaultWidth / 2, 80, game.lang.menu_title, textStyles.h1_green);
  34. // Subtitle : <game mode>
  35. this.lbl_game = game.add.text(defaultWidth / 2, 110, '', textStyles.h2_blue_2);
  36. // Loads navigation icons
  37. navigationIcons.func_addIcons(
  38. false, false, false,
  39. true, true,
  40. false, false);
  41. // INFO ICONS
  42. this.menuIcons = [];
  43. let infoIcon;
  44. // --------------------------- GAME ICONS
  45. const offset = defaultWidth / (info.gameType.length + 1);
  46. for (let i = 0, x = offset; i < info.gameType.length; i++, x += offset) {
  47. const icon = game.add.image(x, defaultHeight / 2 - 70, info.gameTypeUrl[i], 1);
  48. icon.anchor(0.5, 0.5);
  49. icon.gameShape = info.gameShape[i];
  50. icon.gameType = info.gameType[i];
  51. icon.iconType = 'game';
  52. this.menuIcons.push(icon);
  53. // "more information" button
  54. infoIcon = game.add.image(x + 70, defaultHeight / 2 - 70 - 80, 'info', 0.6, 0.4);
  55. infoIcon.anchor(0.5, 0.5);
  56. infoIcon.iconType = 'infoIcon';
  57. infoIcon.id = icon.gameType;
  58. this.menuIcons.push(infoIcon);
  59. }
  60. // --------------------------- INFO BOX
  61. this.infoBox = document.getElementById('myModal');
  62. // When the user clicks on the 'x', close the modal
  63. document.getElementsByClassName('close')[0].onclick = function () {
  64. self.infoBox.style.display = 'none';
  65. }
  66. // When the user clicks anywhere outside of the modal, close it
  67. window.onclick = function (event) {
  68. if (event.target == self.infoBox) {
  69. self.infoBox.style.display = 'none';
  70. }
  71. }
  72. this.infoBoxContent = {
  73. squareOne: {
  74. title: '<b>' + game.lang.game + ':</b> ' + game.lang.square + ' I',
  75. body: '<ul>' + game.lang.infoBox_squareOne + '</ul>',
  76. img: '<center> <img width=60% src="' + game.image['s1-A'].src + '"./assets/img/info_box/s1-A.png"> </center>'
  77. },
  78. circleOne: {
  79. title: '<b>' + game.lang.game + ':</b> ' + game.lang.circle + ' I',
  80. body: '<ul>' + game.lang.infoBox_circleOne + '</ul>',
  81. img: '<center> <img width=80% src="' + game.image['c1-A'].src + '"> </center>',
  82. },
  83. squareTwo: {
  84. title: '<b>' + game.lang.game + ':</b> ' + game.lang.square + ' II',
  85. body: '<ul>' + game.lang.infoBox_squareTwo + '</ul>',
  86. img: '<center> <img width=80% src="' + game.image['s2'].src + '"> </center>',
  87. }
  88. };
  89. // ------------- EVENTS
  90. game.event.add('click', this.func_onInputDown);
  91. game.event.add('mousemove', this.func_onInputOver);
  92. }
  93. },
  94. /**
  95. * Displays game menu information boxes.
  96. */
  97. func_showInfoBox: function (icon) {
  98. self.infoBox.style.display = 'block';
  99. let msg = '<h3>' + self.infoBoxContent[icon.id].title + '</h3>'
  100. + '<p>' + self.infoBoxContent[icon.id].body + '</p>'
  101. + self.infoBoxContent[icon.id].img;
  102. document.getElementById('infobox-content').innerHTML = msg;
  103. },
  104. /**
  105. * Saves info from selected game and goes to next state
  106. *
  107. * @param {object} icon clicked icon
  108. */
  109. func_load: function (icon) {
  110. if (audioStatus) game.audio.beepSound.play();
  111. switch (icon.iconType) {
  112. case 'infoIcon': self.func_showInfoBox(icon); break;
  113. case 'game':
  114. gameShape = icon.gameShape;
  115. gameTypeString = icon.gameType;
  116. switch (gameTypeString) {
  117. case 'squareOne': gameType = squareOne; break;
  118. case 'squareTwo': gameType = squareTwo; break;
  119. case 'circleOne': gameType = circleOne; break;
  120. default: console.error('Game error: the name of the game is not valid');
  121. }
  122. self.menuIcons = self.lbl_game.name;
  123. game.state.start('customMenu');
  124. break;
  125. default: console.error("Game error: Problem with selected icon.");
  126. }
  127. },
  128. /**
  129. * Display the name of the game on screen
  130. *
  131. * @param {object} icon icon for the game
  132. */
  133. func_showTitle: function (icon) {
  134. const number = (icon.gameType.slice(-3) == 'One') ? 'I' : 'II';
  135. self.lbl_game.name = game.lang[icon.gameShape] + ' ' + number;
  136. },
  137. /**
  138. * Remove the name of the game from screen
  139. */
  140. func_clearTitle: function () {
  141. self.lbl_game.name = '';
  142. },
  143. /**
  144. * Called by mouse click event
  145. *
  146. * @param {object} mouseEvent contains the mouse click coordinates
  147. */
  148. func_onInputDown: function (mouseEvent) {
  149. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  150. // Check menu icons
  151. for (let i in self.menuIcons) {
  152. // If mouse is within the bounds of an icon
  153. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  154. // Click first valid icon
  155. self.func_load(self.menuIcons[i]);
  156. break;
  157. }
  158. }
  159. // Check navigation icons
  160. navigationIcons.func_onInputDown(x, y);
  161. game.render.all();
  162. },
  163. /**
  164. * Called by mouse move event
  165. *
  166. * @param {object} mouseEvent contains the mouse move coordinates
  167. */
  168. func_onInputOver: function (mouseEvent) {
  169. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  170. let overIcon;
  171. // Check menu icons
  172. for (let i in self.menuIcons) {
  173. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  174. overIcon = i;
  175. break;
  176. }
  177. }
  178. // Update gui
  179. if (overIcon) { // if pointer is over icon
  180. document.body.style.cursor = 'pointer';
  181. if (self.menuIcons[overIcon].iconType == 'game') self.func_showTitle(self.menuIcons[overIcon]);
  182. self.menuIcons.forEach(cur => {
  183. if (cur.iconType == self.menuIcons[overIcon].iconType) { // if its in the same icon category
  184. if (cur == self.menuIcons[overIcon]) { // if its the icon the pointer is over
  185. cur.scale = cur.originalScale * 1.1;
  186. } else {
  187. cur.scale = cur.originalScale;
  188. }
  189. }
  190. });
  191. } else { // if pointer is not over icon
  192. self.func_clearTitle();
  193. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  194. document.body.style.cursor = 'auto';
  195. }
  196. // Check navigation icons
  197. navigationIcons.func_onInputOver(x, y);
  198. game.render.all();
  199. }
  200. };