preMenu.js 8.3 KB

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