circleOne.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. let gameCircleOne = {
  3. create: function(){},
  4. update: function(){},
  5. ---------------------------- end of phaser functions
  6. func_updateCounter: function(){},
  7. func_overCircle: function(){},
  8. func_outCircle: function(){},
  9. func_clickCircle: function(){},
  10. func_setPlace: function(){},
  11. func_postScore: function(){},
  12. func_viewHelp: function(){},
  13. func_checkOverlap: function(_,_){}
  14. //func_getRndDivisor: function(){}
  15. };
  16. */
  17. // Kid and Circle states, games 1 and 2
  18. let gameCircleOne = {
  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. detail="";
  26. // Sets background image
  27. game.add.image(0, 0, 'bgimage');
  28. // Calls function that loads navigation icons
  29. iconSettings["func_addButtons"](true,true,
  30. true,true,true,
  31. true,false,
  32. 'difficulty', this.func_viewHelp);
  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 styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  41. //Floor and road
  42. startX = 66; //Initial kid and place position
  43. if(levelOperator=='Minus') startX = 66+5*156;
  44. placeDistance = 156; //Distance between places
  45. blockSize = 60;
  46. for(let i=0;i<9;i++){
  47. game.add.image(i*100, 501, 'floor');
  48. }
  49. let road = game.add.image(47, 515, 'road');
  50. road.scale.setTo(1.01,0.94);
  51. if(levelType=='A'){
  52. road.inputEnabled = true;
  53. road.events.onInputDown.add(this.func_setPlace, {beep: beepSound}); //enabling input for tablets
  54. }
  55. for(let p=0;p<=5;p++){// Places
  56. let place = game.add.image(66+p*placeDistance, 526, 'place_a');
  57. place.anchor.setTo(0.5);
  58. place.scale.setTo(0.3);
  59. game.add.text(66+p*placeDistance, 560, p , stylePlace).anchor.setTo(0.5);
  60. }
  61. //Control variables
  62. clicked = false; //Air ballon positioned
  63. hideLabels = false; //Labels animations
  64. animate = false; //Start move animation
  65. checkCollide = false; //Check kid inside ballon's basket
  66. result = false; //Game is correct
  67. fly = false; //Start ballon fly animation
  68. flyCounter = 0; //Fly counter
  69. flyEnd = 140; //Fly end counter
  70. hasFigure = false; //If has level figure
  71. //trace
  72. trace = this.add.bitmapData(this.game.width, this.game.height);
  73. trace.addToWorld();
  74. trace.clear();
  75. //generator
  76. //Circles and fractions
  77. let maxBlocks = levelPosition+1; //Maximum blocks according to difficulty
  78. if(levelType=='B' || levelOperator=='Mixed') maxBlocks = 6;
  79. blocks = game.add.group(); //Fraction arrays
  80. numBlocks = game.rnd.integerInRange(levelPosition, maxBlocks); //Number of blocks
  81. curBlock = 0; //Actual index block
  82. blockDirection = []; //Directions right(plus), left (minus)
  83. blockDistance = []; //Displacement distance of the blocks
  84. blockLabel = game.add.group(); //Labels of the blocks
  85. blockSeparator = game.add.group(); //Separator of the labels
  86. blockAngle = []; //Angles of blocks
  87. blockTraceColor = []; //Trace colors
  88. endPosition = startX; //Ending position, accumulative
  89. //Game B exclusive variables
  90. balloonPlace = this.game.world.centerX; //Fixed place for ballon (game B)
  91. fractionClicked = false; //If clicked a fraction (game B)
  92. fractionIndex = -1; //Index of clicked fraction (game B)
  93. numPlus = game.rnd.integerInRange(1, numBlocks-1);
  94. for(let p=0;p<numBlocks;p++){
  95. let portion = game.rnd.integerInRange(1, levelDifficulty); //Portion of the circle, according to difficulty
  96. detail += portion+",";
  97. if(portion==levelDifficulty){
  98. hasFigure = true;
  99. }
  100. let direction = '';
  101. let lineColor = '';
  102. if(levelOperator=='Mixed'){
  103. if(p<=numPlus){
  104. direction = 'Right';
  105. lineColor = 0x31314e;
  106. }else{
  107. direction = 'Left';
  108. lineColor = 0xb30000;
  109. }
  110. /*let directions = ['Right','Left'];
  111. let rndIndex = game.rnd.integerInRange(0, 1);
  112. direction = directions[rndIndex];
  113. if(rndIndex==0) lineColor = 0x31314e;
  114. else lineColor = 0xb30000;*/
  115. }else if(levelOperator=='Plus'){
  116. direction = 'Right';
  117. lineColor = 0x31314e;
  118. }else if(levelOperator=='Minus'){
  119. direction = 'Left';
  120. lineColor = 0xb30000;
  121. }
  122. blockTraceColor[p] = lineColor;
  123. let block = game.add.graphics(startX, 490-p*blockSize);
  124. block.anchor.setTo(0.5,0.5);
  125. block.lineStyle(2, lineColor);
  126. block.beginFill(0xefeff5);
  127. if (direction == 'Right') block.scale.y *= -1;
  128. blockDirection[p] = direction;
  129. if(portion==1){
  130. block.drawCircle(0, 0, blockSize);
  131. blockDistance.push(placeDistance);
  132. blockAngle.push(360);
  133. if(levelLabel){
  134. let labelX = startX;
  135. if(levelOperator=='Minus') labelX -= 65;
  136. else labelX += 65;
  137. let label = game.add.text(labelX, 490-p*blockSize, portion , styleLabel);
  138. label.anchor.setTo(0.5, 0.5);
  139. blockLabel.add(label);
  140. }
  141. }else{
  142. let distance = 360/portion+5;
  143. block.arc(0, 0, blockSize/2, game.math.degToRad(distance), 0, true);
  144. blockDistance.push(Math.floor(placeDistance/portion));
  145. blockAngle.push(distance);
  146. if(levelLabel){
  147. let labelX = startX;
  148. if(levelOperator=='Minus') labelX -= 65;
  149. else labelX += 65;
  150. let separator = game.add.sprite(labelX, 485-p*blockSize, 'separator');
  151. separator.anchor.setTo(0.5, 0.5);
  152. blockSeparator.add(separator);
  153. let label = game.add.text(labelX, 488-p*blockSize, '1\n'+portion , styleLabel);
  154. label.anchor.setTo(0.5, 0.5);
  155. blockLabel.add(label);
  156. }
  157. }
  158. if(direction=='Right'){
  159. endPosition += Math.floor(placeDistance/portion);
  160. }else if(direction=='Left'){
  161. endPosition -= Math.floor(placeDistance/portion);
  162. }
  163. block.endFill();
  164. block.angle +=90;
  165. //If game is type B, (select fractions, adding event)
  166. if(levelType=='B'){
  167. block.alpha = 0.5;
  168. block.inputEnabled = true;
  169. block.input.useHandCursor = true;
  170. block.events.onInputDown.add(this.func_clickCircle, {indice: p});
  171. block.events.onInputOver.add(this.func_overCircle, {indice: p});
  172. block.events.onInputOut.add(this.func_outCircle, {indice: p});
  173. }
  174. blocks.add(block);
  175. }
  176. //Calculate next block
  177. if(blockDirection[curBlock]=='Right'){
  178. nextEnd = startX+blockDistance[curBlock];
  179. }else{
  180. nextEnd = startX-blockDistance[curBlock];
  181. }
  182. //If game is type B, selectiong a random balloon place
  183. if(levelType=='B'){
  184. balloonPlace = startX;
  185. endIndex = game.rnd.integerInRange(numPlus, numBlocks);
  186. for(let i=0;i<endIndex;i++){
  187. if(blockDirection[i]=='Right')
  188. balloonPlace += blockDistance[i];
  189. else if(blockDirection[i]=='Left')
  190. balloonPlace -= blockDistance[i];
  191. }
  192. if(balloonPlace<66 || balloonPlace>66+5*placeDistance || !hasFigure){
  193. game.state.start('gameCircleOne');
  194. }
  195. }
  196. //If end position is out of bounds, restart
  197. if (endPosition<66 || endPosition>66+3*260 || !hasFigure){
  198. game.state.start('gameCircleOne');
  199. }
  200. //kid
  201. kid_walk = game.add.sprite(startX, 495-numBlocks*blockSize, 'kid_walk');
  202. kid_walk.anchor.setTo(0.5, 0.8);
  203. kid_walk.scale.setTo(0.8);
  204. kid_walk.animations.add('right',[0,1,2,3,4,5,6,7,8,9,10,11]);
  205. kid_walk.animations.add('left',[23,22,21,20,19,18,17,16,15,14,13,12]);
  206. if(levelOperator=='Minus'){
  207. kid_walk.animations.play('left', 6, true);
  208. kid_walk.animations.stop();
  209. }
  210. //globo
  211. balloon = game.add.sprite(balloonPlace, 350, 'balloon');
  212. balloon.anchor.setTo(0.5, 0.5);
  213. balloon.alpha = 0.5;
  214. basket = game.add.sprite(balloonPlace, 472, 'balloon_basket');
  215. basket.anchor.setTo(0.5, 0.5);
  216. //ok and error images
  217. okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  218. okImg.anchor.setTo(0.5);
  219. okImg.alpha = 0;
  220. errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  221. errorImg.anchor.setTo(0.5);
  222. errorImg.alpha = 0;
  223. },
  224. update: function() {
  225. if (game.input.activePointer.isDown && !fly && !clicked){
  226. //Positionate balloon - Game A
  227. if(levelType=='A'){
  228. if(game.input.mousePointer.y>60){ //Dead zone for click
  229. balloon.x = game.input.mousePointer.x;
  230. balloon.alpha = 1;
  231. clicked = true;
  232. animate = true;
  233. if(audioStatus){
  234. beepSound.play();
  235. }
  236. if(blockDirection[curBlock]=='Right'){
  237. kid_walk.animations.play('right', 6, true);
  238. }else{
  239. kid_walk.animations.play('left', 6, true);
  240. }
  241. if(levelLabel){ //Hiding labels
  242. blockLabel.visible = false;
  243. blockSeparator.visible = false;
  244. }
  245. }
  246. }
  247. }
  248. if(!clicked){
  249. if(!fly){
  250. if(levelType=="A"){
  251. //Follow mouse
  252. if (game.physics.arcade.distanceToPointer(balloon, game.input.activePointer) > 8){
  253. balloon.x = game.input.mousePointer.x;
  254. basket.x = game.input.mousePointer.x;
  255. }
  256. }
  257. }
  258. }
  259. //Start animation
  260. if(animate){
  261. let color = '';
  262. if(blockDirection[curBlock]=='Right'){
  263. kid_walk.x+=2;
  264. color = 'rgba(0, 51, 153, 1)';
  265. }else if(blockDirection[curBlock]=='Left'){
  266. kid_walk.x-=2;
  267. color = 'rgba(179, 0, 0, 1)';
  268. }
  269. trace.rect(kid_walk.x, 526, 2, 2, color);
  270. for(let i=0;i<numBlocks;i++){ //Moving every block
  271. if(blockDirection[curBlock]=='Right'){
  272. blocks.children[i].x +=2;
  273. }else{
  274. blocks.children[i].x -=2;
  275. }
  276. }
  277. blockAngle[curBlock] -= 4.6;
  278. blocks.children[curBlock].clear();
  279. blocks.children[curBlock].lineStyle(2, blockTraceColor[curBlock]);
  280. blocks.children[curBlock].beginFill(0xefeff5);
  281. blocks.children[curBlock].arc(0, 0, blockSize/2, game.math.degToRad(blockAngle[curBlock]), 0, true);
  282. blocks.children[curBlock].endFill();
  283. if(blockDirection[curBlock]=='Right'){
  284. if(blocks.children[curBlock].x>=nextEnd){
  285. blocks.children[curBlock].visible = false;
  286. blocks.y += blockSize;
  287. kid_walk.y += blockSize;
  288. curBlock+=1;
  289. if(blockDirection[curBlock]=='Right'){
  290. nextEnd += blockDistance[curBlock];
  291. kid_walk.animations.play('right', 6, true);
  292. }else if(blockDirection[curBlock]=='Left'){
  293. nextEnd -= blockDistance[curBlock];
  294. kid_walk.animations.play('left', 6, true);
  295. }
  296. }
  297. }else{
  298. if(blocks.children[curBlock].x<=nextEnd){
  299. blocks.children[curBlock].visible = false;
  300. blocks.y += blockSize;
  301. kid_walk.y += blockSize;
  302. curBlock+=1;
  303. if(blockDirection[curBlock]=='Right'){
  304. nextEnd += blockDistance[curBlock];
  305. kid_walk.animations.play('right', 6, true);
  306. }else if(blockDirection[curBlock]=='Left'){
  307. nextEnd -= blockDistance[curBlock];
  308. kid_walk.animations.play('left', 6, true);
  309. }
  310. }
  311. }
  312. if(curBlock==numBlocks ){ //Final position
  313. animate= false;
  314. checkCollide = true;
  315. }
  316. }
  317. //Check if kid is inside the basket
  318. if(checkCollide){
  319. kid_walk.animations.stop();
  320. timer.stop();
  321. if(this.func_checkOverlap(basket,kid_walk)){
  322. if(kid_walk.frame < 12)
  323. kid_walk.frame = 24;
  324. else
  325. kid_walk.frame = 25;
  326. result = true;
  327. }else{
  328. result = false;
  329. }
  330. this.func_postScore();
  331. fly = true;
  332. checkCollide = false;
  333. }
  334. //Fly animation
  335. if(fly){
  336. if(flyCounter==0){
  337. if(result){
  338. if(audioStatus){
  339. okSound.play();
  340. }
  341. passedLevels++;
  342. if(debugMode) console.log("passedLevels = "+passedLevels);
  343. okImg.alpha = 1;
  344. }else{
  345. if(audioStatus){
  346. errorSound.play();
  347. }
  348. errorImg.alpha = 1;
  349. }
  350. }
  351. flyCounter += 1;
  352. balloon.y -= 2;
  353. basket.y -= 2;
  354. if(result){
  355. kid_walk.y -=2;
  356. }
  357. if(flyCounter>=flyEnd){
  358. if(result){
  359. levelMove = true;
  360. }else{
  361. levelMove = false;
  362. }
  363. game.state.start('map');
  364. }
  365. }
  366. },
  367. func_updateCounter: function() {
  368. totalTime++;
  369. },
  370. func_overCircle: function(){
  371. if(!clicked){
  372. for(let i=0;i<numBlocks;i++){
  373. if(i<=this.indice){
  374. blocks.children[i].alpha = 1;
  375. }else{
  376. blocks.children[i].alpha = 0.5;
  377. }
  378. }
  379. }
  380. },
  381. func_outCircle: function(){
  382. if(!clicked){
  383. for(let i=0;i<=this.indice;i++){
  384. blocks.children[i].alpha = 0.5;
  385. }
  386. }
  387. },
  388. func_clickCircle: function(){
  389. if(!clicked){
  390. let minusBlocks = 0;
  391. for(let i=0;i<numBlocks;i++){
  392. if(i<=this.indice){
  393. fractionIndex = this.indice;
  394. blocks.children[i].alpha = 1;
  395. }else{
  396. blocks.children[i].visible = false; //Delete unselected block
  397. minusBlocks +=1; //number of blocks to reduce
  398. kid_walk.y += blockSize; //Lowering kid
  399. }
  400. }
  401. numBlocks -= minusBlocks; //Final reduced blocks
  402. balloon.alpha = 1;
  403. clicked = true;
  404. animate = true;
  405. if(audioStatus){
  406. beepSound.play();
  407. }
  408. if(blockDirection[curBlock]=='Right'){
  409. kid_walk.animations.play('right', 6, true);
  410. }else{
  411. kid_walk.animations.play('left', 6, true);
  412. }
  413. if(levelLabel){ //Hiding labels
  414. blockLabel.visible = false;
  415. blockSeparator.visible = false;
  416. }
  417. }
  418. },
  419. func_setPlace: function(){
  420. if(!clicked){
  421. balloon.x = game.input.x;
  422. basket.x = game.input.x;
  423. balloon.alpha = 1;
  424. clicked = true;
  425. animate = true;
  426. if(audioStatus){
  427. beepSound.play();
  428. }
  429. if(blockDirection[curBlock]=='Right'){
  430. kid_walk.animations.play('right', 6, true);
  431. }else{
  432. kid_walk.animations.play('left', 6, true);
  433. }
  434. if(levelLabel){ //Hiding labels
  435. blockLabel.visible = false;
  436. blockSeparator.visible = false;
  437. }
  438. }
  439. },
  440. func_postScore: function (){
  441. let abst = "numCircles:" + numBlocks + ", valCircles: " + detail + " balloonX: " + basket.x + ", selIndex: " + fractionIndex;
  442. let lang_str = "pt_BR"; //TODO NAO esta pegando a lingua definida pelo usuario!
  443. let hr = new XMLHttpRequest();
  444. // Create some variables we need to send to our PHP file
  445. let url = "php/save.php";
  446. let vars = "s_ip=" + hip + "&s_name=" + username + "&s_lang=" + lang + "&s_game=" + levelShape + "&s_mode=" + levelType;
  447. vars += "&s_oper=" + levelOperator + "&s_leve=" + levelDifficulty + "&s_posi=" + levelPosition + "&s_resu=" + result + "&s_time=" + totalTime + "&s_deta=" + abst;
  448. hr.open("POST", url, true);
  449. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  450. hr.onreadystatechange = function() {
  451. if(debugMode) console.log(hr);
  452. if(hr.readyState == 4 && hr.status == 200) {
  453. let return_data = hr.responseText;
  454. if(debugMode) console.log(return_data);
  455. }
  456. }
  457. // Send the data to PHP now... and wait for response to update the status div
  458. hr.send(vars); // Actually execute the request
  459. if(debugMode) console.log("processing...");
  460. },
  461. func_viewHelp: function(){
  462. if(!clicked){
  463. let pointer;
  464. if(levelType=='A'){
  465. pointer = game.add.image(endPosition, 490, 'pointer');
  466. }else{
  467. pointer = game.add.image(blocks.children[endIndex-1].x, blocks.children[endIndex-1].y-blockSize/2, 'pointer');
  468. }
  469. pointer.anchor.setTo(0.5, 0);
  470. pointer.alpha = 0.7;
  471. }
  472. },
  473. func_checkOverlap: function (spriteA, spriteB) {
  474. let xA = spriteA.x;
  475. let xB = spriteB.x;
  476. if(Math.abs(xA-xB)>14){
  477. return false;
  478. }else{
  479. return true;
  480. }
  481. }
  482. };