menu.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, context.canvas.width, context.canvas.height, undefined, 0, colors.blueBckg, 1);
  22. // Floor
  23. for (let i = 0; i < context.canvas.width / 100; i++) { game.add.image(i * 100, context.canvas.height - 100, 'floor'); }
  24. // Overtitle: Welcome, <player name>!
  25. game.add.text(context.canvas.width / 2, 40, game.lang.welcome + ', ' + playerName + '!', textStyles.h4_brown);
  26. // Title : Select a game
  27. game.add.text(context.canvas.width / 2, 80, game.lang.menu_title, textStyles.h1_green);
  28. // Subtitle : <game mode>
  29. this.lbl_game = game.add.text(context.canvas.width / 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 = game.math.getOffset(context.canvas.width, info.gameType.length);
  40. for (let i = 0, x = offset; i < info.gameType.length; i++, x += offset) {
  41. const icon = game.add.image(x, context.canvas.height / 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, context.canvas.height / 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('my-modal');
  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: '<strong>' + game.lang.game + ':</strong> ' + game.lang.square + ' I',
  69. body: '<ul>' + game.lang.infoBox_squareOne + '</ul>',
  70. img: '<img class="mx-auto" width=60% src="' + game.image['s1-A'].src + '">'
  71. },
  72. circleOne: {
  73. title: '<strong>' + game.lang.game + ':</strong> ' + game.lang.circle + ' I',
  74. body: '<ul>' + game.lang.infoBox_circleOne + '</ul>',
  75. img: '<img class="mx-auto" width=80% src="' + game.image['c1-A'].src + '">',
  76. },
  77. squareTwo: {
  78. title: '<strong>' + game.lang.game + ':</strong> ' + game.lang.square + ' II',
  79. body: '<ul>' + game.lang.infoBox_squareTwo + '</ul>',
  80. img: '<img class="mx-auto" width=80% src="' + game.image['s2'].src + '">',
  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.popSound.play();
  105. switch (icon.iconType) {
  106. case 'infoIcon': self.showInfoBox(icon); break;
  107. case 'game':
  108. gameShape = icon.gameShape;
  109. gameType = icon.gameType;
  110. if (!info.gameType.includes(gameType)) console.error('Game error: the name of the game is not valid.');
  111. self.menuIcons = self.lbl_game.name;
  112. game.state.start('customMenu');
  113. break;
  114. }
  115. },
  116. /**
  117. * Display the name of the game on screen
  118. *
  119. * @param {object} icon icon for the game
  120. */
  121. showTitle: function (icon) {
  122. const number = (icon.gameType.slice(-3) == 'One') ? 'I' : 'II';
  123. self.lbl_game.name = game.lang[icon.gameShape] + ' ' + number;
  124. },
  125. /**
  126. * Remove the name of the game from screen
  127. */
  128. clearTitle: function () {
  129. self.lbl_game.name = '';
  130. },
  131. /**
  132. * Called by mouse click event
  133. *
  134. * @param {object} mouseEvent contains the mouse click coordinates
  135. */
  136. onInputDown: function (mouseEvent) {
  137. const x = game.math.getMouse(mouseEvent).x;
  138. const y = game.math.getMouse(mouseEvent).y;
  139. // Check menu icons
  140. for (let i in self.menuIcons) {
  141. // If mouse is within the bounds of an icon
  142. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  143. // Click first valid icon
  144. self.load(self.menuIcons[i]);
  145. break;
  146. }
  147. }
  148. // Check navigation icons
  149. navigationIcons.onInputDown(x, y);
  150. game.render.all();
  151. },
  152. /**
  153. * Called by mouse move event
  154. *
  155. * @param {object} mouseEvent contains the mouse move coordinates
  156. */
  157. onInputOver: function (mouseEvent) {
  158. const x = game.math.getMouse(mouseEvent).x;
  159. const y = game.math.getMouse(mouseEvent).y;
  160. let overIcon;
  161. // Check menu icons
  162. for (let i in self.menuIcons) {
  163. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  164. overIcon = i;
  165. break;
  166. }
  167. }
  168. // Update gui
  169. if (overIcon) { // If pointer is over icon
  170. document.body.style.cursor = 'pointer';
  171. if (self.menuIcons[overIcon].iconType == 'game') self.showTitle(self.menuIcons[overIcon]);
  172. self.menuIcons.forEach(cur => {
  173. if (cur.iconType == self.menuIcons[overIcon].iconType) { // If its in the same icon category
  174. if (cur == self.menuIcons[overIcon]) { // If its the icon the pointer is over
  175. cur.scale = cur.originalScale * 1.1;
  176. } else {
  177. cur.scale = cur.originalScale;
  178. }
  179. }
  180. });
  181. } else { // If pointer is not over icon
  182. self.clearTitle();
  183. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  184. document.body.style.cursor = 'auto';
  185. }
  186. // Check navigation icons
  187. navigationIcons.onInputOver(x, y);
  188. game.render.all();
  189. }
  190. };