menu.js 7.5 KB

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