1
0

preMenu.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [BOOT STATE] First state called. Loads media. <br>
  5. *
  6. * @namespace
  7. */
  8. const bootState = {
  9. /**
  10. * Preloads media for current state
  11. */
  12. preload: function () {
  13. // LOADING MEDIA
  14. game.load.audio(url.boot.audio);
  15. game.load.image(url.boot.image);
  16. game.load.sprite(url.boot.sprite);
  17. },
  18. /**
  19. * Main code
  20. */
  21. create: function () {
  22. // Calls first screen seen by the player
  23. // FOR MOODLE
  24. if (moodle) {
  25. loadLangState.firstTime = false;
  26. const moodleLang = iLMparameters.lang;
  27. switch (moodleLang) {
  28. case 'en': langString = 'en_US'; break;
  29. case 'pt': langString = 'pt_BR'; break;
  30. case 'fr': langString = 'fr_FR'; break;
  31. case 'es': langString = 'es_PE'; break;
  32. case 'it': langString = 'it_IT'; break;
  33. default: langString = 'en_US';
  34. }
  35. game.state.start('loadLang');
  36. } else {
  37. game.state.start('lang');
  38. }
  39. }
  40. };
  41. /** [LANGUAGE STATE] Screen that asks the user to select the language for the game text.
  42. *
  43. * @namespace
  44. */
  45. const langState = {
  46. /**
  47. * Main code
  48. */
  49. create: function () {
  50. // Background color
  51. game.add.geom.rect(0, 0, defaultWidth, defaultHeight, colors.white, 0, colors.blueBckg, 1);
  52. // Parameters for the elements on the screen
  53. this.listOfFlags = [];
  54. this.langs = {
  55. text: ['FRAÇÕES ', 'FRAZIONI ', 'FRACTIONS ', 'FRACCIONES ', 'FRACTIONS '], // Language names
  56. flag: ['flag_BR', 'flag_IT', 'flag_US', 'flag_PE', 'flag_FR'], // Icon names
  57. lang: ['pt_BR', 'it_IT', 'en_US', 'es_PE', 'fr_FR'], // Parameters sent for language object
  58. x: [-220, -220, -220, 200, 200],
  59. y: [-180, 0, 180, -100, 100]
  60. };
  61. // Create elements on screen
  62. for (let i in this.langs.flag) {
  63. // Add text for language names
  64. game.add.text(defaultWidth / 2 + this.langs.x[i], defaultHeight / 2 + this.langs.y[i], this.langs.text[i], textStyles.h2_green).align = 'right';
  65. // Add icons for flags
  66. const flag = game.add.image(defaultWidth / 2 + this.langs.x[i] + 100, defaultHeight / 2 + this.langs.y[i], this.langs.flag[i]);
  67. flag.anchor(0.5, 0.5);
  68. this.listOfFlags.push(flag);
  69. }
  70. game.event.add('click', this.onInputDown);
  71. game.event.add('mousemove', this.onInputOver);
  72. },
  73. /**
  74. * Calls state that loads selected language
  75. *
  76. * @param {string} selectedLang language selected by player
  77. */
  78. setLang: function (selectedLang) {
  79. // Saves language name e.g 'pt_BR'
  80. langString = selectedLang;
  81. // Calls loading screen
  82. game.state.start('loadLang');
  83. },
  84. /**
  85. * Called by mouse click event
  86. *
  87. * @param {object} mouseEvent contains the mouse click coordinates
  88. */
  89. onInputDown: function (mouseEvent) {
  90. const x = mouseEvent.offsetX;
  91. const y = mouseEvent.offsetY;
  92. self.listOfFlags.forEach(cur => {
  93. if (game.math.isOverIcon(x, y, cur)) {
  94. for (let i in self.langs.flag) {
  95. if (self.langs.flag[i] == cur.name) {
  96. self.setLang(self.langs.lang[i]);
  97. break;
  98. }
  99. }
  100. }
  101. });
  102. },
  103. /**
  104. * Called by mouse move event
  105. *
  106. * @param {object} mouseEvent contains the mouse move coordinates
  107. */
  108. onInputOver: function (mouseEvent) {
  109. const x = mouseEvent.offsetX;
  110. const y = mouseEvent.offsetY;
  111. let flag = false;
  112. self.listOfFlags.forEach(cur => {
  113. if (game.math.isOverIcon(x, y, cur)) {
  114. flag = true;
  115. cur.scale = cur.scale = 1.05;
  116. } else {
  117. cur.scale = cur.scale = 1;
  118. }
  119. });
  120. if (flag) document.body.style.cursor = 'pointer';
  121. else document.body.style.cursor = 'auto';
  122. game.render.all();
  123. }
  124. };
  125. /** [LOADING LANGUAGE STATE] Loads the selected language.
  126. *
  127. * @namespace
  128. */
  129. const loadLangState = {
  130. /**
  131. * Preloads media for current state
  132. */
  133. preload: function () {
  134. // LOADING MEDIA : selected language
  135. game.load.lang('assets/lang/' + langString);
  136. },
  137. /**
  138. * Main code
  139. */
  140. create: function () {
  141. if (debugMode) console.log('Language: ' + langString);
  142. // Make sure to only ask for player name on the first time oppening the game
  143. if (this.firstTime == undefined) {
  144. this.firstTime = false;
  145. game.state.start('name'); // First time opening ifractions ('language' >> 'name' >> 'menu')
  146. } else {
  147. game.state.start('menu'); // If changing language during the game ('language' >> >> 'menu')
  148. }
  149. }
  150. };
  151. /** [NAME STATE] Screen that asks for the user's name.
  152. *
  153. * @namespace
  154. */
  155. const nameState = {
  156. /**
  157. * Main code
  158. */
  159. create: function () {
  160. // Background color
  161. game.add.geom.rect(0, 0, defaultWidth, defaultHeight, colors.white, 0, colors.blueBckg, 1);
  162. // Set title and warning text
  163. game.add.text(defaultWidth / 2, defaultHeight / 2 - 100, game.lang.insert_name, textStyles.h1_green);
  164. this.warningEmptyName = game.add.text(defaultWidth / 2, defaultHeight / 2 - 70, '', textStyles.h4_brown);
  165. // Set 'ok' button that gets player's information
  166. this.okBtn = game.add.geom.rect(defaultWidth / 2 - 84, defaultHeight / 2 + 70, 168, 60, undefined, 0, colors.gray, 0.6);
  167. // Set button Text
  168. game.add.text(defaultWidth / 2 + 1, defaultHeight / 2 + 112, game.lang.ready, textStyles.h1_white);
  169. // Makes text field visible
  170. document.getElementById('textbox').style.visibility = 'visible';
  171. // Does the same as the button click when the player presses 'enter'
  172. document.getElementById('textbox-content').addEventListener('keypress', function (e) {
  173. const keycode = e.key || e.code;
  174. if (keycode == 'Enter') {
  175. if (self.checkEmptyName()) self.saveName();
  176. game.render.all(); // Can show empty name
  177. }
  178. });
  179. game.event.add('click', this.onInputDown);
  180. game.event.add('mousemove', this.onInputOver);
  181. },
  182. /**
  183. * Checks if player entered name in text box
  184. *
  185. * @returns {boolean} false is textBox is emptys
  186. */
  187. checkEmptyName: function () {
  188. // If text field is empty displays error message
  189. if (document.getElementById('textbox-content').value == '') {
  190. self.warningEmptyName.name = game.lang.empty_name;
  191. return false;
  192. }
  193. return true;
  194. },
  195. /**
  196. * Saves player name and calls next state
  197. */
  198. saveName: function () {
  199. // Saves player's input in global variable 'playerName'
  200. playerName = document.getElementById('textbox-content').value;
  201. // Hides and clears text field
  202. document.getElementById('textbox').style.visibility = 'hidden';
  203. document.getElementById('textbox-content').value = '';
  204. if (audioStatus) game.audio.beepSound.play();
  205. if (debugMode) console.log('Username: ' + playerName);
  206. // FOR MOODLE
  207. // Calls 'menu' state
  208. if (!moodle) game.state.start('menu');
  209. },
  210. /**
  211. * Called by mouse click event
  212. *
  213. * @param {object} mouseEvent contains the mouse click coordinates
  214. */
  215. onInputDown: function (mouseEvent) {
  216. const x = mouseEvent.offsetX;
  217. const y = mouseEvent.offsetY;
  218. const cur = self.okBtn;
  219. if (game.math.isOverIcon(x, y, cur)) {
  220. if (self.checkEmptyName()) {
  221. self.saveName();
  222. }
  223. }
  224. game.render.all();
  225. },
  226. /**
  227. * Called by mouse move event
  228. *
  229. * @param {object} mouseEvent contains the mouse move coordinates
  230. */
  231. onInputOver: function (mouseEvent) {
  232. const x = mouseEvent.offsetX;
  233. const y = mouseEvent.offsetY;
  234. const cur = self.okBtn;
  235. if (game.math.isOverIcon(x, y, cur)) {
  236. document.body.style.cursor = 'pointer';
  237. cur.alpha = 0.4;
  238. } else {
  239. document.body.style.cursor = 'auto';
  240. cur.alpha = 0.6;
  241. }
  242. game.render.all();
  243. }
  244. };