preMenu.js 13 KB

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