preMenu.js 7.2 KB

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