circleOne.js 20 KB

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