preMenu.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 json with language chosen by the player
  63. game.load.json('dictionary', 'assets/lang/' + langString + '.json');
  64. },
  65. create: function () {
  66. // object used to translate text
  67. lang = game.cache.getJSON('dictionary');
  68. if (debugMode) console.log("Language: " + langString);
  69. // Make sure to only ask for player name on the first time oppening the game
  70. if (this.firstTime == undefined) {
  71. this.firstTime = false;
  72. game.state.start('name'); // first time opening ifractions ('language' >> 'name' >> 'menu')
  73. } else {
  74. game.state.start('menu'); // if changing language during the game ('language' >>>> 'menu')
  75. }
  76. }
  77. };
  78. // NAME SCREEN: asks for player's name
  79. let nameState = {
  80. create: function () {
  81. // Set title and warning text
  82. const title = game.add.text(this.game.world.centerX, this.game.world.centerY - 100, lang.insert_name, textStyles.title1);
  83. title.anchor.setTo(0.5);
  84. this.warningEmptyName = game.add.text(this.game.world.centerX, this.game.world.centerY - 70, "", textStyles.overtitle);
  85. this.warningEmptyName.anchor.setTo(0.5);
  86. // Set 'ok' button that gets player's information
  87. const btn = game.add.graphics(this.game.world.centerX - 84, this.game.world.centerY + 70);
  88. btn.beginFill(colors.teal);
  89. btn.drawRect(0, 0, 168, 60);
  90. btn.alpha = 0.5;
  91. btn.endFill();
  92. btn.inputEnabled = true;
  93. btn.input.useHandCursor = true;
  94. btn.events.onInputDown.add(this.func_checkEmptyName);
  95. btn.events.onInputOver.add(function () { btn.alpha = 0.4 });
  96. btn.events.onInputOut.add(function () { btn.alpha = 0.5 });
  97. // Set button Text
  98. const ready = game.add.text(this.game.world.centerX + 1, this.game.world.centerY + 102, lang.ready, textStyles.buttonLabel);
  99. ready.anchor.setTo(0.5);
  100. // Makes text field visible
  101. document.getElementById("text-field").style.visibility = "visible";
  102. // Does the same as the button click when the player presses "enter"
  103. document.getElementById("name_id").addEventListener('keypress', function (e) {
  104. let keycode = e.keycode ? e.keycode : e.which;
  105. if (keycode == 13) nameState.func_checkEmptyName();
  106. });
  107. },
  108. func_checkEmptyName: function () {
  109. // If text field is empty displays error message
  110. if (document.getElementById("name_id").value == "") {
  111. nameState.warningEmptyName.setText(lang.empty_name);
  112. } else { // If text field is NOT empty calls function that saves the player's name
  113. nameState.func_saveName();
  114. }
  115. },
  116. func_saveName: function () {
  117. // Saves player's input in global variable 'playerName'
  118. playerName = document.getElementById("name_id").value;
  119. // Hides and clears text field
  120. document.getElementById("text-field").style.visibility = "hidden";
  121. document.getElementById("name_id").value = "";
  122. if (audioStatus) beepSound.play();
  123. if (debugMode) console.log("Username: " + playerName);
  124. // Calls 'menu' state
  125. game.state.start('menu');
  126. }
  127. };