preMenu.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.h2_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. },
  112. /**
  113. * Calls state that loads selected language
  114. *
  115. * @param {string} selectedLang language selected by player
  116. */
  117. setLang: function (selectedLang) {
  118. // Saves language name e.g 'pt_BR'
  119. langString = selectedLang;
  120. if (audioStatus) game.audio.popSound.play();
  121. // Calls loading screen
  122. game.state.start('loadLang');
  123. },
  124. /**
  125. * Called by mouse click event
  126. *
  127. * @param {object} mouseEvent contains the mouse click coordinates
  128. */
  129. onInputDown: function (mouseEvent) {
  130. const x = game.math.getMouse(mouseEvent).x;
  131. const y = game.math.getMouse(mouseEvent).y;
  132. self.listOfFlags.forEach((cur) => {
  133. if (game.math.isOverIcon(x, y, cur)) {
  134. for (let i in self.langs.flag) {
  135. if (self.langs.flag[i] == cur.name) {
  136. self.setLang(self.langs.lang[i]);
  137. break;
  138. }
  139. }
  140. }
  141. });
  142. },
  143. /**
  144. * Called by mouse move event
  145. *
  146. * @param {object} mouseEvent contains the mouse move coordinates
  147. */
  148. onInputOver: function (mouseEvent) {
  149. const x = game.math.getMouse(mouseEvent).x;
  150. const y = game.math.getMouse(mouseEvent).y;
  151. let flag = false;
  152. self.listOfFlags.forEach((cur) => {
  153. if (game.math.isOverIcon(x, y, cur)) {
  154. flag = true;
  155. cur.scale = cur.scale = 1.05;
  156. } else {
  157. cur.scale = cur.scale = 1;
  158. }
  159. });
  160. if (flag) document.body.style.cursor = 'pointer';
  161. else document.body.style.cursor = 'auto';
  162. game.render.all();
  163. },
  164. };
  165. /** [LOADING LANGUAGE STATE] Loads the selected language.
  166. *
  167. * @namespace
  168. */
  169. const loadLangState = {
  170. /**
  171. * Preloads media for current state
  172. */
  173. preload: function () {
  174. // LOADING MEDIA : selected language
  175. game.load.lang('assets/lang/' + langString);
  176. },
  177. /**
  178. * Main code
  179. */
  180. create: function () {
  181. if (debugMode) console.log('Language: ' + langString);
  182. // Make sure to only ask for player name on the first time oppening the game
  183. if (this.firstTime == undefined) {
  184. this.firstTime = false;
  185. game.state.start('name'); // First time opening ifractions ('language' >> 'name' >> 'menu')
  186. } else {
  187. game.state.start('menu'); // If changing language during the game ('language' >> >> 'menu')
  188. }
  189. },
  190. };
  191. /** [NAME STATE] Screen that asks for the user's name.
  192. *
  193. * @namespace
  194. */
  195. const nameState = {
  196. /**
  197. * Main code
  198. */
  199. create: function () {
  200. // Background color
  201. game.add.geom.rect(
  202. 0,
  203. 0,
  204. context.canvas.width,
  205. context.canvas.height,
  206. colors.white,
  207. 0,
  208. colors.blueBg,
  209. 1
  210. );
  211. // Set title and warning text
  212. game.add.text(
  213. context.canvas.width / 2,
  214. context.canvas.height / 2 - 100,
  215. game.lang.insert_name,
  216. textStyles.h1_green
  217. );
  218. this.warningEmptyName = game.add.text(
  219. context.canvas.width / 2,
  220. context.canvas.height / 2 - 70,
  221. '',
  222. textStyles.h4_brown
  223. );
  224. // Set 'ok' button that gets player's information
  225. this.okBtn = game.add.geom.rect(
  226. context.canvas.width / 2 - 84,
  227. context.canvas.height / 2 + 70,
  228. 168,
  229. 60,
  230. undefined,
  231. 0,
  232. colors.gray,
  233. 0.6
  234. );
  235. // Set button Text
  236. game.add.text(
  237. context.canvas.width / 2 + 1,
  238. context.canvas.height / 2 + 112,
  239. game.lang.ready,
  240. textStyles.h1_white
  241. );
  242. // Makes text field visible
  243. document.getElementById('textbox').style.visibility = 'visible';
  244. // Does the same as the button click when the player presses 'enter'
  245. document
  246. .getElementById('textbox-content')
  247. .addEventListener('keypress', function (e) {
  248. const keycode = e.key || e.code;
  249. if (keycode == 'Enter') {
  250. if (self.checkEmptyName()) self.saveName();
  251. game.render.all(); // Can show empty name
  252. }
  253. });
  254. game.event.add('click', this.onInputDown);
  255. game.event.add('mousemove', this.onInputOver);
  256. },
  257. /**
  258. * Checks if player entered name in text box
  259. *
  260. * @returns {boolean} false is textBox is emptys
  261. */
  262. checkEmptyName: function () {
  263. // If text field is empty displays error message
  264. if (document.getElementById('textbox-content').value == '') {
  265. self.warningEmptyName.name = game.lang.empty_name;
  266. return false;
  267. }
  268. return true;
  269. },
  270. /**
  271. * Saves player name and calls next state
  272. */
  273. saveName: function () {
  274. // Saves player's input in global variable 'playerName'
  275. playerName = document.getElementById('textbox-content').value;
  276. // Hides and clears text field
  277. document.getElementById('textbox').style.visibility = 'hidden';
  278. document.getElementById('textbox-content').value = '';
  279. if (audioStatus) game.audio.popSound.play();
  280. if (debugMode) console.log('Username: ' + playerName);
  281. // FOR MOODLE
  282. // Calls 'menu' state
  283. if (!moodle) game.state.start('menu');
  284. },
  285. /**
  286. * Called by mouse click event
  287. *
  288. * @param {object} mouseEvent contains the mouse click coordinates
  289. */
  290. onInputDown: function (mouseEvent) {
  291. const x = game.math.getMouse(mouseEvent).x;
  292. const y = game.math.getMouse(mouseEvent).y;
  293. const cur = self.okBtn;
  294. if (game.math.isOverIcon(x, y, cur)) {
  295. if (self.checkEmptyName()) {
  296. self.saveName();
  297. }
  298. }
  299. game.render.all();
  300. },
  301. /**
  302. * Called by mouse move event
  303. *
  304. * @param {object} mouseEvent contains the mouse move coordinates
  305. */
  306. onInputOver: function (mouseEvent) {
  307. const x = game.math.getMouse(mouseEvent).x;
  308. const y = game.math.getMouse(mouseEvent).y;
  309. const cur = self.okBtn;
  310. if (game.math.isOverIcon(x, y, cur)) {
  311. document.body.style.cursor = 'pointer';
  312. cur.alpha = 0.4;
  313. } else {
  314. document.body.style.cursor = 'auto';
  315. cur.alpha = 0.6;
  316. }
  317. game.render.all();
  318. },
  319. };