preMenu.js 8.0 KB

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