squareTwo.js 17 KB

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