preMenu.js 7.7 KB

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