menu_main.js 7.4 KB

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