preMenu.js 8.6 KB

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