menu.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [MAIN MENU STATE] Screen where the user can select a game.
  5. *
  6. * @namespace
  7. */
  8. const menuState = {
  9. /**
  10. * Main code
  11. */
  12. create: function () {
  13. // FOR MOODLE
  14. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'false') { // Student role
  15. playerName = game.lang.student; // TODO pegar o nome do aluno no bd do moodle
  16. getiLMContent();
  17. } else {
  18. // FOR MOODLE
  19. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'true') playerName = game.lang.professor;
  20. // Background color
  21. game.add.geom.rect(0, 0, defaultWidth, defaultHeight, undefined, 0, colors.blueBckg, 1);
  22. // Floor
  23. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, defaultHeight - 100, 'floor'); }
  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. // Loads navigation icons
  31. navigationIcons.add(
  32. false, false, false,
  33. true, true,
  34. false, false);
  35. // INFO ICONS
  36. 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. // "more information" button
  48. infoIcon = game.add.image(x + 70, defaultHeight / 2 - 70 - 80, 'info', 0.6, 0.4);
  49. infoIcon.anchor(0.5, 0.5);
  50. infoIcon.iconType = 'infoIcon';
  51. infoIcon.id = icon.gameType;
  52. this.menuIcons.push(infoIcon);
  53. }
  54. // --------------------------- INFO BOX
  55. this.infoBox = document.getElementById('myModal');
  56. // When the user clicks on the 'x', close the modal
  57. document.getElementsByClassName('close')[0].onclick = function () {
  58. self.infoBox.style.display = 'none';
  59. }
  60. // When the user clicks anywhere outside of the modal, close it
  61. window.onclick = function (event) {
  62. if (event.target == self.infoBox) {
  63. self.infoBox.style.display = 'none';
  64. }
  65. }
  66. this.infoBoxContent = {
  67. squareOne: {
  68. title: '<b>' + game.lang.game + ':</b> ' + game.lang.square + ' I',
  69. body: '<ul>' + game.lang.infoBox_squareOne + '</ul>',
  70. img: '<center> <img width=60% src="' + game.image['s1-A'].src + '"./assets/img/info_box/s1-A.png"> </center>'
  71. },
  72. circleOne: {
  73. title: '<b>' + game.lang.game + ':</b> ' + game.lang.circle + ' I',
  74. body: '<ul>' + game.lang.infoBox_circleOne + '</ul>',
  75. img: '<center> <img width=80% src="' + game.image['c1-A'].src + '"> </center>',
  76. },
  77. squareTwo: {
  78. title: '<b>' + game.lang.game + ':</b> ' + game.lang.square + ' II',
  79. body: '<ul>' + game.lang.infoBox_squareTwo + '</ul>',
  80. img: '<center> <img width=80% src="' + game.image['s2'].src + '"> </center>',
  81. }
  82. };
  83. // ------------- EVENTS
  84. game.event.add('click', this.onInputDown);
  85. game.event.add('mousemove', this.onInputOver);
  86. }
  87. },
  88. /**
  89. * Displays game menu information boxes.
  90. */
  91. showInfoBox: function (icon) {
  92. self.infoBox.style.display = 'block';
  93. let msg = '<h3>' + self.infoBoxContent[icon.id].title + '</h3>'
  94. + '<p>' + self.infoBoxContent[icon.id].body + '</p>'
  95. + self.infoBoxContent[icon.id].img;
  96. document.getElementById('infobox-content').innerHTML = msg;
  97. },
  98. /**
  99. * Saves info from selected game and goes to next state
  100. *
  101. * @param {object} icon clicked icon
  102. */
  103. load: function (icon) {
  104. if (audioStatus) game.audio.beepSound.play();
  105. switch (icon.iconType) {
  106. case 'infoIcon': self.showInfoBox(icon); break;
  107. case 'game':
  108. gameShape = icon.gameShape;
  109. gameTypeString = icon.gameType;
  110. switch (gameTypeString) {
  111. case 'squareOne': gameType = squareOne; break;
  112. case 'squareTwo': gameType = squareTwo; break;
  113. case 'circleOne': gameType = circleOne; break;
  114. default: console.error('Game error: the name of the game is not valid.');
  115. }
  116. self.menuIcons = self.lbl_game.name;
  117. game.state.start('customMenu');
  118. break;
  119. }
  120. },
  121. /**
  122. * Display the name of the game on screen
  123. *
  124. * @param {object} icon icon for the game
  125. */
  126. showTitle: function (icon) {
  127. const number = (icon.gameType.slice(-3) == 'One') ? 'I' : 'II';
  128. self.lbl_game.name = game.lang[icon.gameShape] + ' ' + number;
  129. },
  130. /**
  131. * Remove the name of the game from screen
  132. */
  133. clearTitle: function () {
  134. self.lbl_game.name = '';
  135. },
  136. /**
  137. * Called by mouse click event
  138. *
  139. * @param {object} mouseEvent contains the mouse click coordinates
  140. */
  141. onInputDown: function (mouseEvent) {
  142. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  143. // Check menu icons
  144. for (let i in self.menuIcons) {
  145. // If mouse is within the bounds of an icon
  146. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  147. // Click first valid icon
  148. self.load(self.menuIcons[i]);
  149. break;
  150. }
  151. }
  152. // Check navigation icons
  153. navigationIcons.onInputDown(x, y);
  154. game.render.all();
  155. },
  156. /**
  157. * Called by mouse move event
  158. *
  159. * @param {object} mouseEvent contains the mouse move coordinates
  160. */
  161. onInputOver: function (mouseEvent) {
  162. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  163. let overIcon;
  164. // Check menu icons
  165. for (let i in self.menuIcons) {
  166. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  167. overIcon = i;
  168. break;
  169. }
  170. }
  171. // Update gui
  172. if (overIcon) { // If pointer is over icon
  173. document.body.style.cursor = 'pointer';
  174. if (self.menuIcons[overIcon].iconType == 'game') self.showTitle(self.menuIcons[overIcon]);
  175. self.menuIcons.forEach(cur => {
  176. if (cur.iconType == self.menuIcons[overIcon].iconType) { // If its in the same icon category
  177. if (cur == self.menuIcons[overIcon]) { // If its the icon the pointer is over
  178. cur.scale = cur.originalScale * 1.1;
  179. } else {
  180. cur.scale = cur.originalScale;
  181. }
  182. }
  183. });
  184. } else { // If pointer is not over icon
  185. self.clearTitle();
  186. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  187. document.body.style.cursor = 'auto';
  188. }
  189. // Check navigation icons
  190. navigationIcons.onInputOver(x, y);
  191. game.render.all();
  192. }
  193. };