preMenu.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. }
  93. else {
  94. cur.scale = cur.scale = 1;
  95. }
  96. });
  97. if (flag) document.body.style.cursor = 'pointer';
  98. else document.body.style.cursor = 'auto';
  99. game.render.all();
  100. },
  101. /* GAME FUNCTIONS */
  102. /**
  103. * Calls state that loads selected language
  104. *
  105. * @param {string} selectedLang language selected by player
  106. */
  107. func_setLang: function (selectedLang) {
  108. // Saves language name e.g 'pt_BR'
  109. langString = selectedLang;
  110. // Calls loading screen
  111. game.state.start('loadLang');
  112. }
  113. };
  114. /**
  115. * LOADING LANGUAGE STATE: Loads selected language to be able to translate the game text
  116. *
  117. * @namespace
  118. */
  119. const loadLangState = {
  120. /**
  121. * Preloads media for current state
  122. */
  123. preload: function () {
  124. // LOADING MEDIA : selected language
  125. game.load.lang('assets/lang/' + langString);
  126. },
  127. /**
  128. * Main code
  129. */
  130. create: function () {
  131. if (debugMode) console.log('Language: ' + langString);
  132. // Make sure to only ask for player name on the first time oppening the game
  133. if (this.firstTime == undefined) {
  134. this.firstTime = false;
  135. game.state.start('name'); // First time opening ifractions ('language' >> 'name' >> 'menu')
  136. } else {
  137. game.state.start('menu'); // If changing language during the game ('language' >> >> 'menu')
  138. }
  139. }
  140. };
  141. /**
  142. * NAME STATE: asks for player's name
  143. *
  144. * @namespace
  145. */
  146. const nameState = {
  147. /**
  148. * Main code
  149. */
  150. create: function () {
  151. // Background color
  152. game.add.graphic.rect(0, 0, 900, 600, colors.white, 0, colors.blueBckg, 1);
  153. // Set title and warning text
  154. game.add.text(defaultWidth / 2, defaultHeight / 2 - 100, game.lang.insert_name, textStyles.h1_green);
  155. this.warningEmptyName = game.add.text(defaultWidth / 2, defaultHeight / 2 - 70, '', textStyles.h4_brown);
  156. // Set 'ok' button that gets player's information
  157. this.okBtn = game.add.graphic.rect(defaultWidth / 2 - 84, defaultHeight / 2 + 70, 168, 60, undefined, 0, colors.gray, 0.6);
  158. // Set button Text
  159. game.add.text(defaultWidth / 2 + 1, defaultHeight / 2 + 112, game.lang.ready, textStyles.h1_white);
  160. // Makes text field visible
  161. document.getElementById('textbox').style.visibility = 'visible';
  162. // Does the same as the button click when the player presses 'enter'
  163. document.getElementById('textbox-content').addEventListener('keypress', function (e) {
  164. const keycode = e.key || e.code;
  165. if (keycode == 'Enter') {
  166. if (self.func_checkEmptyName()) self.func_saveName();
  167. game.render.all(); // Can show empty name
  168. }
  169. });
  170. game.event.add('click', this.func_onInputDown);
  171. game.event.add('mousemove', this.func_onInputOver);
  172. },
  173. /* EVENT HANDLER*/
  174. /**
  175. * Called by mouse click event
  176. *
  177. * @param {object} mouseEvent contains the mouse click coordinates
  178. */
  179. func_onInputDown: function (mouseEvent) {
  180. const x = mouseEvent.offsetX;
  181. const y = mouseEvent.offsetY;
  182. const cur = self.okBtn;
  183. if (game.math.isOverIcon(x, y, cur)) {
  184. if (self.func_checkEmptyName()) {
  185. self.func_saveName();
  186. }
  187. }
  188. game.render.all();
  189. },
  190. /**
  191. * Called by mouse move event
  192. *
  193. * @param {object} mouseEvent contains the mouse move coordinates
  194. */
  195. func_onInputOver: function (mouseEvent) {
  196. const x = mouseEvent.offsetX;
  197. const y = mouseEvent.offsetY;
  198. const cur = self.okBtn;
  199. if (game.math.isOverIcon(x, y, cur)) {
  200. document.body.style.cursor = 'pointer';
  201. cur.alpha = 0.4;
  202. }
  203. else {
  204. document.body.style.cursor = 'auto';
  205. cur.alpha = 0.6;
  206. }
  207. game.render.all();
  208. },
  209. /* GAME FUNCTIONS */
  210. /**
  211. * Checks if player entered name in text box
  212. *
  213. * @returns {boolean}
  214. */
  215. func_checkEmptyName: function () {
  216. // If text field is empty displays error message
  217. if (document.getElementById('textbox-content').value == '') {
  218. self.warningEmptyName.name = game.lang.empty_name;
  219. return false;
  220. }
  221. return true;
  222. },
  223. /**
  224. * Saves player name and calls next state
  225. */
  226. func_saveName: function () {
  227. // Saves player's input in global variable 'playerName'
  228. playerName = document.getElementById('textbox-content').value;
  229. // Hides and clears text field
  230. document.getElementById('textbox').style.visibility = 'hidden';
  231. document.getElementById('textbox-content').value = '';
  232. if (audioStatus) game.audio.beepSound.play();
  233. if (debugMode) console.log('Username: ' + playerName);
  234. // Calls 'menu' state
  235. game.state.start('menu');
  236. }
  237. };