menu_main.js 7.3 KB

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