menu_main.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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') {
  15. // Student role
  16. playerName = game.lang.student; // TODO pegar o nome do aluno no bd do moodle
  17. getiLMContent();
  18. } else {
  19. // FOR MOODLE
  20. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'true')
  21. playerName = game.lang.professor;
  22. renderBackground();
  23. // Overtitle: Welcome, <player name>!
  24. game.add.text(
  25. context.canvas.width / 2,
  26. 60,
  27. game.lang.welcome + ', ' + playerName + '!',
  28. { ...textStyles.h3_, fill: colors.maroon }
  29. );
  30. // Title : Select a game
  31. game.add.text(context.canvas.width / 2, 120, game.lang.menu_title, {
  32. ...textStyles.h1_,
  33. fill: colors.green,
  34. });
  35. // Subtitle : <game mode>
  36. this.lbl_game = game.add.text(
  37. context.canvas.width / 2,
  38. 170,
  39. '',
  40. textStyles.h2_
  41. );
  42. // Loads navigation icons
  43. navigation.add.right(['audio', 'lang']);
  44. this.menuIcons = [];
  45. // --------------------------- GAME ICONS
  46. const offset = game.math.getOffset(context.canvas.width, gameList.length);
  47. for (let i = 0, x = offset; i < gameList.length; i++, x += offset) {
  48. const icon = game.add.image(
  49. x,
  50. context.canvas.height / 2 - 70,
  51. gameList[i].assets.menu.gameNameBtn,
  52. 1.5
  53. );
  54. icon.anchor(0.5, 0.5);
  55. icon.gameId = i;
  56. icon.iconType = 'game';
  57. this.menuIcons.push(icon);
  58. // "more information" button
  59. const infoIcon = game.add.image(
  60. x + 110,
  61. context.canvas.height / 2 - 100 - 80 - 10,
  62. 'info',
  63. 0.8,
  64. 0.6
  65. );
  66. infoIcon.anchor(0.5, 0.5);
  67. infoIcon.iconType = 'infoIcon';
  68. infoIcon.id = i;
  69. this.menuIcons.push(infoIcon);
  70. }
  71. // --------------------------- INFO BOX
  72. this.setInfoBoxes();
  73. // ------------- EVENTS
  74. game.event.add('click', this.onInputDown);
  75. game.event.add('mousemove', this.onInputOver);
  76. if (isDebugMode && debugState.menu.skip) {
  77. // programmatically select a game
  78. const id = debugState.menu.id;
  79. gameId = id;
  80. gameName = gameList[id].gameName;
  81. gameShape = gameList[id].gameShape;
  82. audioStatus = debugState.menu.audioStatus || false;
  83. self.menuIcons =
  84. game.lang[gameShape] + ' ' + gameName.slice(-3) == 'One' ? 'I' : 'II';
  85. game.state.start('customMenu');
  86. }
  87. }
  88. },
  89. setInfoBoxes: function () {
  90. self.infoBox = document.querySelector('.ifr-modal');
  91. // When the user clicks on the 'x', close the modal
  92. document.querySelector('.ifr-modal__closeButton').onclick = function () {
  93. self.infoBox.style.display = 'none';
  94. };
  95. // When the user clicks anywhere outside of the modal, close it
  96. window.onclick = function (event) {
  97. if (event.target == self.infoBox) {
  98. self.infoBox.style.display = 'none';
  99. }
  100. };
  101. },
  102. /**
  103. * Displays game menu information boxes.
  104. */
  105. showInfoBox: function (icon) {
  106. if (gameList?.[icon.id]?.assets?.menu?.infoBox) {
  107. self.infoBox.style.display = 'block';
  108. const data = gameList[icon.id].assets.menu.infoBox();
  109. const content = `<h3>${data.title}</h3>
  110. <p>${data.body}</p>
  111. ${data.img}`;
  112. document.querySelector('.ifr-modal__infobox').innerHTML = content;
  113. } else {
  114. console.error('Error: no info box was setup for this game.');
  115. }
  116. },
  117. /**
  118. * Saves info from selected game and goes to next state
  119. *
  120. * @param {object} icon clicked icon
  121. */
  122. load: function (icon) {
  123. if (audioStatus) game.audio.popSound.play();
  124. switch (icon.iconType) {
  125. case 'infoIcon':
  126. self.showInfoBox(icon);
  127. break;
  128. case 'game':
  129. gameId = icon.gameId;
  130. gameName = gameList[gameId].gameName;
  131. gameShape = gameList[gameId].gameShape;
  132. if (!gameList.find((game) => game.gameName === gameName))
  133. console.error('Game error: the name of the game is not valid.');
  134. if (isDebugMode) console.log('Game Name: ' + gameName);
  135. self.menuIcons = self.lbl_game.name;
  136. game.state.start('customMenu');
  137. break;
  138. }
  139. },
  140. /**
  141. * Display the name of the game on screen
  142. *
  143. * @param {object} icon icon for the game
  144. */
  145. showTitle: function (icon) {
  146. const number =
  147. gameList[icon.gameId].gameName.slice(-3) == 'One' ? 'I' : 'II';
  148. const shape = gameList[icon.gameId].gameName.slice(0, -3);
  149. self.lbl_game.name = game.lang[shape] + ' ' + number;
  150. },
  151. /**
  152. * Remove the name of the game from screen
  153. */
  154. clearTitle: function () {
  155. self.lbl_game.name = '';
  156. },
  157. /**
  158. * Called by mouse click event
  159. *
  160. * @param {object} mouseEvent contains the mouse click coordinates
  161. */
  162. onInputDown: function (mouseEvent) {
  163. const x = game.math.getMouse(mouseEvent).x;
  164. const y = game.math.getMouse(mouseEvent).y;
  165. // Check menu icons
  166. for (let i in self.menuIcons) {
  167. // If mouse is within the bounds of an icon
  168. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  169. // Click first valid icon
  170. self.load(self.menuIcons[i]);
  171. break;
  172. }
  173. }
  174. // Check navigation icons
  175. navigation.onInputDown(x, y);
  176. game.render.all();
  177. },
  178. /**
  179. * Called by mouse move event
  180. *
  181. * @param {object} mouseEvent contains the mouse move coordinates
  182. */
  183. onInputOver: function (mouseEvent) {
  184. const x = game.math.getMouse(mouseEvent).x;
  185. const y = game.math.getMouse(mouseEvent).y;
  186. let overIcon;
  187. // Check menu icons
  188. for (let i in self.menuIcons) {
  189. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  190. overIcon = i;
  191. break;
  192. }
  193. }
  194. // Update gui
  195. if (overIcon) {
  196. // If pointer is over icon
  197. document.body.style.cursor = 'pointer';
  198. if (self.menuIcons[overIcon].iconType == 'game')
  199. self.showTitle(self.menuIcons[overIcon]);
  200. self.menuIcons.forEach((cur) => {
  201. if (cur.iconType == self.menuIcons[overIcon].iconType) {
  202. // If its in the same icon category
  203. if (cur == self.menuIcons[overIcon]) {
  204. // If its the icon the pointer is over
  205. cur.scale = cur.initialScale * 1.1;
  206. } else {
  207. cur.scale = cur.initialScale;
  208. }
  209. }
  210. });
  211. } else {
  212. // If pointer is not over icon
  213. self.clearTitle();
  214. self.menuIcons.forEach((cur) => {
  215. cur.scale = cur.initialScale;
  216. });
  217. document.body.style.cursor = 'auto';
  218. }
  219. // Check navigation icons
  220. navigation.onInputOver(x, y);
  221. game.render.all();
  222. },
  223. };