preMenu.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 buttonSettings = {
  19. func_addButtons: function(_,_,_,_,_,_,_,_,_){},
  20. loadState: function(){}
  21. };
  22. */
  23. // "choose language" screen
  24. let langState = {
  25. create: function() {
  26. if(this.game.world.width != defaultWidth) this.game.world.setBounds(0, 0, defaultWidth, this.game.world.height);
  27. // AUX
  28. const style = { font: '28px Arial', fill: '#00804d', align: 'center' };
  29. // AUDIO
  30. beepSound = game.add.audio('sound_beep'); // game sound
  31. okSound = game.add.audio('sound_ok'); // correct answer sound
  32. errorSound = game.add.audio('sound_error'); // wrong answer sound
  33. // BACKGROUND
  34. game.stage.backgroundColor = '#cce5ff';
  35. // LANGUAGES
  36. let flagObjList = [];
  37. const langNameList= ['FRAÇÕES ', 'FRAZIONI ', 'FRACTIONS ', 'FRACCIONES ', 'FRACTIONS ' ];
  38. const flagList = ['flag_BR', 'flag_IT', 'flag_US', 'flag_PE', 'flag_FR' ];
  39. const langList = ['pt_BR', 'it_IT', 'en_US', 'es_PE', 'fr_FR' ];
  40. const x1List = [-220, -220, -220, 200, 200];
  41. const x2List = [-120, -120, -120, 300, 300];
  42. const yList = [-180, 0, 180, -100, 100];
  43. for(let i=0; i<langList.length; i++){
  44. let titleList = game.add.text(this.game.world.centerX + x1List[i], this.game.world.centerY + yList[i], langNameList[i], style);
  45. titleList.anchor.setTo(1, 0.5);
  46. flagObjList[i] = game.add.sprite(this.game.world.centerX + x2List[i], this.game.world.centerY + yList[i], flagList[i]);
  47. flagObjList[i].anchor.setTo(0.5, 0.5);
  48. flagObjList[i].inputEnabled = true;
  49. flagObjList[i].input.useHandCursor = true;
  50. flagObjList[i].events.onInputDown.add(this.func_setLang,{ lang: langList[i] });
  51. flagObjList[i].events.onInputOver.add(function(){ this.flagObj.scale.setTo(1.05) },{ flagObj: flagObjList[i] });
  52. flagObjList[i].events.onInputOut.add( function(){ this.flagObj.scale.setTo(1) },{ flagObj: flagObjList[i] });
  53. }
  54. },
  55. func_setLang: function(){
  56. // set language
  57. lang = this.lang;
  58. // start resource loading
  59. game.state.start('load');
  60. }
  61. };
  62. // "loading" screen and load json dictionary
  63. let loadState = {
  64. preload: function() {
  65. // Displaying the progress bar
  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. // Loading dictionary
  70. game.load.json('dictionary', 'assets/languages/'+lang+'.json');
  71. },
  72. create: function() {
  73. // gets selected language from json
  74. lang = game.cache.getJSON('dictionary');
  75. if(firstTime==true){ // select language screen - first time opening ifractions
  76. firstTime = false;
  77. game.state.start('name'); // go to select name screen, then menu
  78. }else{ // changing language during the game
  79. game.state.start('menu'); // go to menu
  80. }
  81. }
  82. };
  83. // "username" screen
  84. let nameState = {
  85. create: function() {
  86. // AUX
  87. const style = { font: '30px Arial', fill: '#00804d', align: 'center' };
  88. // title
  89. let 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. // "READY" button
  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. let ready = game.add.text(this.game.world.centerX + 1, this.game.world.centerY + 102, lang.ready, { font: '34px Arial', fill: '#f0f5f5', align: 'center' });
  106. ready.anchor.setTo(0.5);
  107. document.getElementById("text-field-div").style.visibility = "visible";
  108. document.getElementById("name_id").addEventListener('keypress', function(e){
  109. let keycode = e.keycode ? e.keycode : e.which;
  110. //se apertar enter vai para ready, assim como o botão
  111. if(keycode == 13){
  112. nameState["func_checkEmptyName"]();
  113. }
  114. });
  115. },
  116. func_checkEmptyName: function() {
  117. if(document.getElementById("name_id").value!=""){
  118. nameState["func_savename"]();
  119. errorEmptyName.setText("");
  120. }else{
  121. errorEmptyName.setText(lang.empty_name);
  122. }
  123. },
  124. func_savename: function() {
  125. // saves the typed name on username variable
  126. username = document.getElementById("name_id").value;
  127. if(debugMode) console.log("user is " + username);
  128. document.getElementById("text-field-div").style.visibility = "hidden";
  129. //clears the text field again
  130. document.getElementById("name_id").value = "";
  131. if(audioStatus){
  132. beepSound.play();
  133. }
  134. game.state.start('menu');
  135. }
  136. };
  137. let buttonSettings = {
  138. m_info_left: null,
  139. m_back: null,
  140. m_list: null,
  141. m_help: null,
  142. m_info_right: null,
  143. m_audio: null,
  144. m_world: null,
  145. xEsq: null,
  146. xDir: null,
  147. func_addButtons: function(left, right, b0Esq, b1Esq, b2Esq, b0Dir, b1Dir, phase, helpBtn){
  148. this.xDir = defaultWidth - 50 - 10;
  149. this.xEsq = 10;
  150. if(left == true){
  151. this.m_info_left = game.add.text(this.xEsq, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  152. }
  153. if(right == true){
  154. this.m_info_right = game.add.text(this.xDir+50, 53, "", { font: "20px Arial", fill: "#330000", align: "right" });
  155. this.m_info_right.anchor.setTo(1,0.02);
  156. }
  157. // left buttons
  158. if(b0Esq == true){
  159. // Return to diffculty
  160. this.m_back = game.add.sprite(this.xEsq, 10, 'back');
  161. this.m_back.inputEnabled = true;
  162. this.m_back.input.useHandCursor = true;
  163. this.m_back.events.onInputDown.add(this.loadState, {state: phase, beep: beepSound});
  164. this.m_back.events.onInputOver.add(function(){ this.m_info_left.text = lang.menu_back},{m_info_left: this.m_info_left});
  165. this.m_back.events.onInputOut.add(function(){ this.m_info_left.text = ""},{m_info_left: this.m_info_left});
  166. this.xEsq+=50;
  167. }
  168. if(b1Esq == true){
  169. // Return to menu button
  170. this.m_list = game.add.sprite(this.xEsq, 10, 'list');
  171. this.m_list.inputEnabled = true;
  172. this.m_list.input.useHandCursor = true;
  173. this.m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  174. this.m_list.events.onInputOver.add(function(){ this.m_info_left.text = lang.menu_list},{m_info_left: this.m_info_left});
  175. this.m_list.events.onInputOut.add(function(){ this.m_info_left.text = ""},{m_info_left: this.m_info_left});
  176. this.xEsq+=50;
  177. }
  178. if(b2Esq == true){
  179. // Help button
  180. this.m_help = game.add.sprite(this.xEsq, 10, 'help');
  181. this.m_help.inputEnabled = true;
  182. this.m_help.input.useHandCursor = true;
  183. this.m_help.events.onInputDown.add(helpBtn, {beep: beepSound});
  184. this.m_help.events.onInputOver.add(function(){ this.m_info_left.text = lang.menu_help},{m_info_left: this.m_info_left});
  185. this.m_help.events.onInputOut.add(function(){ this.m_info_left.text = ""},{m_info_left: this.m_info_left});
  186. this.xEsq+=50;
  187. }
  188. // rightButtons
  189. if(b0Dir == true){
  190. this.m_audio = game.add.sprite(this.xDir, 10, 'audio');
  191. audioStatus ? this.m_audio.frame = 0 : this.m_audio.frame = 1;
  192. this.m_audio.inputEnabled = true;
  193. this.m_audio.input.useHandCursor = true;
  194. this.m_audio.events.onInputDown.add(function(){ if(audioStatus){ audioStatus=false; this.m_audio.frame = 1; }else{ audioStatus=true; this.m_audio.frame = 0; }},{m_audio: this.m_audio});
  195. this.m_audio.events.onInputOver.add(function(){ this.m_info_right.text = lang.audio },{m_info_right: this.m_info_right});
  196. this.m_audio.events.onInputOut.add(function(){ this.m_info_right.text = "" },{m_info_right: this.m_info_right});
  197. this.xDir-=50;
  198. }
  199. if(b1Dir == true){
  200. // Return to language button
  201. this.m_world = game.add.sprite(this.xDir, 10, 'world');
  202. this.m_world.inputEnabled = true;
  203. this.m_world.input.useHandCursor = true;
  204. this.m_world.events.onInputDown.add(this.loadState, {state: "language", beep: beepSound});
  205. this.m_world.events.onInputOver.add(function(){ this.m_info_right.text = lang.menu_world },{m_info_right : this.m_info_right});
  206. this.m_world.events.onInputOut.add(function(){ this.m_info_right.text = "" },{m_info_right: this.m_info_right});
  207. this.xDir-=50;
  208. }
  209. },
  210. changeRightButtonX: function(newWidth){
  211. this.m_info_right.x = newWidth - 10;
  212. this.m_audio.x = newWidth - 50 - 10;
  213. if(debugMode) console.log(this.m_audio.x+" "+newWidth);
  214. this.m_world.x = newWidth - 50 - 50 - 10;
  215. },
  216. loadState: function(){
  217. if(audioStatus){
  218. this.beep.play();
  219. }
  220. game.state.start(this.state);
  221. }
  222. };