preMenu.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. var errorEmptyName;
  2. /*
  3. var langState = {
  4. create: function(){},
  5. --------------------------------------- end of phaser functions
  6. setLang: function(){} //calls loadState
  7. };
  8. var loadState = {
  9. preload: function(){},
  10. create: function(){} //calls nameState
  11. -------------------------------------- end of phaser functions
  12. };
  13. var nameState = {
  14. create: function(){},
  15. ------------------------------------------------ end of phaser functions
  16. nameIsEmpty: function(){}
  17. ready: function(){} //calls menu.js -> menuState
  18. };
  19. var buttonSettings = {
  20. addButtons: function(_,_,_,_,_,_,_,_,_){},
  21. loadState: function(){}
  22. };
  23. */
  24. // "choose language" screen
  25. var langState = {
  26. create: function() {
  27. game.stage.backgroundColor = '#cce5ff';
  28. var style = { font: '28px Arial', fill: '#00804d', align: 'center' };
  29. //pt_BR
  30. var title1 = game.add.text(this.game.world.centerX - 220, this.game.world.centerY - 100, 'FRAÇÕES ', style);
  31. title1.anchor.setTo(1, 0.5);
  32. var flag1 = game.add.sprite(this.game.world.centerX - 120, this.game.world.centerY - 100, 'flag_BR');
  33. flag1.anchor.setTo(0.5, 0.5);
  34. flag1.inputEnabled = true;
  35. flag1.input.useHandCursor = true;
  36. flag1.events.onInputDown.add(this.setLang,{lang:'pt_BR'});
  37. flag1.events.onInputOver.add(function(){ flag1.scale.setTo(1.05) });
  38. flag1.events.onInputOut.add(function(){ flag1.scale.setTo(1) });
  39. //es_PE
  40. var title2 = game.add.text(this.game.world.centerX + 200, this.game.world.centerY - 100, 'FRACCIONES ', style);
  41. title2.anchor.setTo(1, 0.5);
  42. var flag2 = game.add.sprite(this.game.world.centerX + 300, this.game.world.centerY - 100, 'flag_PE');
  43. flag2.anchor.setTo(0.5, 0.5);
  44. flag2.inputEnabled = true;
  45. flag2.input.useHandCursor = true;
  46. flag2.events.onInputDown.add(this.setLang,{lang:'es_PE'});
  47. flag2.events.onInputOver.add(function(){ flag2.scale.setTo(1.05) });
  48. flag2.events.onInputOut.add(function(){ flag2.scale.setTo(1) });
  49. //en_US
  50. var title3 = game.add.text(this.game.world.centerX - 220, this.game.world.centerY + 100, 'FRACTIONS ', style);
  51. title3.anchor.setTo(1, 0.5);
  52. var flag3 = game.add.sprite(this.game.world.centerX - 120, this.game.world.centerY + 100, 'flag_US');
  53. flag3.anchor.setTo(0.5, 0.5);
  54. flag3.inputEnabled = true;
  55. flag3.input.useHandCursor = true;
  56. flag3.events.onInputDown.add(this.setLang,{lang:'en_US'});
  57. flag3.events.onInputOver.add(function(){ flag3.scale.setTo(1.05) });
  58. flag3.events.onInputOut.add(function(){ flag3.scale.setTo(1) });
  59. //fr_FR
  60. var title4 = game.add.text(this.game.world.centerX + 200, this.game.world.centerY + 100, 'FRACTIONS ', style);
  61. title4.anchor.setTo(1, 0.5);
  62. var flag4 = game.add.sprite(this.game.world.centerX + 300, this.game.world.centerY + 100, 'flag_FR');
  63. flag4.anchor.setTo(0.5, 0.5);
  64. flag4.inputEnabled = true;
  65. flag4.input.useHandCursor = true;
  66. flag4.events.onInputDown.add(this.setLang,{lang:'fr_FR'});
  67. flag4.events.onInputOver.add(function(){ flag4.scale.setTo(1.05) });
  68. flag4.events.onInputOut.add(function(){ flag4.scale.setTo(1) });
  69. },
  70. setLang: function(){
  71. //set language
  72. lang = this.lang;
  73. //start resource loading
  74. game.state.start('load');
  75. }
  76. };
  77. // "loading" screen
  78. var loadState = {
  79. preload: function() {
  80. // Displaying the progress bar
  81. var progressBar = game.add.sprite(game.world.centerX, game.world.centerY, 'progressBar');
  82. progressBar.anchor.setTo(0.5, 0.5);
  83. game.load.setPreloadSprite(progressBar);
  84. // Loading dictionary
  85. game.load.json('dictionary', 'assets/languages/'+lang+'.json');
  86. },
  87. create: function() {
  88. // gets selected language from json
  89. lang = game.cache.getJSON('dictionary');
  90. audio_lang_aux = lang.audio_on;
  91. if(!nameStatus){
  92. nameStatus = true;
  93. game.state.start('name');
  94. }else{
  95. game.state.start('menu');
  96. }
  97. }
  98. };
  99. // "username" screen
  100. var nameState = {
  101. create: function() {
  102. var style = { font: '30px Arial', fill: '#00804d', align: 'center' };
  103. var styleName = { font: '44px Arial', fill: '#000000', align: 'center' };
  104. // title
  105. var title = game.add.text(this.game.world.centerX, this.game.world.centerY - 100, lang.insert_name, style);
  106. title.anchor.setTo(0.5);
  107. // "READY" button
  108. var btn = game.add.graphics(this.game.world.centerX - 84, this.game.world.centerY + 70);
  109. btn.lineStyle(1, 0x293d3d);
  110. btn.beginFill(0x3d5c5c);
  111. btn.drawRect(0, 0, 168, 60);
  112. btn.alpha = 0.5;
  113. btn.endFill();
  114. btn.inputEnabled = true;
  115. btn.input.useHandCursor = true;
  116. btn.events.onInputDown.add(this.nameIsEmpty, null);
  117. btn.events.onInputOver.add(function(){ btn.alpha=0.4 });
  118. btn.events.onInputOut.add(function(){ btn.alpha=0.5 });
  119. var ready = game.add.text(this.game.world.centerX + 1, this.game.world.centerY + 102, lang.ready, { font: '34px Arial', fill: '#f0f5f5', align: 'center' });
  120. ready.anchor.setTo(0.5);
  121. errorEmptyName = game.add.text(this.game.world.centerX, this.game.world.centerY - 70, "", {font: '18px Arial', fill: '#330000', align: 'center'});
  122. errorEmptyName.anchor.setTo(0.5);
  123. document.getElementById("text-field-div").style.visibility = "visible";
  124. document.getElementById("name_id").addEventListener('keypress', function(e){
  125. var keycode = e.keycode ? e.keycode : e.which;
  126. //se apertar enter vai para ready, assim como o botão
  127. if(keycode == 13){
  128. nameState["nameIsEmpty"]();
  129. }
  130. });
  131. },
  132. nameIsEmpty: function() {
  133. if(document.getElementById("name_id").value!=""){
  134. nameState["ready"]();
  135. errorEmptyName.setText("");
  136. }else{
  137. errorEmptyName.setText(lang.empty_name);
  138. }
  139. },
  140. ready: function() {
  141. // saves the typed name on username variable
  142. username = document.getElementById("name_id").value;
  143. console.log("user is" + username);
  144. document.getElementById("text-field-div").style.visibility = "hidden";
  145. //clears the text field again
  146. document.getElementById("name_id").value = "";
  147. if(oneMenu){ //If menu, normal game
  148. // Go to the menu state
  149. game.state.start('menu');
  150. }else{ //parameters game
  151. if(oneShape=="Circle"){
  152. game.state.start("mapCOne");
  153. }else if(oneShape=="Square"){
  154. if(oneOperator=='Mixed'){
  155. twoPosition = 0;
  156. twoMove = true;
  157. twoDifficulty = oneDifficulty;
  158. twoOperator= "";
  159. twoLabel= false;
  160. twoShape = oneShape;
  161. twoType = oneType;
  162. twoMenu = false;
  163. game.state.start("mapSTwo");
  164. }else{
  165. game.state.start("mapSOne");
  166. }
  167. }
  168. }
  169. }
  170. };
  171. var m_info_right, m_info_left;
  172. var m_world, m_menu, m_back, m_help, m_audio;
  173. var buttonSettings = {
  174. addButtons: function(left, right, b0Esq, b1Esq, b2Esq, b0Dir, b1Dir, phase, helpBtn){
  175. var xEsq = 10;
  176. var xDir = (game.world.width - 50 - 10);
  177. if(left == 1){
  178. m_info_left = game.add.text(xEsq, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  179. }
  180. if(right == 1){
  181. m_info_right = game.add.text(xDir+50, 53, "", { font: "20px Arial", fill: "#330000", align: "right" });
  182. m_info_right.anchor.setTo(1,0.02);
  183. }
  184. //left buttons
  185. if(b0Esq == 1){
  186. // Return to diffculty
  187. m_back = game.add.sprite(xEsq, 10, 'back');
  188. m_back.inputEnabled = true;
  189. m_back.input.useHandCursor = true;
  190. m_back.events.onInputDown.add(this.loadState, {state: phase, beep: beepSound});
  191. m_back.events.onInputOver.add(function(){ m_info_left.text = lang.menu_back});
  192. m_back.events.onInputOut.add(function(){ m_info_left.text = ""});
  193. xEsq+=50;
  194. }
  195. if(b1Esq == 1){
  196. // Return to menu button
  197. m_list = game.add.sprite(xEsq, 10, 'list');
  198. m_list.inputEnabled = true;
  199. m_list.input.useHandCursor = true;
  200. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  201. m_list.events.onInputOver.add(function(){ m_info_left.text = lang.menu_list});
  202. m_list.events.onInputOut.add(function(){ m_info_left.text = ""});
  203. xEsq+=50;
  204. }
  205. if(b2Esq == 1){
  206. // Help button
  207. m_help = game.add.sprite(xEsq, 10, 'help');
  208. m_help.inputEnabled = true;
  209. m_help.input.useHandCursor = true;
  210. m_help.events.onInputDown.add(helpBtn, {beep: beepSound});
  211. m_help.events.onInputOver.add(function(){ m_info_left.text = lang.menu_help});
  212. m_help.events.onInputOut.add(function(){ m_info_left.text = ""});
  213. xEsq+=50;
  214. }
  215. //rightButtons
  216. if(b0Dir == 1){
  217. m_audio = game.add.sprite(xDir, 10, 'audio');
  218. audioStatus ? m_audio.frame = 0 : m_audio.frame = 1;
  219. m_audio.inputEnabled = true;
  220. m_audio.input.useHandCursor = true;
  221. m_audio.events.onInputDown.add(function(){ if(audioStatus){ audioStatus=false; m_audio.frame = 1; }else{ audioStatus=true; m_audio.frame = 0; }});
  222. m_audio.events.onInputOver.add(function(){ m_info_right.text = lang.audio });
  223. m_audio.events.onInputOut.add(function(){ m_info_right.text = "" });
  224. xDir-=50;
  225. }
  226. if(b1Dir == 1){
  227. // Return to language button
  228. m_world = game.add.sprite(xDir, 10, 'world');
  229. m_world.inputEnabled = true;
  230. m_world.input.useHandCursor = true;
  231. m_world.events.onInputDown.add(this.loadState, {state: "language", beep: beepSound});
  232. m_world.events.onInputOver.add(function(){ m_info_right.text = lang.menu_world });
  233. m_world.events.onInputOut.add(function(){ m_info_right.text = "" });
  234. xDir-=50;
  235. }
  236. },
  237. loadState: function(){
  238. if(audioStatus){
  239. this.beep.play();
  240. }
  241. game.state.start(this.state);
  242. }
  243. };