preMenu.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. let langState = {
  3. create: function(){},
  4. -------------------------------------- end of phaser functions
  5. func_setLang: function(){} //calls loadState
  6. };
  7. let loadState = {
  8. preload: function(){},
  9. create: function(){} //calls nameState
  10. -------------------------------------- end of phaser functions
  11. };
  12. let nameState = {
  13. create: function(){},
  14. -------------------------------------- end of phaser functions
  15. func_checkEmptyName: function(){}
  16. func_saveName: function(){}
  17. };
  18. */
  19. // LANGUAGE SCREEN: the player can choose a preferred language for the game text to be displayed
  20. let langState = {
  21. create: function () {
  22. // Sets stage width back to default (if we are back from 'menu' state where it can be changed)
  23. if (this.game.world.width != defaultWidth) this.game.world.setBounds(0, 0, defaultWidth, this.game.world.height);
  24. // Parameters for the elements on the screen
  25. const langs = {
  26. text: ['FRAÇÕES ', 'FRAZIONI ', 'FRACTIONS ', 'FRACCIONES ', 'FRACTIONS '], // Language names
  27. flag: ['flag_BR', 'flag_IT', 'flag_US', 'flag_PE', 'flag_FR'], // Icon names
  28. lang: ['pt_BR', 'it_IT', 'en_US', 'es_PE', 'fr_FR'], // Parameters sent for language object
  29. x: [-220, -220, -220, 200, 200],
  30. y: [-180, 0, 180, -100, 100]
  31. }
  32. // Create elements on screen
  33. for (let i = 0, length = langs.lang.length; i < length; i++) {
  34. // Add text for language names
  35. const language = game.add.text(this.game.world.centerX + langs.x[i], this.game.world.centerY + langs.y[i], langs.text[i], textStyles.title2);
  36. language.anchor.setTo(1, 0.5);
  37. // Add icons for flags
  38. const flag = game.add.sprite(this.game.world.centerX + langs.x[i] + 100, this.game.world.centerY + langs.y[i], langs.flag[i]);
  39. flag.anchor.setTo(0.5, 0.5);
  40. flag.inputEnabled = true;
  41. flag.input.useHandCursor = true;
  42. flag.events.onInputDown.add(this.func_setLang, { langs_lang: langs.lang[i] });
  43. flag.events.onInputOver.add(function () { this.flag.scale.setTo(1.05) }, { flag: flag });
  44. flag.events.onInputOut.add(function () { this.flag.scale.setTo(1) }, { flag: flag });
  45. }
  46. },
  47. // Calls loading screen while loads language
  48. func_setLang: function () {
  49. // Saves language name e.g 'pt_BR'
  50. langString = this.langs_lang;
  51. // Calls loading screen
  52. game.state.start('load');
  53. }
  54. };
  55. // Loads selected language to be able to translate the game text
  56. let loadState = {
  57. preload: function () {
  58. // Progress bar
  59. const progressBar = game.add.sprite(game.world.centerX, game.world.centerY, 'progressBar');
  60. progressBar.anchor.setTo(0.5, 0.5);
  61. game.load.setPreloadSprite(progressBar);
  62. // Loads selected language
  63. loadLangs('/Ifractions-web/assets/lang/' + langString);
  64. },
  65. create: function () {
  66. if (debugMode) console.log("Language: " + langString);
  67. // Make sure to only ask for player name on the first time oppening the game
  68. if (this.firstTime == undefined) {
  69. this.firstTime = false;
  70. game.state.start('name'); // first time opening ifractions ('language' >> 'name' >> 'menu')
  71. } else {
  72. game.state.start('menu'); // if changing language during the game ('language' >>>> 'menu')
  73. }
  74. }
  75. };
  76. // NAME SCREEN: asks for player's name
  77. let nameState = {
  78. create: function () {
  79. // Set title and warning text
  80. const title = game.add.text(this.game.world.centerX, this.game.world.centerY - 100, lang.insert_name, textStyles.title1);
  81. title.anchor.setTo(0.5);
  82. this.warningEmptyName = game.add.text(this.game.world.centerX, this.game.world.centerY - 70, "", textStyles.overtitle);
  83. this.warningEmptyName.anchor.setTo(0.5);
  84. // Set 'ok' button that gets player's information
  85. const btn = game.add.graphics(this.game.world.centerX - 84, this.game.world.centerY + 70);
  86. btn.beginFill(colors.teal);
  87. btn.drawRect(0, 0, 168, 60);
  88. btn.alpha = 0.5;
  89. btn.endFill();
  90. btn.inputEnabled = true;
  91. btn.input.useHandCursor = true;
  92. btn.events.onInputDown.add(this.func_checkEmptyName);
  93. btn.events.onInputOver.add(function () { btn.alpha = 0.4 });
  94. btn.events.onInputOut.add(function () { btn.alpha = 0.5 });
  95. // Set button Text
  96. const ready = game.add.text(this.game.world.centerX + 1, this.game.world.centerY + 102, lang.ready, textStyles.buttonLabel);
  97. ready.anchor.setTo(0.5);
  98. // Makes text field visible
  99. document.getElementById("text-field").style.visibility = "visible";
  100. // Does the same as the button click when the player presses "enter"
  101. document.getElementById("name_id").addEventListener('keypress', function (e) {
  102. let keycode = e.keycode ? e.keycode : e.which;
  103. if (keycode == 13) nameState.func_checkEmptyName();
  104. });
  105. },
  106. func_checkEmptyName: function () {
  107. // If text field is empty displays error message
  108. if (document.getElementById("name_id").value == "") {
  109. nameState.warningEmptyName.setText(lang.empty_name);
  110. } else { // If text field is NOT empty calls function that saves the player's name
  111. nameState.func_saveName();
  112. }
  113. },
  114. func_saveName: function () {
  115. // Saves player's input in global variable 'playerName'
  116. playerName = document.getElementById("name_id").value;
  117. // Hides and clears text field
  118. document.getElementById("text-field").style.visibility = "hidden";
  119. document.getElementById("name_id").value = "";
  120. if (audioStatus) sound.beepSound.play();
  121. if (debugMode) console.log("Username: " + playerName);
  122. // Calls 'menu' state
  123. game.state.start('menu');
  124. }
  125. };