menu_main.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 (
  119. gameList[icon.id] &&
  120. gameList[icon.id].assets &&
  121. gameList[icon.id].assets.menu &&
  122. gameList[icon.id].assets.menu.infoBox
  123. ) {
  124. self.infoBox.style.display = 'block';
  125. const data = gameList[icon.id].assets.menu.infoBox();
  126. const content = `<h3>${data.title}</h3>
  127. <p>${data.body}</p>
  128. ${data.img}`;
  129. document.querySelector('.ifr-modal__infobox').innerHTML = content;
  130. } else {
  131. console.error('Error: no info box was setup for this game.');
  132. }
  133. },
  134. /**
  135. * Saves info from selected game and goes to next state
  136. *
  137. * @param {object} icon clicked icon
  138. */
  139. load: function (icon) {
  140. if (audioStatus) game.audio.popSound.play();
  141. switch (icon.iconType) {
  142. case 'infoIcon':
  143. self.showInfoBox(icon);
  144. break;
  145. case 'game':
  146. gameId = icon.gameId;
  147. gameName = gameList[gameId].gameName;
  148. gameShape = gameList[gameId].gameShape;
  149. if (!gameList.find((game) => game.gameName === gameName))
  150. console.error('Game error: the name of the game is not valid.');
  151. if (isDebugMode) console.log('Game Name: ' + gameName);
  152. self.menuIcons = self.lbl_game.name;
  153. game.state.start('customMenu');
  154. break;
  155. }
  156. },
  157. /**
  158. * Display the name of the game on screen
  159. *
  160. * @param {object} icon icon for the game
  161. */
  162. showTitle: function (icon) {
  163. const number =
  164. gameList[icon.gameId].gameName.slice(-3) == 'One' ? 'I' : 'II';
  165. const shape = gameList[icon.gameId].gameName.slice(0, -3);
  166. self.lbl_game.name = game.lang[shape] + ' ' + number;
  167. },
  168. /**
  169. * Remove the name of the game from screen
  170. */
  171. clearTitle: function () {
  172. self.lbl_game.name = '';
  173. },
  174. /**
  175. * Called by mouse click event
  176. *
  177. * @param {object} mouseEvent contains the mouse click coordinates
  178. */
  179. onInputDown: function (mouseEvent) {
  180. const x = game.math.getMouse(mouseEvent).x;
  181. const y = game.math.getMouse(mouseEvent).y;
  182. // Check menu icons
  183. for (let i in self.menuIcons) {
  184. // If mouse is within the bounds of an icon
  185. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  186. // Click first valid icon
  187. self.load(self.menuIcons[i]);
  188. break;
  189. }
  190. }
  191. // Check navigation icons
  192. navigation.onInputDown(x, y);
  193. game.render.all();
  194. },
  195. /**
  196. * Called by mouse move event
  197. *
  198. * @param {object} mouseEvent contains the mouse move coordinates
  199. */
  200. onInputOver: function (mouseEvent) {
  201. const x = game.math.getMouse(mouseEvent).x;
  202. const y = game.math.getMouse(mouseEvent).y;
  203. let overIcon;
  204. // Check menu icons
  205. for (let i in self.menuIcons) {
  206. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  207. overIcon = i;
  208. break;
  209. }
  210. }
  211. // Update gui
  212. if (overIcon) {
  213. // If pointer is over icon
  214. document.body.style.cursor = 'pointer';
  215. if (self.menuIcons[overIcon].iconType == 'game')
  216. self.showTitle(self.menuIcons[overIcon]);
  217. self.menuIcons.forEach((cur) => {
  218. if (cur.iconType == self.menuIcons[overIcon].iconType) {
  219. // If its in the same icon category
  220. if (cur == self.menuIcons[overIcon]) {
  221. // If its the icon the pointer is over
  222. cur.scale = cur.initialScale * 1.1;
  223. } else {
  224. cur.scale = cur.initialScale;
  225. }
  226. }
  227. });
  228. } else {
  229. // If pointer is not over icon
  230. self.clearTitle();
  231. self.menuIcons.forEach((cur) => {
  232. cur.scale = cur.initialScale;
  233. });
  234. document.body.style.cursor = 'auto';
  235. }
  236. // Check navigation icons
  237. navigation.onInputOver(x, y);
  238. game.render.all();
  239. },
  240. };