squareTwo.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. let gameSquareTwo = {
  3. create: function(){},
  4. update: function(){},
  5. ---------------------------- end of phaser functions
  6. func_updateCounter: function(){},
  7. func_overSquare: function(){},
  8. func_outSquare: function(){},
  9. func_clickSquare: function(){},
  10. //func_setPlace: function(){},
  11. func_postScore: function(){},
  12. //func_viewHelp: function(){},
  13. //func_checkOverlap: function(){}
  14. func_getRndDivisor: function(){}
  15. };
  16. */
  17. // Fractions Comparison Square states
  18. let gameSquareTwo = {
  19. create: function() {
  20. //timer
  21. totalTime = 0;
  22. timer = game.time.create(false);
  23. timer.loop(1000, this.func_updateCounter, this);
  24. timer.start();
  25. points = [2,4,6,8,9,10,12,14,15,16,18,20];
  26. // Background
  27. game.add.image(0, 0, 'bgimage');
  28. // Calls function that loads navigation icons
  29. iconSettings["func_addButtons"](true,true,
  30. true,true,false,
  31. true,false,
  32. 'difficulty', false);
  33. //Clouds
  34. game.add.image(300, 100, 'cloud');
  35. game.add.image(660, 80, 'cloud');
  36. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  37. // Styles for labels
  38. let stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  39. let styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  40. let styleFraction = { font: '20px Arial', fill: '#000080', align: 'center'};
  41. let styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  42. //Floor
  43. for(let i=0;i<9;i++){
  44. game.add.image(i*100, 501, 'floor');
  45. }
  46. //kid
  47. kid = game.add.sprite(100, 470, 'kid_lost');
  48. kid.anchor.setTo(0.5, 0.7);
  49. kid.scale.setTo(0.8);
  50. //kid.animations.add('right',[0,1,2,3,4,5,6,7,8,9,10,11]);
  51. //kid.animations.add('left',[23,22,21,20,19,18,17,16,15,14,13,12]);
  52. kid.animations.add('front', [3,4,5])
  53. kidDirection = 'front';
  54. kidLeftLimit = 100;
  55. kidRightLimit = 800;
  56. kid.animations.play('front', 6, true);
  57. //Control variables
  58. sizeA = 0; //Size of first block
  59. sizeB = 0; //Size of second block
  60. valueA = 0; //Number of clicked blocks for a
  61. valueB = 0; //Number of clicked blocks for b
  62. clickA = false; //If block A is clicked
  63. clickB = false; //If block B is clicked
  64. animateA = false; //Animate A selected blocks to position
  65. animateB = false; //Animate B selected blocks to position
  66. result = false; //Game is correct
  67. animate = null; //Final animation sequence
  68. //generator
  69. if(debugMode) console.log("----------");
  70. if(debugMode) console.log("Diff " + levelDifficulty + ", ini " + ((levelDifficulty-1)*2+1) + ", end " + ((levelDifficulty-1)*2+3));
  71. let rPoint = game.rnd.integerInRange((levelDifficulty-1)*2+1,(levelDifficulty-1)*2+3);
  72. sizeA = points[rPoint];
  73. if(debugMode) console.log("Rpoint " + rPoint + ", val " + sizeA);
  74. sizeB = this.func_getRndDivisor(sizeA);
  75. blockB = game.rnd.integerInRange(1, sizeB);
  76. blockA = (sizeA/sizeB) * blockB;
  77. if(debugMode) console.log("SA " + sizeA + ", SB " + sizeB + ", BA " + blockA + ", BB " + blockB );
  78. //Blocks and fractions group
  79. blocksA = game.add.group(); //Main blocks A
  80. blocksB = game.add.group(); //Main blocks B
  81. auxblqA = game.add.group(); //Auxiliar blocks A
  82. auxblqB = game.add.group(); //Auxiliar blocks B
  83. //Creating blocks
  84. blockW = 400;
  85. blockH = 50;
  86. if(levelOperator!="C"){
  87. xA=230, yA=90;
  88. xB=xA, yB=yA+3*blockH+30;
  89. }else{
  90. xB=230, yB=90;
  91. xA=xB, yA=yB+3*blockH+30;
  92. }
  93. //Blocks A
  94. let widthA = blockW/sizeA;
  95. let lineColor = 0x1e2f2f;
  96. let fillColor = 0x83afaf;
  97. let fillColorS = 0xe0ebeb;
  98. for(let i=0; i<sizeA; i++){
  99. //if(debugMode) console.log("Block A"+i+": x:"+(xA+i*widthA)+", y:"+yA);
  100. let block = game.add.graphics(xA+i*widthA, yA);
  101. block.anchor.setTo(0.5, 0.5);
  102. block.lineStyle(2, lineColor);
  103. block.beginFill(fillColor);
  104. block.drawRect(0, 0, widthA, blockH);
  105. block.alpha = 0.5;
  106. block.endFill();
  107. block.inputEnabled = true;
  108. block.input.useHandCursor = true;
  109. block.events.onInputDown.add(this.func_clickSquare, {who: 'A',indice: i});
  110. block.events.onInputOver.add(this.func_overSquare, {who: 'A',indice: i});
  111. block.events.onInputOut.add(this.func_outSquare, {who: 'A',indice: i});
  112. blocksA.add(block);
  113. //aux blocks
  114. let xAux = xA+i*widthA, yAux = yA+blockH+10;
  115. if(levelOperator == 'C') yAux = yA;
  116. block = game.add.graphics(xAux, yAux );
  117. block.anchor.setTo(0.5, 0.5);
  118. block.lineStyle(1, lineColor);
  119. block.beginFill(fillColorS);
  120. block.drawRect(0, 0, widthA, blockH);
  121. if(levelOperator!='A') block.alpha = 0;
  122. else block.alpha = 0.2;
  123. auxblqA.add(block);
  124. }
  125. //label block A
  126. let labelX = xA+blockW+30;
  127. let labelY = yA+blockH/2;
  128. labelA = game.add.text(labelX, labelY, sizeA , styleFraction);
  129. labelA.anchor.setTo(0.5, 0.41);
  130. //label fraction
  131. labelX = xA+(blockA*widthA)+40;
  132. labelY = yA+blockH+34;
  133. fractionA = game.add.text(labelX, labelY, "0\n"+sizeA , styleFraction);
  134. fractionA.anchor.setTo(0.5, 0.41);
  135. separatorA = game.add.sprite(labelX, labelY, 'separator');
  136. separatorA.anchor.setTo(0.5, 0.5);
  137. fractionA.alpha = 0;
  138. separatorA.alpha = 0;
  139. //Blocks B
  140. let widthB = blockW/sizeB;
  141. lineColor = 0x260d0d;
  142. fillColor = 0xd27979;
  143. fillColorS = 0xf2d9d9;
  144. for(let i=0; i<sizeB; i++){
  145. let block = game.add.graphics(xB+i*widthB, yB);
  146. block.anchor.setTo(0.5, 0.5);
  147. block.lineStyle(2, lineColor);
  148. block.beginFill(fillColor);
  149. block.drawRect(0, 0, widthB, blockH);
  150. block.endFill();
  151. block.inputEnabled = true;
  152. block.input.useHandCursor = true;
  153. block.events.onInputDown.add(this.func_clickSquare, {who: 'B',indice: i});
  154. block.events.onInputOver.add(this.func_overSquare, {who: 'B',indice: i});
  155. block.events.onInputOut.add(this.func_outSquare, {who: 'B',indice: i});
  156. blocksB.add(block);
  157. //aux blocks
  158. let xAux = xB+i*widthB, yAux = yB+blockH+10;
  159. if(levelOperator == 'C') yAux = yB;
  160. block = game.add.graphics(xAux, yAux);
  161. block.anchor.setTo(0.5, 0.5);
  162. block.lineStyle(1, lineColor);
  163. block.beginFill(fillColorS);
  164. block.drawRect(0, 0, widthB, blockH);
  165. if(levelOperator!='A') block.alpha = 0;
  166. else block.alpha = 0.2;
  167. auxblqB.add(block);
  168. }
  169. //label block B
  170. labelX = xA+blockW+30;
  171. labelY = yB+blockH/2;
  172. labelB = game.add.text(labelX, labelY, sizeB , styleFraction);
  173. labelB.anchor.setTo(0.5, 0.41);
  174. //label fraction
  175. labelX = xA+(blockB*widthB)+40;
  176. labelY = yB+blockH+34;
  177. fractionB = game.add.text(labelX, labelY, "0\n"+sizeB , styleFraction);
  178. fractionB.anchor.setTo(0.5, 0.41);
  179. separatorB = game.add.sprite(labelX, labelY, 'separator');
  180. separatorB.anchor.setTo(0.5, 0.5);
  181. fractionB.alpha = 0;
  182. separatorB.alpha = 0;
  183. //ok and error images
  184. okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  185. okImg.anchor.setTo(0.5);
  186. okImg.alpha = 0;
  187. errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  188. errorImg.anchor.setTo(0.5);
  189. errorImg.alpha = 0;
  190. //error text
  191. errorTextA = game.add.text(game.world.centerX, game.world.centerY-225, "", {font: "20px Arial", fill: "#330000", align: "center" });
  192. errorTextA.anchor.setTo(0.5, 0.5);
  193. errorTextB = game.add.text(game.world.centerX, game.world.centerY-45, "", {font: "20px Arial", fill: "#330000", align: "center" });
  194. errorTextB.anchor.setTo(0.5, 0.5);
  195. counter = 0;
  196. endCounter = 100;
  197. cDelay = 0;
  198. eDelay = 60;
  199. },
  200. update: function() {
  201. if (game.physics.arcade.distanceToPointer(kid, game.input.activePointer) > 20 ){
  202. let xPos = game.input.mousePointer.x;
  203. /*if (xPos < kid.x + 10){
  204. kidDirection='right';
  205. kid.animations.play('right', 8, true);
  206. }else if (xPos > kid.x){
  207. kidDirection='right';
  208. kid.animations.play('right', 8, true);
  209. }*/
  210. //set limit to the arrow
  211. if (xPos < kidLeftLimit){
  212. xPos = kidLeftLimit;
  213. }
  214. if (xPos > kidRightLimit){
  215. xPos = kidRightLimit;
  216. }
  217. kid.x = xPos;
  218. }
  219. //If clicked A only, animate
  220. if(animateA){
  221. for(let i=0;i<valueA;i++){
  222. blocksA.children[i].y +=2;
  223. }
  224. if(blocksA.children[0].y>=auxblqA.children[0].y){
  225. animateA = false;
  226. fractionA.alpha = 1;
  227. fractionA.setText(valueA+"\n"+sizeA);
  228. separatorA.alpha = 1;
  229. }
  230. }
  231. //If clicked B only, animate
  232. if(animateB){
  233. for(let i=0;i<valueB;i++){
  234. blocksB.children[i].y +=2;
  235. }
  236. if(blocksB.children[0].y>=auxblqB.children[0].y){
  237. animateB = false;
  238. fractionB.alpha = 1;
  239. fractionB.setText(valueB+"\n"+sizeB);
  240. separatorB.alpha = 1;
  241. }
  242. }
  243. //if clicked A and B
  244. if(clickA && clickB && !this.animate){
  245. //Check result
  246. timer.stop();
  247. cDelay++;
  248. if(cDelay>=eDelay){
  249. //fractions are equivalent : correct
  250. if((valueA/sizeA) == (valueB/sizeB)){
  251. result = true;
  252. levelMove = true;
  253. if(audioStatus){
  254. okSound.play();
  255. }
  256. kid.animations.stop();
  257. passedLevels++;
  258. if(debugMode) console.log("passedLevels = " + passedLevels);
  259. okImg.alpha = 1;
  260. //fractions are not equivalent
  261. }else{
  262. result = false;
  263. levelMove = false;
  264. if(audioStatus){
  265. errorSound.play();
  266. }
  267. kid.animations.stop();
  268. errorImg.alpha = 1;
  269. }
  270. this.func_postScore();
  271. clickA = false;
  272. clickB = false;
  273. animate = true;
  274. }
  275. }
  276. if(animate){
  277. counter++;
  278. if(result){
  279. // kid.x += 2;
  280. // kidDirection='right';
  281. // kid.animations.play('right', 8, true);
  282. }
  283. if(counter>endCounter){
  284. game.state.start('map');
  285. }
  286. }
  287. },
  288. func_updateCounter: function() {
  289. totalTime++;
  290. },
  291. func_overSquare: function(){
  292. if(!clickA && this.who=="A"){
  293. if(this.indice == sizeA-1){
  294. if(yA==90){
  295. errorTextA.setText(lang.error_msg);
  296. errorTextB.setText("");
  297. }else{
  298. errorTextA.setText("");
  299. errorTextB.setText(lang.error_msg);
  300. }
  301. }else{
  302. errorTextA.setText("");
  303. errorTextB.setText("");
  304. for(let i=0;i<sizeA;i++){
  305. if(i<=this.indice){
  306. blocksA.children[i].alpha = 1;
  307. }else{
  308. blocksA.children[i].alpha = 0.5;
  309. }
  310. }
  311. fractionA.x = xA+((this.indice +1)*(blockW/sizeA))+40;
  312. fractionA.alpha = 1;
  313. fractionA.setText(this.indice +1);
  314. }
  315. }
  316. if(!clickB && this.who=="B"){
  317. if(this.indice == sizeB-1){
  318. if(yA==90){
  319. errorTextA.setText("");
  320. errorTextB.setText(lang.error_msg);
  321. }else{
  322. errorTextA.setText(lang.error_msg);
  323. errorTextB.setText("");
  324. }
  325. }else{
  326. errorTextA.setText("");
  327. errorTextB.setText("");
  328. for(let i=0;i<sizeB;i++){
  329. if(i<=this.indice){
  330. blocksB.children[i].alpha = 1;
  331. }else{
  332. blocksB.children[i].alpha = 0.5;
  333. }
  334. }
  335. fractionB.x = xB+((this.indice +1)*(blockW/sizeB))+40;
  336. fractionB.alpha = 1;
  337. fractionB.setText(this.indice +1);
  338. }
  339. }
  340. },
  341. func_outSquare: function(){
  342. if(!clickA && this.who=="A"){
  343. for(let i=0;i<=this.indice;i++){
  344. blocksA.children[i].alpha = 0.5;
  345. }
  346. fractionA.alpha = 0;
  347. }
  348. if(!clickB && this.who=="B"){
  349. for(let i=0;i<=this.indice;i++){
  350. blocksB.children[i].alpha = 0.5;
  351. }
  352. fractionB.alpha = 0;
  353. }
  354. },
  355. func_clickSquare: function(){
  356. if(!clickA && this.who=="A" && this.indice!=sizeA-1){
  357. for(let i=0;i<sizeA;i++){
  358. blocksA.children[i].inputEnabled = false;
  359. if(i<=this.indice){
  360. blocksA.children[i].alpha = 1;
  361. }else{
  362. blocksA.children[i].alpha = 0.5;
  363. auxblqA.children[i].alpha = 0;
  364. }
  365. }
  366. labelA.alpha = 0;
  367. if(audioStatus){
  368. beepSound.play();
  369. }
  370. clickA = true;
  371. valueA = this.indice+1;
  372. fractionA.x = xA+(valueA*(blockW/sizeA))+40;
  373. separatorA.x = fractionA.x
  374. animateA = true;
  375. }
  376. if(!clickB && this.who=="B" && this.indice!=sizeB-1){
  377. for(let i=0;i<sizeB;i++){
  378. blocksB.children[i].inputEnabled = false;
  379. if(i<=this.indice){
  380. blocksB.children[i].alpha = 1;
  381. }else{
  382. blocksB.children[i].alpha = 0.5;
  383. auxblqB.children[i].alpha = 0;
  384. }
  385. }
  386. labelB.alpha = 0;
  387. if(audioStatus){
  388. beepSound.play();
  389. }
  390. clickB = true;
  391. valueB = this.indice+1;
  392. fractionB.x = xB+(valueB*(blockW/sizeB))+40;
  393. separatorB.x = fractionB.x
  394. animateB = true;
  395. }
  396. },
  397. func_postScore: function (){
  398. let abst = "numBlocksA:" + sizeA + ", valueA: " + valueA +", numBlocksB: " + sizeB + ", valueB: " + valueB;
  399. let hr = new XMLHttpRequest();
  400. // Create some variables we need to send to our PHP file
  401. let url = "php/save.php";
  402. let vars = "s_ip="+hip+"&s_name=" + username + "&s_lang=" + lang + "&s_game=" + levelShape + "&s_mode=" + levelType;
  403. vars += "&s_oper=Equal&s_leve=" + levelDifficulty + "&s_posi=" + levelPosition + "&s_resu=" + result + "&s_time=" + totalTime + "&s_deta=" + abst;
  404. hr.open("POST", url, true);
  405. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  406. hr.onreadystatechange = function() {
  407. if(debugMode) console.log(hr);
  408. if(hr.readyState == 4 && hr.status == 200) {
  409. let return_data = hr.responseText;
  410. if(debugMode) console.log(return_data);
  411. }
  412. }
  413. // Send the data to PHP now... and wait for response to update the status div
  414. hr.send(vars); // Actually execute the request
  415. if(debugMode) console.log("processing...");
  416. },
  417. //Calculation help functions
  418. func_getRndDivisor: function(number){ //Get random divisor for a number
  419. let div = []; //Divisors found
  420. let p = 0; //current dividor index
  421. for(let i=2; i<number;i++){
  422. if(number%i==0){
  423. div[p] = i;
  424. p++;
  425. }
  426. }
  427. let x = game.rnd.integerInRange(0,p-1);
  428. return div[x];
  429. },
  430. };