preMenu.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. var langState = {
  3. create: function(){},
  4. --------------------------------------- end of phaser functions
  5. func_setLang: function(){} //calls loadState
  6. };
  7. var loadState = {
  8. preload: function(){},
  9. create: function(){} //calls nameState
  10. -------------------------------------- end of phaser functions
  11. };
  12. var nameState = {
  13. create: function(){},
  14. ------------------------------------------------ end of phaser functions
  15. func_checkEmptyName: function(){}
  16. func_savename: function(){} //calls menu.js -> menuState
  17. };
  18. var buttonSettings = {
  19. func_addButtons: function(_,_,_,_,_,_,_,_,_){},
  20. loadState: function(){}
  21. };
  22. */
  23. // "choose language" screen
  24. var 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. var 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. var flagObjList = [];
  37. var langNameList= ['FRAÇÕES ', 'FRAZIONI ', 'FRACTIONS ', 'FRACCIONES ', 'FRACTIONS ' ];
  38. var flagList = ['flag_BR', 'flag_IT', 'flag_US', 'flag_PE', 'flag_FR' ];
  39. var langList = ['pt_BR', 'it_IT', 'en_US', 'es_PE', 'fr_FR' ];
  40. var x1List = [-220, -220, -220, 200, 200];
  41. var x2List = [-120, -120, -120, 300, 300];
  42. var yList = [-180, 0, 180, -100, 100];
  43. for(var i=0; i<langList.length; i++){
  44. var 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. var loadState = {
  64. preload: function() {
  65. // Displaying the progress bar
  66. var 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. var nameState = {
  85. create: function() {
  86. // AUX
  87. var style = { font: '30px Arial', fill: '#00804d', align: 'center' };
  88. var styleName = { font: '44px Arial', fill: '#000000', align: 'center' };
  89. // title
  90. var title = game.add.text(this.game.world.centerX, this.game.world.centerY - 100, lang.insert_name, style);
  91. title.anchor.setTo(0.5);
  92. errorEmptyName = game.add.text(this.game.world.centerX, this.game.world.centerY - 70, "", {font: '18px Arial', fill: '#330000', align: 'center'});
  93. errorEmptyName.anchor.setTo(0.5);
  94. // "READY" button
  95. var btn = game.add.graphics(this.game.world.centerX - 84, this.game.world.centerY + 70);
  96. btn.lineStyle(1, 0x293d3d);
  97. btn.beginFill(0x3d5c5c);
  98. btn.drawRect(0, 0, 168, 60);
  99. btn.alpha = 0.5;
  100. btn.endFill();
  101. btn.inputEnabled = true;
  102. btn.input.useHandCursor = true;
  103. btn.events.onInputDown.add(this.func_checkEmptyName);
  104. btn.events.onInputOver.add(function(){ btn.alpha=0.4 });
  105. btn.events.onInputOut.add(function(){ btn.alpha=0.5 });
  106. var 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. document.getElementById("text-field-div").style.visibility = "visible";
  109. document.getElementById("name_id").addEventListener('keypress', function(e){
  110. var keycode = e.keycode ? e.keycode : e.which;
  111. //se apertar enter vai para ready, assim como o botão
  112. if(keycode == 13){
  113. nameState["func_checkEmptyName"]();
  114. }
  115. });
  116. },
  117. func_checkEmptyName: function() {
  118. if(document.getElementById("name_id").value!=""){
  119. nameState["func_savename"]();
  120. errorEmptyName.setText("");
  121. }else{
  122. errorEmptyName.setText(lang.empty_name);
  123. }
  124. },
  125. func_savename: function() {
  126. // saves the typed name on username variable
  127. username = document.getElementById("name_id").value;
  128. if(debugMode) console.log("user is " + username);
  129. document.getElementById("text-field-div").style.visibility = "hidden";
  130. //clears the text field again
  131. document.getElementById("name_id").value = "";
  132. if(audioStatus){
  133. beepSound.play();
  134. }
  135. game.state.start('menu');
  136. }
  137. };
  138. var buttonSettings = {
  139. m_info_left: null,
  140. m_back: null,
  141. m_list: null,
  142. m_help: null,
  143. m_info_right: null,
  144. m_audio: null,
  145. m_world: null,
  146. xEsq: null,
  147. xDir: null,
  148. func_addButtons: function(left, right, b0Esq, b1Esq, b2Esq, b0Dir, b1Dir, phase, helpBtn){
  149. this.xDir = defaultWidth - 50 - 10;
  150. this.xEsq = 10;
  151. if(left == true){
  152. this.m_info_left = game.add.text(this.xEsq, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  153. }
  154. if(right == true){
  155. this.m_info_right = game.add.text(this.xDir+50, 53, "", { font: "20px Arial", fill: "#330000", align: "right" });
  156. this.m_info_right.anchor.setTo(1,0.02);
  157. }
  158. // left buttons
  159. if(b0Esq == true){
  160. // Return to diffculty
  161. this.m_back = game.add.sprite(this.xEsq, 10, 'back');
  162. this.m_back.inputEnabled = true;
  163. this.m_back.input.useHandCursor = true;
  164. this.m_back.events.onInputDown.add(this.loadState, {state: phase, beep: beepSound});
  165. this.m_back.events.onInputOver.add(function(){ this.m_info_left.text = lang.menu_back},{m_info_left: this.m_info_left});
  166. this.m_back.events.onInputOut.add(function(){ this.m_info_left.text = ""},{m_info_left: this.m_info_left});
  167. this.xEsq+=50;
  168. }
  169. if(b1Esq == true){
  170. // Return to menu button
  171. this.m_list = game.add.sprite(this.xEsq, 10, 'list');
  172. this.m_list.inputEnabled = true;
  173. this.m_list.input.useHandCursor = true;
  174. this.m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  175. this.m_list.events.onInputOver.add(function(){ this.m_info_left.text = lang.menu_list},{m_info_left: this.m_info_left});
  176. this.m_list.events.onInputOut.add(function(){ this.m_info_left.text = ""},{m_info_left: this.m_info_left});
  177. this.xEsq+=50;
  178. }
  179. if(b2Esq == true){
  180. // Help button
  181. this.m_help = game.add.sprite(this.xEsq, 10, 'help');
  182. this.m_help.inputEnabled = true;
  183. this.m_help.input.useHandCursor = true;
  184. this.m_help.events.onInputDown.add(helpBtn, {beep: beepSound});
  185. this.m_help.events.onInputOver.add(function(){ this.m_info_left.text = lang.menu_help},{m_info_left: this.m_info_left});
  186. this.m_help.events.onInputOut.add(function(){ this.m_info_left.text = ""},{m_info_left: this.m_info_left});
  187. this.xEsq+=50;
  188. }
  189. // rightButtons
  190. if(b0Dir == true){
  191. this.m_audio = game.add.sprite(this.xDir, 10, 'audio');
  192. audioStatus ? this.m_audio.frame = 0 : this.m_audio.frame = 1;
  193. this.m_audio.inputEnabled = true;
  194. this.m_audio.input.useHandCursor = true;
  195. 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});
  196. this.m_audio.events.onInputOver.add(function(){ this.m_info_right.text = lang.audio },{m_info_right: this.m_info_right});
  197. this.m_audio.events.onInputOut.add(function(){ this.m_info_right.text = "" },{m_info_right: this.m_info_right});
  198. this.xDir-=50;
  199. }
  200. if(b1Dir == true){
  201. // Return to language button
  202. this.m_world = game.add.sprite(this.xDir, 10, 'world');
  203. this.m_world.inputEnabled = true;
  204. this.m_world.input.useHandCursor = true;
  205. this.m_world.events.onInputDown.add(this.loadState, {state: "language", beep: beepSound});
  206. this.m_world.events.onInputOver.add(function(){ this.m_info_right.text = lang.menu_world },{m_info_right : this.m_info_right});
  207. this.m_world.events.onInputOut.add(function(){ this.m_info_right.text = "" },{m_info_right: this.m_info_right});
  208. this.xDir-=50;
  209. }
  210. },
  211. changeRightButtonX: function(newWidth){
  212. this.m_info_right.x = newWidth - 10;
  213. this.m_audio.x = newWidth - 50 - 10;
  214. console.log(this.m_audio.x+" "+newWidth);
  215. this.m_world.x = newWidth - 50 - 50 - 10;
  216. },
  217. loadState: function(){
  218. if(audioStatus){
  219. this.beep.play();
  220. }
  221. game.state.start(this.state);
  222. }
  223. };