menu_main.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_brown
  29. );
  30. // Title : Select a game
  31. game.add.text(
  32. context.canvas.width / 2,
  33. 120,
  34. game.lang.menu_title,
  35. textStyles.h1_green
  36. );
  37. // Subtitle : <game mode>
  38. this.lbl_game = game.add.text(
  39. context.canvas.width / 2,
  40. 160,
  41. '',
  42. textStyles.h2_blue
  43. );
  44. // Loads navigation icons
  45. navigationIcons.add(false, false, false, true, true, false, false);
  46. // INFO ICONS
  47. this.menuIcons = [];
  48. let infoIcon;
  49. // --------------------------- GAME ICONS
  50. const offset = game.math.getOffset(
  51. context.canvas.width,
  52. metadata.gameNames.length
  53. );
  54. for (
  55. let i = 0, x = offset;
  56. i < metadata.gameNames.length;
  57. i++, x += offset
  58. ) {
  59. const icon = game.add.image(
  60. x,
  61. context.canvas.height / 2 - 70,
  62. metadata.gameNameIconNames[i],
  63. 1.5
  64. );
  65. icon.anchor(0.5, 0.5);
  66. icon.gameShape = metadata.gameShapes[i];
  67. icon.gameName = metadata.gameNames[i];
  68. icon.iconType = 'game';
  69. this.menuIcons.push(icon);
  70. // "more information" button
  71. infoIcon = game.add.image(
  72. x + 110,
  73. context.canvas.height / 2 - 100 - 80 - 10,
  74. 'info',
  75. 1,
  76. 1
  77. );
  78. infoIcon.anchor(0.5, 0.5);
  79. infoIcon.iconType = 'infoIcon';
  80. infoIcon.id = icon.gameName;
  81. this.menuIcons.push(infoIcon);
  82. }
  83. // --------------------------- INFO BOX
  84. this.infoBox = document.querySelector('.ifr-modal');
  85. // When the user clicks on the 'x', close the modal
  86. document.querySelector('.ifr-modal__closeButton').onclick = function () {
  87. self.infoBox.style.display = 'none';
  88. };
  89. // When the user clicks anywhere outside of the modal, close it
  90. window.onclick = function (event) {
  91. if (event.target == self.infoBox) {
  92. self.infoBox.style.display = 'none';
  93. }
  94. };
  95. this.infoBoxContent = {
  96. squareOne: {
  97. title:
  98. '<strong>' +
  99. game.lang.game +
  100. ':</strong> ' +
  101. game.lang.square +
  102. ' I',
  103. body: '<ul>' + game.lang.infoBox_squareOne + '</ul>',
  104. img:
  105. '<img class="mx-auto" width=60% src="' +
  106. game.image['s1-A'].src +
  107. '">',
  108. },
  109. circleOne: {
  110. title:
  111. '<strong>' +
  112. game.lang.game +
  113. ':</strong> ' +
  114. game.lang.circle +
  115. ' I',
  116. body: '<ul>' + game.lang.infoBox_circleOne + '</ul>',
  117. img:
  118. '<img class="mx-auto" width=80% src="' +
  119. game.image['c1-A'].src +
  120. '">',
  121. },
  122. squareTwo: {
  123. title:
  124. '<strong>' +
  125. game.lang.game +
  126. ':</strong> ' +
  127. game.lang.square +
  128. ' II',
  129. body: '<ul>' + game.lang.infoBox_squareTwo + '</ul>',
  130. img:
  131. '<img class="mx-auto" width=80% src="' +
  132. game.image['s2'].src +
  133. '">',
  134. },
  135. };
  136. // ------------- EVENTS
  137. game.event.add('click', this.onInputDown);
  138. game.event.add('mousemove', this.onInputOver);
  139. // console.log('DEBUG');
  140. // const s1 = 0;
  141. // const c1 = 2;
  142. // const s2 = 4;
  143. // const sc1 = 6;
  144. // this.load(self.menuIcons[sc1]);
  145. }
  146. },
  147. /**
  148. * Displays game menu information boxes.
  149. */
  150. showInfoBox: function (icon) {
  151. self.infoBox.style.display = 'block';
  152. let msg =
  153. '<h3>' +
  154. self.infoBoxContent[icon.id].title +
  155. '</h3>' +
  156. '<p>' +
  157. self.infoBoxContent[icon.id].body +
  158. '</p>' +
  159. self.infoBoxContent[icon.id].img;
  160. document.querySelector('.ifr-modal__infobox').innerHTML = msg;
  161. },
  162. /**
  163. * Saves info from selected game and goes to next state
  164. *
  165. * @param {object} icon clicked icon
  166. */
  167. load: function (icon) {
  168. if (audioStatus) game.audio.popSound.play();
  169. switch (icon.iconType) {
  170. case 'infoIcon':
  171. self.showInfoBox(icon);
  172. break;
  173. case 'game':
  174. gameShape = icon.gameShape;
  175. gameName = icon.gameName;
  176. if (!metadata.gameNames.includes(gameName))
  177. console.error('Game error: the name of the game is not valid.');
  178. self.menuIcons = self.lbl_game.name;
  179. game.state.start('customMenu');
  180. break;
  181. }
  182. },
  183. /**
  184. * Display the name of the game on screen
  185. *
  186. * @param {object} icon icon for the game
  187. */
  188. showTitle: function (icon) {
  189. const number = icon.gameName.slice(-3) == 'One' ? 'I' : 'II';
  190. const shape = icon.gameName.slice(0, -3);
  191. self.lbl_game.name = game.lang[shape] + ' ' + number;
  192. },
  193. /**
  194. * Remove the name of the game from screen
  195. */
  196. clearTitle: function () {
  197. self.lbl_game.name = '';
  198. },
  199. /**
  200. * Called by mouse click event
  201. *
  202. * @param {object} mouseEvent contains the mouse click coordinates
  203. */
  204. onInputDown: function (mouseEvent) {
  205. const x = game.math.getMouse(mouseEvent).x;
  206. const y = game.math.getMouse(mouseEvent).y;
  207. // Check menu icons
  208. for (let i in self.menuIcons) {
  209. // If mouse is within the bounds of an icon
  210. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  211. // Click first valid icon
  212. self.load(self.menuIcons[i]);
  213. break;
  214. }
  215. }
  216. // Check navigation icons
  217. navigationIcons.onInputDown(x, y);
  218. game.render.all();
  219. },
  220. /**
  221. * Called by mouse move event
  222. *
  223. * @param {object} mouseEvent contains the mouse move coordinates
  224. */
  225. onInputOver: function (mouseEvent) {
  226. const x = game.math.getMouse(mouseEvent).x;
  227. const y = game.math.getMouse(mouseEvent).y;
  228. let overIcon;
  229. // Check menu icons
  230. for (let i in self.menuIcons) {
  231. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  232. overIcon = i;
  233. break;
  234. }
  235. }
  236. // Update gui
  237. if (overIcon) {
  238. // If pointer is over icon
  239. document.body.style.cursor = 'pointer';
  240. if (self.menuIcons[overIcon].iconType == 'game')
  241. self.showTitle(self.menuIcons[overIcon]);
  242. self.menuIcons.forEach((cur) => {
  243. if (cur.iconType == self.menuIcons[overIcon].iconType) {
  244. // If its in the same icon category
  245. if (cur == self.menuIcons[overIcon]) {
  246. // If its the icon the pointer is over
  247. cur.scale = cur.originalScale * 1.1;
  248. } else {
  249. cur.scale = cur.originalScale;
  250. }
  251. }
  252. });
  253. } else {
  254. // If pointer is not over icon
  255. self.clearTitle();
  256. self.menuIcons.forEach((cur) => {
  257. cur.scale = cur.originalScale;
  258. });
  259. document.body.style.cursor = 'auto';
  260. }
  261. // Check navigation icons
  262. navigationIcons.onInputOver(x, y);
  263. game.render.all();
  264. },
  265. };