menu.js 7.9 KB

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