menu.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * Preloads media for current state
  11. */
  12. preload: function () {
  13. // LOADING MEDIA
  14. game.load.image(url.menu.image);
  15. game.load.sprite(url.menu.sprite);
  16. },
  17. /**
  18. * Main code
  19. */
  20. create: function () {
  21. // FOR MOODLE
  22. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'false') { // Student role
  23. playerName = game.lang.student; // TODO pegar o nome do aluno no bd do moodle
  24. getiLMContent();
  25. } else {
  26. // FOR MOODLE
  27. if (moodle && iLMparameters.iLM_PARAM_SendAnswer == 'true') playerName = game.lang.professor;
  28. // Background color
  29. game.add.geom.rect(0, 0, defaultWidth, defaultHeight, undefined, 0, colors.blueBckg, 1);
  30. // Floor
  31. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, defaultHeight - 100, 'floor'); }
  32. // Overtitle: Welcome, <player name>!
  33. game.add.text(defaultWidth / 2, 40, game.lang.welcome + ', ' + playerName + '!', textStyles.h4_brown);
  34. // Title : Select a game
  35. game.add.text(defaultWidth / 2, 80, game.lang.menu_title, textStyles.h1_green);
  36. // Subtitle : <game mode>
  37. this.lbl_game = game.add.text(defaultWidth / 2, 110, '', textStyles.h2_blue_2);
  38. // Loads navigation icons
  39. navigationIcons.add(
  40. false, false, false,
  41. true, true,
  42. false, false);
  43. // INFO ICONS
  44. this.menuIcons = [];
  45. let infoIcon;
  46. // --------------------------- GAME ICONS
  47. const offset = defaultWidth / (info.gameType.length + 1);
  48. for (let i = 0, x = offset; i < info.gameType.length; i++, x += offset) {
  49. const icon = game.add.image(x, defaultHeight / 2 - 70, info.gameTypeUrl[i], 1);
  50. icon.anchor(0.5, 0.5);
  51. icon.gameShape = info.gameShape[i];
  52. icon.gameType = info.gameType[i];
  53. icon.iconType = 'game';
  54. this.menuIcons.push(icon);
  55. // "more information" button
  56. infoIcon = game.add.image(x + 70, defaultHeight / 2 - 70 - 80, 'info', 0.6, 0.4);
  57. infoIcon.anchor(0.5, 0.5);
  58. infoIcon.iconType = 'infoIcon';
  59. infoIcon.id = icon.gameType;
  60. this.menuIcons.push(infoIcon);
  61. }
  62. // --------------------------- INFO BOX
  63. this.infoBox = document.getElementById('myModal');
  64. // When the user clicks on the 'x', close the modal
  65. document.getElementsByClassName('close')[0].onclick = function () {
  66. self.infoBox.style.display = 'none';
  67. }
  68. // When the user clicks anywhere outside of the modal, close it
  69. window.onclick = function (event) {
  70. if (event.target == self.infoBox) {
  71. self.infoBox.style.display = 'none';
  72. }
  73. }
  74. this.infoBoxContent = {
  75. squareOne: {
  76. title: '<b>' + game.lang.game + ':</b> ' + game.lang.square + ' I',
  77. body: '<ul>' + game.lang.infoBox_squareOne + '</ul>',
  78. img: '<center> <img width=60% src="' + game.image['s1-A'].src + '"./assets/img/info_box/s1-A.png"> </center>'
  79. },
  80. circleOne: {
  81. title: '<b>' + game.lang.game + ':</b> ' + game.lang.circle + ' I',
  82. body: '<ul>' + game.lang.infoBox_circleOne + '</ul>',
  83. img: '<center> <img width=80% src="' + game.image['c1-A'].src + '"> </center>',
  84. },
  85. squareTwo: {
  86. title: '<b>' + game.lang.game + ':</b> ' + game.lang.square + ' II',
  87. body: '<ul>' + game.lang.infoBox_squareTwo + '</ul>',
  88. img: '<center> <img width=80% src="' + game.image['s2'].src + '"> </center>',
  89. }
  90. };
  91. // ------------- EVENTS
  92. game.event.add('click', this.onInputDown);
  93. game.event.add('mousemove', this.onInputOver);
  94. }
  95. },
  96. /**
  97. * Displays game menu information boxes.
  98. */
  99. showInfoBox: function (icon) {
  100. self.infoBox.style.display = 'block';
  101. let msg = '<h3>' + self.infoBoxContent[icon.id].title + '</h3>'
  102. + '<p>' + self.infoBoxContent[icon.id].body + '</p>'
  103. + self.infoBoxContent[icon.id].img;
  104. document.getElementById('infobox-content').innerHTML = msg;
  105. },
  106. /**
  107. * Saves info from selected game and goes to next state
  108. *
  109. * @param {object} icon clicked icon
  110. */
  111. load: function (icon) {
  112. if (audioStatus) game.audio.beepSound.play();
  113. switch (icon.iconType) {
  114. case 'infoIcon': self.showInfoBox(icon); break;
  115. case 'game':
  116. gameShape = icon.gameShape;
  117. gameTypeString = icon.gameType;
  118. switch (gameTypeString) {
  119. case 'squareOne': gameType = squareOne; break;
  120. case 'squareTwo': gameType = squareTwo; break;
  121. case 'circleOne': gameType = circleOne; break;
  122. default: console.error('Game error: the name of the game is not valid.');
  123. }
  124. self.menuIcons = self.lbl_game.name;
  125. game.state.start('customMenu');
  126. break;
  127. }
  128. },
  129. /**
  130. * Display the name of the game on screen
  131. *
  132. * @param {object} icon icon for the game
  133. */
  134. showTitle: function (icon) {
  135. const number = (icon.gameType.slice(-3) == 'One') ? 'I' : 'II';
  136. self.lbl_game.name = game.lang[icon.gameShape] + ' ' + number;
  137. },
  138. /**
  139. * Remove the name of the game from screen
  140. */
  141. clearTitle: function () {
  142. self.lbl_game.name = '';
  143. },
  144. /**
  145. * Called by mouse click event
  146. *
  147. * @param {object} mouseEvent contains the mouse click coordinates
  148. */
  149. onInputDown: function (mouseEvent) {
  150. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  151. // Check menu icons
  152. for (let i in self.menuIcons) {
  153. // If mouse is within the bounds of an icon
  154. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  155. // Click first valid icon
  156. self.load(self.menuIcons[i]);
  157. break;
  158. }
  159. }
  160. // Check navigation icons
  161. navigationIcons.onInputDown(x, y);
  162. game.render.all();
  163. },
  164. /**
  165. * Called by mouse move event
  166. *
  167. * @param {object} mouseEvent contains the mouse move coordinates
  168. */
  169. onInputOver: function (mouseEvent) {
  170. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  171. let overIcon;
  172. // Check menu icons
  173. for (let i in self.menuIcons) {
  174. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  175. overIcon = i;
  176. break;
  177. }
  178. }
  179. // Update gui
  180. if (overIcon) { // If pointer is over icon
  181. document.body.style.cursor = 'pointer';
  182. if (self.menuIcons[overIcon].iconType == 'game') self.showTitle(self.menuIcons[overIcon]);
  183. self.menuIcons.forEach(cur => {
  184. if (cur.iconType == self.menuIcons[overIcon].iconType) { // If its in the same icon category
  185. if (cur == self.menuIcons[overIcon]) { // If its the icon the pointer is over
  186. cur.scale = cur.originalScale * 1.1;
  187. } else {
  188. cur.scale = cur.originalScale;
  189. }
  190. }
  191. });
  192. } else { // If pointer is not over icon
  193. self.clearTitle();
  194. self.menuIcons.forEach(cur => { cur.scale = cur.originalScale; });
  195. document.body.style.cursor = 'auto';
  196. }
  197. // Check navigation icons
  198. navigationIcons.onInputOver(x, y);
  199. game.render.all();
  200. }
  201. };