preMenu.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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(){} //calls menu.js -> menuState
  17. };
  18. let iconSettings = {
  19. func_addButtons: function(_,_,_,_,_,_,_,_,_){},
  20. loadState: function(){}
  21. };
  22. */
  23. // Language screen: the player can choose a preferred language for the game text to be displayed
  24. let langState = {
  25. create: function() {
  26. //
  27. if(this.game.world.width != defaultWidth) this.game.world.setBounds(0, 0, defaultWidth, this.game.world.height);
  28. const style = { font: '28px Arial', fill: '#00804d', align: 'center' };
  29. game.stage.backgroundColor = '#cce5ff';
  30. // Preparing and setting language screen elements
  31. const langs = {
  32. list: [],
  33. text: ['FRAÇÕES ', 'FRAZIONI ', 'FRACTIONS ', 'FRACCIONES ', 'FRACTIONS '],
  34. flag: ['flag_BR', 'flag_IT', 'flag_US', 'flag_PE', 'flag_FR' ],
  35. lang: ['pt_BR', 'it_IT', 'en_US', 'es_PE', 'fr_FR' ],
  36. x_text: [-220, -220, -220, 200, 200],
  37. x_flag: [-120, -120, -120, 300, 300],
  38. y: [-180, 0, 180, -100, 100]
  39. }
  40. for(let i=0; i<langs.lang.length; i++){
  41. // Add text for language names
  42. const titleList = game.add.text(this.game.world.centerX + langs.x_text[i], this.game.world.centerY + langs.y[i], langs.text[i], style);
  43. titleList.anchor.setTo(1, 0.5);
  44. // Add images for flags
  45. langs.list[i] = game.add.sprite(this.game.world.centerX + langs.x_flag[i], this.game.world.centerY + langs.y[i], langs.flag[i]);
  46. langs.list[i].anchor.setTo(0.5, 0.5);
  47. // Set event listener for the flags
  48. langs.list[i].inputEnabled = true;
  49. langs.list[i].input.useHandCursor = true;
  50. langs.list[i].events.onInputDown.add(this.func_setLang,{ langs_lang: langs.lang[i] });
  51. langs.list[i].events.onInputOver.add(function(){ this.langs_list.scale.setTo(1.05) },{ langs_list: langs.list[i] });
  52. langs.list[i].events.onInputOut.add( function(){ this.langs_list.scale.setTo(1) },{ langs_list: langs.list[i] });
  53. }
  54. },
  55. // Calls loading screen
  56. func_setLang: function(){
  57. // Temporarily sets value of global 'lang' to chosen language
  58. lang = this.langs_lang;
  59. // Calls loading screen
  60. game.state.start('load');
  61. }
  62. };
  63. // Loading screen: shows loading bar while loads json dictionary
  64. let loadState = {
  65. preload: function() {
  66. let progressBar = game.add.sprite(game.world.centerX, game.world.centerY, 'progressBar');
  67. progressBar.anchor.setTo(0.5, 0.5);
  68. game.load.setPreloadSprite(progressBar);
  69. // Loads jason dictionary, setting the game language chosen by the player
  70. game.load.json('dictionary', 'assets/languages/'+lang+'.json');
  71. },
  72. create: function() {
  73. // Set value of global 'lang' to json's dictionary
  74. lang = game.cache.getJSON('dictionary');
  75. // Set screens flow
  76. if(firstTime==true){ // first time opening ifractions goes from 'language' to 'name' screen then 'menu'
  77. firstTime = false;
  78. game.state.start('name');
  79. }else{ // changing language during the game skips 'name' screen
  80. game.state.start('menu');
  81. }
  82. }
  83. };
  84. // Name screen: Asks for player's name
  85. let nameState = {
  86. create: function() {
  87. const style = { font: '30px Arial', fill: '#00804d', align: 'center' };
  88. // Set title and error text
  89. const title = game.add.text(this.game.world.centerX, this.game.world.centerY - 100, lang.insert_name, style);
  90. title.anchor.setTo(0.5);
  91. errorEmptyName = game.add.text(this.game.world.centerX, this.game.world.centerY - 70, "", {font: '18px Arial', fill: '#330000', align: 'center'});
  92. errorEmptyName.anchor.setTo(0.5);
  93. // Set button that gets player's information
  94. let btn = game.add.graphics(this.game.world.centerX - 84, this.game.world.centerY + 70);
  95. btn.lineStyle(1, 0x293d3d);
  96. btn.beginFill(0x3d5c5c);
  97. btn.drawRect(0, 0, 168, 60);
  98. btn.alpha = 0.5;
  99. btn.endFill();
  100. btn.inputEnabled = true;
  101. btn.input.useHandCursor = true;
  102. btn.events.onInputDown.add(this.func_checkEmptyName);
  103. btn.events.onInputOver.add(function(){ btn.alpha=0.4 });
  104. btn.events.onInputOut.add(function(){ btn.alpha=0.5 });
  105. // Set button Text
  106. const ready = game.add.text(this.game.world.centerX + 1, this.game.world.centerY + 102, lang.ready, { font: '34px Arial', fill: '#f0f5f5', align: 'center' });
  107. ready.anchor.setTo(0.5);
  108. // Makes text field visible
  109. document.getElementById("text-field-div").style.visibility = "visible";
  110. // Accepts "enter" as button click
  111. document.getElementById("name_id").addEventListener('keypress', function(e){
  112. let keycode = e.keycode ? e.keycode : e.which;
  113. if(keycode == 13){
  114. nameState["func_checkEmptyName"]();
  115. }
  116. });
  117. },
  118. func_checkEmptyName: function() {
  119. // If text field is NOT empty will call function that saves the player's name
  120. if(document.getElementById("name_id").value!=""){
  121. errorEmptyName.setText("");
  122. nameState["func_saveName"]();
  123. }else{
  124. errorEmptyName.setText(lang.empty_name); // Displays error message
  125. }
  126. },
  127. // Calls menu screen
  128. func_saveName: function() {
  129. // Saves player's input in global variable 'username'
  130. username = document.getElementById("name_id").value;
  131. if(debugMode) console.log("user is " + username);
  132. // Hides and clears text field
  133. document.getElementById("text-field-div").style.visibility = "hidden";
  134. document.getElementById("name_id").value = "";
  135. if(audioStatus) beepSound.play();
  136. // Calls menu state
  137. game.state.start('menu');
  138. }
  139. };
  140. let iconSettings = {
  141. // Add navigation icons on the top of the page based the entered parameters
  142. func_addButtons: function(left_side, right_side, left_btn0, left_btn1, left_btn2, right_btn0, right_btn1, level, helpBtn){
  143. this.left_x = 10;
  144. this.right_x = defaultWidth - 50 - 10;
  145. // Labels for the navigation icons
  146. if(left_side){
  147. this.left_text = game.add.text(this.left_x, 53, "",
  148. { font: "20px Arial", fill: "#330000", align: "center" });
  149. }
  150. if(right_side){
  151. this.right_text = game.add.text(this.right_x+50, 53, "",
  152. { font: "20px Arial", fill: "#330000", align: "right" });
  153. this.right_text.anchor.setTo(1,0.02);
  154. }
  155. // Buttons on the left side of the page
  156. if(left_btn0){ // Return to select difficulty screen
  157. this.icon_back = game.add.sprite(this.left_x, 10, 'back');
  158. this.icon_back.inputEnabled = true;
  159. this.icon_back.input.useHandCursor = true;
  160. this.icon_back.events.onInputDown.add(this.loadState, {state: level, beep: beepSound});
  161. this.icon_back.events.onInputOver.add(function(){ this.left_text.text = lang.menu_back},{left_text: this.left_text});
  162. this.icon_back.events.onInputOut.add(function(){ this.left_text.text = ""},{left_text: this.left_text});
  163. this.left_x+=50;
  164. }
  165. if(left_btn1){ // Return to main menu screen
  166. this.icon_list = game.add.sprite(this.left_x, 10, 'list');
  167. this.icon_list.inputEnabled = true;
  168. this.icon_list.input.useHandCursor = true;
  169. this.icon_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  170. this.icon_list.events.onInputOver.add(function(){ this.left_text.text = lang.menu_list},{left_text: this.left_text});
  171. this.icon_list.events.onInputOut.add(function(){ this.left_text.text = ""},{left_text: this.left_text});
  172. this.left_x+=50;
  173. }
  174. if(left_btn2){ // In some levels, shows solution to the game
  175. this.icon_help = game.add.sprite(this.left_x, 10, 'help');
  176. this.icon_help.inputEnabled = true;
  177. this.icon_help.input.useHandCursor = true;
  178. this.icon_help.events.onInputDown.add(helpBtn, {beep: beepSound});
  179. this.icon_help.events.onInputOver.add(function(){ this.left_text.text = lang.menu_help},{left_text: this.left_text});
  180. this.icon_help.events.onInputOut.add(function(){ this.left_text.text = ""},{left_text: this.left_text});
  181. this.left_x+=50;
  182. }
  183. // Buttons on the right side of the page
  184. if(right_btn0){ // Turns game audio on/off
  185. this.icon_audio = game.add.sprite(this.right_x, 10, 'audio');
  186. audioStatus ? this.icon_audio.frame = 0 : this.icon_audio.frame = 1;
  187. this.icon_audio.inputEnabled = true;
  188. this.icon_audio.input.useHandCursor = true;
  189. this.icon_audio.events.onInputDown.add(function(){ if(audioStatus){ audioStatus=false; this.icon_audio.frame = 1; }else{ audioStatus=true; this.icon_audio.frame = 0; }},{icon_audio: this.icon_audio});
  190. this.icon_audio.events.onInputOver.add(function(){ this.right_text.text = lang.audio },{right_text: this.right_text});
  191. this.icon_audio.events.onInputOut.add(function(){ this.right_text.text = "" },{right_text: this.right_text});
  192. this.right_x-=50;
  193. }
  194. if(right_btn1){ // Return to select language screen
  195. this.icon_world = game.add.sprite(this.right_x, 10, 'world');
  196. this.icon_world.inputEnabled = true;
  197. this.icon_world.input.useHandCursor = true;
  198. this.icon_world.events.onInputDown.add(this.loadState, {state: "language", beep: beepSound});
  199. this.icon_world.events.onInputOver.add(function(){ this.right_text.text = lang.menu_world },{right_text : this.right_text});
  200. this.icon_world.events.onInputOut.add(function(){ this.right_text.text = "" },{right_text: this.right_text});
  201. this.right_x-=50;
  202. }
  203. },
  204. changeRightButtonX: function(newWidth){
  205. this.right_text.x = newWidth - 10;
  206. this.icon_audio.x = newWidth - 50 - 10;
  207. if(debugMode) console.log(this.icon_audio.x+" "+newWidth);
  208. this.icon_world.x = newWidth - 50 - 50 - 10;
  209. },
  210. loadState: function(){
  211. if(audioStatus){
  212. this.beep.play();
  213. }
  214. game.state.start(this.state);
  215. }
  216. };