preMenu.js 7.4 KB

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