preMenu.js 7.7 KB

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