preMenu.js 7.7 KB

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