preMenu_lang.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [LANGUAGE STATE] Screen that asks the user to select the language for the game text.
  5. *
  6. * @namespace
  7. */
  8. const langState = {
  9. /**
  10. * Main code
  11. */
  12. create: function () {
  13. renderBackground('plain');
  14. // Parameters for the elements on the screen
  15. this.listOfFlags = [];
  16. this.langs = {
  17. text: [
  18. 'PORTUGUÊS ',
  19. 'ESPAÑOL ',
  20. 'FRANÇAIS ',
  21. 'ENGLISH ',
  22. 'ITALIANO ',
  23. ], // Language names
  24. flag: ['flag_BR', 'flag_ES', 'flag_FR', 'flag_US', 'flag_IT'], // Icon names
  25. lang: ['pt_BR', 'es_ES', 'fr_FR', 'en_US', 'it_IT'], // Parameters sent for language object
  26. x: [-350, -350, -350, 250, 250],
  27. y: [-220, 0, 220, -110, 110],
  28. };
  29. // Create elements on screen
  30. for (let i in this.langs.flag) {
  31. // Add text for language names
  32. game.add.text(
  33. context.canvas.width / 2 + this.langs.x[i],
  34. context.canvas.height / 2 + this.langs.y[i],
  35. this.langs.text[i],
  36. { ...textStyles.h2_, fill: colors.green }
  37. ).align = 'right';
  38. // Add icons for flags
  39. const flag = game.add.image(
  40. context.canvas.width / 2 + this.langs.x[i] + 100,
  41. context.canvas.height / 2 + this.langs.y[i] - 13,
  42. this.langs.flag[i]
  43. );
  44. flag.anchor(0.5, 0.5);
  45. this.listOfFlags.push(flag);
  46. }
  47. game.event.add('click', this.onInputDown);
  48. game.event.add('mousemove', this.onInputOver);
  49. if (isDebugMode && debugState.lang.skip) {
  50. // programmatically select a language
  51. this.setLang(debugState.lang.lang || 'pt_BR');
  52. }
  53. },
  54. /**
  55. * Calls state that loads selected language
  56. *
  57. * @param {string} selectedLang language selected by player
  58. */
  59. setLang: function (selectedLang) {
  60. // Saves language name e.g 'pt_BR'
  61. langString = selectedLang;
  62. if (audioStatus) game.audio.popSound.play();
  63. // Calls loading screen
  64. game.state.start('loadLang');
  65. },
  66. /**
  67. * Called by mouse click event
  68. *
  69. * @param {object} mouseEvent contains the mouse click coordinates
  70. */
  71. onInputDown: function (mouseEvent) {
  72. const x = game.math.getMouse(mouseEvent).x;
  73. const y = game.math.getMouse(mouseEvent).y;
  74. self.listOfFlags.forEach((cur) => {
  75. if (game.math.isOverIcon(x, y, cur)) {
  76. for (let i in self.langs.flag) {
  77. if (self.langs.flag[i] == cur.name) {
  78. self.setLang(self.langs.lang[i]);
  79. break;
  80. }
  81. }
  82. }
  83. });
  84. },
  85. /**
  86. * Called by mouse move event
  87. *
  88. * @param {object} mouseEvent contains the mouse move coordinates
  89. */
  90. onInputOver: function (mouseEvent) {
  91. const x = game.math.getMouse(mouseEvent).x;
  92. const y = game.math.getMouse(mouseEvent).y;
  93. let flag = false;
  94. self.listOfFlags.forEach((cur) => {
  95. if (game.math.isOverIcon(x, y, cur)) {
  96. flag = true;
  97. cur.scale = cur.scale = 1.05;
  98. } else {
  99. cur.scale = cur.scale = 1;
  100. }
  101. });
  102. if (flag) document.body.style.cursor = 'pointer';
  103. else document.body.style.cursor = 'auto';
  104. game.render.all();
  105. },
  106. };
  107. /** [LOADING LANGUAGE STATE] Loads the selected language.
  108. *
  109. * @namespace
  110. */
  111. const loadLangState = {
  112. /**
  113. * Preloads media for current state
  114. */
  115. preload: function () {
  116. // LOADING MEDIA : selected language
  117. game.load.lang('./assets/lang/' + langString);
  118. },
  119. /**
  120. * Main code
  121. */
  122. create: function () {
  123. if (isDebugMode) console.log('Language: ' + langString);
  124. // Make sure to only ask for player name on the first time oppening the game
  125. if (this.firstTime == undefined) {
  126. this.firstTime = false;
  127. game.state.start('name'); // First time opening ifractions ('lang' >> 'name' >> 'menu')
  128. } else {
  129. game.state.start('menu'); // If changing language during the game ('lang' >> >> 'menu')
  130. }
  131. },
  132. };