menu_main.js 7.4 KB

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