circleOne.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. // Kid and Circle states, games 1 and 2
  2. /****************************** MENU ****************************/
  3. var stairsPlus, stairsMinus, stairsMixed;
  4. var menuCircleOne={
  5. create: function() {
  6. // Creating sound variable
  7. var beepSound = game.add.audio('sound_beep');
  8. // Reading dictionary
  9. var words = game.cache.getJSON('dictionary');
  10. // Menu options
  11. //information label
  12. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  13. // Return to language button
  14. m_world = game.add.sprite(10, 10, 'world');
  15. m_world.inputEnabled = true;
  16. m_world.input.useHandCursor = true;
  17. m_world.events.onInputDown.add(this.loadState, {state: "boot", beep: beepSound});
  18. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  19. m_world.events.onInputOut.add(this.showOption, {message: ""});
  20. // Return to menu button
  21. m_list = game.add.sprite(60, 10, 'list');
  22. m_list.inputEnabled = true;
  23. m_list.input.useHandCursor = true;
  24. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  25. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  26. m_list.events.onInputOut.add(this.showOption, {message: ""});
  27. // Setting title
  28. var style = { font: '28px Arial', fill: '#00804d'};
  29. var title = game.add.text(860, 40, words.game_menu_title, style);
  30. title.anchor.setTo(1, 0.5);
  31. //Showing Games and Levels
  32. var maxHeight = 120; //Max height of a stair
  33. var stairHeight = 29; //height growth of a stair
  34. var stairWidth = 85; //Width of a stair
  35. var startStair = 240;
  36. var startSymbol = 150;
  37. var startCircle = (startSymbol/2)+startStair+stairWidth*5;
  38. //First stairs, plus, 5 levels, blue circle
  39. var blueCircle = game.add.graphics(startCircle, 195);
  40. blueCircle.anchor.setTo(0.5,0.5);
  41. blueCircle.lineStyle(2, 0x31314e);
  42. blueCircle.beginFill(0xefeff5);
  43. blueCircle.drawCircle(0, 0, 60);
  44. blueCircle.endFill();
  45. var r_arrow = game.add.sprite(startSymbol, 195, 'h_arrow');
  46. r_arrow.scale.setTo(0.7);
  47. r_arrow.anchor.setTo(0.5,0.5);
  48. stairsPlus = [];
  49. for(var i=1;i<=5;i++){
  50. //stair
  51. var x1 = startStair+(stairWidth*(i-1));
  52. var y1 = 135+maxHeight-i*stairHeight;
  53. var x2 = stairWidth;//x1 + 40;
  54. var y2 = stairHeight*i;//y1 + 24;
  55. stairsPlus[i] = game.add.graphics(0, 0);
  56. stairsPlus[i].lineStyle(1, 0xFFFFFF, 1);
  57. stairsPlus[i].beginFill(0x99b3ff);
  58. stairsPlus[i].drawRect(x1, y1, x2, y2);
  59. stairsPlus[i].endFill();
  60. //event
  61. stairsPlus[i].inputEnabled = true;
  62. stairsPlus[i].input.useHandCursor = true;
  63. stairsPlus[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, operator: 'Plus' });
  64. stairsPlus[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  65. stairsPlus[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  66. //label
  67. var xl = x1+stairWidth/2; //x label
  68. var yl = y1+(stairHeight*i)/2; //y label
  69. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  70. label.anchor.setTo(0.5, 0.4);
  71. }
  72. //Second stairs, minus, 5 levels, red circle
  73. var redCircle = game.add.graphics(startCircle, 350);
  74. redCircle.anchor.setTo(0.5,0.5);
  75. redCircle.lineStyle(2, 0xb30000);
  76. redCircle.beginFill(0xefeff5);
  77. redCircle.drawCircle(0, 0, 60);
  78. redCircle.endFill();
  79. var l_arrow = game.add.sprite(startSymbol, 350, 'h_arrow');
  80. l_arrow.scale.setTo(-0.7, 0.7);
  81. l_arrow.anchor.setTo(0.5,0.5);
  82. var stairsMinus = [];
  83. for(var i=1;i<=5;i++){
  84. //stair
  85. var x1 = startStair+(stairWidth*(i-1));
  86. var y1 = 285+maxHeight-i*stairHeight;
  87. var x2 = stairWidth;//x1 + 40;
  88. var y2 = stairHeight*i;//y1 + 24;
  89. stairsMinus[i] = game.add.graphics(0, 0);
  90. stairsMinus[i].lineStyle(1, 0xFFFFFF, 1);
  91. stairsMinus[i].beginFill(0xff6666);
  92. stairsMinus[i].drawRect(x1, y1, x2, y2);
  93. stairsMinus[i].endFill();
  94. //event
  95. stairsMinus[i].inputEnabled = true;
  96. stairsMinus[i].input.useHandCursor = true;
  97. stairsMinus[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, operator: 'Minus' });
  98. stairsMinus[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  99. stairsMinus[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  100. //label
  101. var xl = x1+stairWidth/2; //x label
  102. var yl = y1+(stairHeight*i)/2; //y label
  103. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  104. label.anchor.setTo(0.5, 0.4);
  105. }
  106. //Thrid stairs, mixed, 5 levels, two circles
  107. var bCircle = game.add.graphics(startCircle-30, 500);
  108. bCircle.anchor.setTo(0.5,0.5);
  109. bCircle.lineStyle(2, 0x31314e);
  110. bCircle.beginFill(0xefeff5);
  111. bCircle.drawCircle(0, 0, 60);
  112. bCircle.endFill();
  113. var rCircle = game.add.graphics(startCircle+40, 500);
  114. rCircle.anchor.setTo(0.5,0.5);
  115. rCircle.lineStyle(2, 0xb30000);
  116. rCircle.beginFill(0xefeff5);
  117. rCircle.drawCircle(0, 0, 60);
  118. rCircle.endFill();
  119. var d_arrow = game.add.sprite(startSymbol, 500, 'h_double');
  120. d_arrow.scale.setTo(0.7);
  121. d_arrow.anchor.setTo(0.5,0.5);
  122. var stairsMixed = [];
  123. for(var i=1;i<=5;i++){
  124. //stair
  125. var x1 = startStair+(stairWidth*(i-1));
  126. var y1 = 435+maxHeight-i*stairHeight;
  127. var x2 = stairWidth;//x1 + 40;
  128. var y2 = stairHeight*i;//y1 + 24;
  129. stairsMixed[i] = game.add.graphics(0, 0);
  130. stairsMixed[i].lineStyle(1, 0xFFFFFF, 1);
  131. stairsMixed[i].beginFill(0xb366ff);
  132. stairsMixed[i].drawRect(x1, y1, x2, y2);
  133. stairsMixed[i].endFill();
  134. //event
  135. stairsMixed[i].inputEnabled = true;
  136. stairsMixed[i].input.useHandCursor = true;
  137. stairsMixed[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, operator: 'Mixed' });
  138. stairsMixed[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  139. stairsMixed[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  140. //label
  141. var xl = x1+stairWidth/2; //x label
  142. var yl = y1+(stairHeight*i)/2; //y label
  143. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  144. label.anchor.setTo(0.5, 0.4);
  145. }
  146. },
  147. //Navigation functions,
  148. showOption: function(){
  149. m_info.text = this.message;
  150. },
  151. loadState: function(){
  152. this.beep.play();
  153. game.state.start(this.state);
  154. },
  155. //MapLoading function
  156. loadMap: function(){
  157. this.beep.play();
  158. onePosition = 0; //Map position
  159. oneMove = true; //Move no next point
  160. oneDifficulty = this.difficulty; //Number of difficulty (1 to 5)
  161. oneOperator = this.operator; //Operator of game
  162. if(onePosition<5){
  163. game.state.start('mapCOne');
  164. }else{
  165. game.state.start('unofinal');
  166. }
  167. }
  168. };
  169. /****************************** MAP ****************************/
  170. var mapCircleOne={
  171. create: function() {
  172. // Creating sound variable
  173. beepSound = game.add.audio('sound_beep');
  174. // Reading dictionary
  175. var words = game.cache.getJSON('dictionary');
  176. // Background
  177. game.add.image(0, 40, 'bgmap');
  178. if(oneMenu){ //IF not url game
  179. // Menu options
  180. //information label
  181. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  182. // Return to language button
  183. m_world = game.add.sprite(10, 10, 'world');
  184. m_world.inputEnabled = true;
  185. m_world.input.useHandCursor = true;
  186. m_world.events.onInputDown.add(this.loadState, {state: "boot", beep: beepSound});
  187. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  188. m_world.events.onInputOut.add(this.showOption, {message: ""});
  189. // Return to menu button
  190. m_list = game.add.sprite(60, 10, 'list');
  191. m_list.inputEnabled = true;
  192. m_list.input.useHandCursor = true;
  193. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  194. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  195. m_list.events.onInputOut.add(this.showOption, {message: ""});
  196. // Return to diffculty
  197. m_back = game.add.sprite(110, 10, 'back');
  198. m_back.inputEnabled = true;
  199. m_back.input.useHandCursor = true;
  200. m_back.events.onInputDown.add(this.loadState, {state: "menuCOne", beep: beepSound});
  201. m_back.events.onInputOver.add(this.showOption, {message: words.menu_back});
  202. m_back.events.onInputOut.add(this.showOption, {message: ""});
  203. }
  204. // Styles for labels
  205. var stylePlace = { font: '26px Arial', fill: '#ffffff', align: 'center'};
  206. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  207. // Progress bar
  208. var percentText = onePosition*25;
  209. var percentBlocks = onePosition;
  210. for(var p=0;p<percentBlocks;p++){
  211. var block = game.add.image(680+p*37, 10, 'block');
  212. block.scale.setTo(2.5, 1); //Scaling to double width
  213. }
  214. game.add.text(840, 10, percentText+'%', styleMenu);
  215. game.add.text(670, 10, words.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  216. game.add.image(680, 10, 'pgbar');
  217. //Road
  218. this.points = {
  219. 'x': [ 90, 204, 318, 432, 546, 660 ],
  220. 'y': [ 486, 422, 358, 294, 230, 166 ]
  221. };
  222. //House
  223. var house = game.add.image(this.points.x[0], this.points.y[0], 'house');
  224. house.scale.setTo(0.7);
  225. house.anchor.setTo(0.7, 0.8);
  226. //School
  227. var school = game.add.image(this.points.x[5], this.points.y[5], 'school');
  228. school.scale.setTo(0.35);
  229. school.anchor.setTo(0.2, 0.7);
  230. //Trees and Rocks
  231. this.rocks = {
  232. 'x': [156, 275, 276, 441, 452, 590, 712],
  233. 'y': [309, 543, 259, 156, 419, 136, 316]
  234. }
  235. this.r_types = [1, 1, 2, 1, 2, 2, 2];
  236. for(var i=0; i<this.r_types.length; i++){
  237. if(this.r_types[i]==1){
  238. var sprite = game.add.image(this.rocks.x[i], this.rocks.y[i], 'rock');
  239. sprite.scale.setTo(0.32);
  240. sprite.anchor.setTo(0.5, 0.95);
  241. }else if(this.r_types[i]==2){
  242. var sprite = game.add.image(this.rocks.x[i], this.rocks.y[i], 'birch');
  243. sprite.scale.setTo(0.4);
  244. sprite.anchor.setTo(0.5, 0.95);
  245. }
  246. }
  247. this.trees = {
  248. 'x': [105, 214, 354, 364, 570, 600, 740, 779],
  249. 'y': [341, 219, 180, 520, 550, 392, 488, 286]
  250. }
  251. this.t_types = [2, 4, 3, 4, 1, 2, 4, 4];
  252. for(var i=0; i<this.t_types.length; i++){
  253. var sprite = game.add.image(this.trees.x[i], this.trees.y[i], 'tree'+this.t_types[i]);
  254. sprite.scale.setTo(0.6);
  255. sprite.anchor.setTo(0.5, 0.95);
  256. }
  257. // places
  258. for (var p = 1; p < this.points.x.length -1; p++){
  259. var place;
  260. if(p<onePosition)
  261. place = game.add.image(this.points.x[p], this.points.y[p], 'place_b');
  262. else if (oneMove && p==onePosition)
  263. place = game.add.image(this.points.x[p], this.points.y[p], 'place_b');
  264. else
  265. place = game.add.image(this.points.x[p], this.points.y[p], 'place_a');
  266. place.anchor.setTo(0.5, 0.5);
  267. place.scale.setTo(0.3);
  268. var sign = game.add.image(this.points.x[p]-20, this.points.y[p]-60, 'sign');
  269. sign.anchor.setTo(0.5, 1);
  270. sign.scale.setTo(0.4);
  271. if(p>0 && p<this.points.x.length-1){
  272. var text = game.add.text(this.points.x[p]-23, this.points.y[p]-84, p, stylePlace);
  273. text.anchor.setTo(0.35, 0.5);
  274. }
  275. }
  276. // Kid start position
  277. this.kid = game.add.sprite(this.points.x[onePosition], this.points.y[onePosition], 'kid_run');
  278. this.kid.anchor.setTo(0.5,1);
  279. this.kid.scale.setTo(0.5);
  280. game.physics.arcade.enable(this.kid);
  281. this.kid.animations.add('run');
  282. this.kid.animations.play('run', 6, true);
  283. // Delay to next level
  284. this.count = 0;
  285. this.wait = 60;
  286. },
  287. update: function() {
  288. // Wait 2 seconds before moving or staring a game
  289. this.count ++;
  290. if(this.count<=this.wait) return;
  291. // If movement is stopped or position is 6 (final), load game
  292. if(onePosition==6){
  293. oneMove = false;
  294. }
  295. if(!oneMove){
  296. this.loadGame();
  297. }
  298. // If momevent is enabled, move to next point from actual
  299. if(oneMove){
  300. game.physics.arcade.moveToXY(
  301. this.kid,
  302. this.points.x[onePosition+1],
  303. this.points.y[onePosition+1],
  304. 100
  305. );
  306. // I kid reached the end, stop movement
  307. if(Math.ceil(this.kid.x)==this.points.x[onePosition+1] || Math.ceil(this.kid.y)==this.points.y[onePosition+1]){
  308. oneMove=false;
  309. onePosition += 1; //Update position
  310. }
  311. }
  312. },
  313. //Navigation functions,
  314. showOption: function(){
  315. m_info.text = this.message;
  316. },
  317. loadState: function(){
  318. this.beep.play();
  319. game.state.start(this.state);
  320. },
  321. //MapLoading function
  322. loadGame: function(){
  323. beepSound.play();
  324. if(onePosition<5){
  325. game.state.start('gameCOne');
  326. }else{
  327. game.state.start('endCOne');
  328. }
  329. }
  330. };
  331. /****************************** GAME ****************************/
  332. var okSound, errorSound; //sounds
  333. var startX; //start position
  334. var clicked, hideLabels, animate, checkCollide, result, hasFigure; //control variables
  335. var fly, flyCounter, flyend; //flyvariables
  336. var trace; //circle trace
  337. var kid_walk, balloon, basket;
  338. //Balloon and blocks control
  339. var maxBlocks, blockSize, blocks, numBlocks, curBlock, blockDirection, blockDistance, blockLabel, blockSeparator, blockAngle, blockTraceColor, endPosition;
  340. var balloonPlace, fractionClicked, fractionIndex, numPlus, endIndex;
  341. var okImg, errorImg;
  342. var detail;
  343. var gameCircleOne={
  344. create: function() {
  345. //timer
  346. totalTime = 0;
  347. timer = game.time.create(false);
  348. timer.loop(1000, this.updateCounter, this);
  349. timer.start();
  350. detail="";
  351. // Creating sound variable
  352. beepSound = game.add.audio('sound_beep');
  353. okSound = game.add.audio('sound_ok');
  354. errorSound = game.add.audio('sound_error');
  355. // Reading dictionary
  356. var words = game.cache.getJSON('dictionary');
  357. // Background
  358. game.add.image(0, 0, 'bgimage');
  359. //Clouds
  360. game.add.image(300, 100, 'cloud');
  361. game.add.image(660, 80, 'cloud');
  362. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  363. // Styles for labels
  364. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  365. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  366. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  367. //Floor and road
  368. startX = 66; //Initial kid and place position
  369. if(oneOperator=='Minus') startX = 66+5*156;
  370. placeDistance = 156; //Distance between places
  371. blockSize = 60;
  372. for(var i=0;i<9;i++){
  373. game.add.image(i*100, 501, 'floor');
  374. }
  375. var road = game.add.image(47, 515, 'road');
  376. road.scale.setTo(1.01,0.94);
  377. if(oneType=='A'){
  378. road.inputEnabled = true;
  379. road.events.onInputDown.add(this.setPlace, {beep: beepSound}); //enabling input for tablets
  380. }
  381. for(var p=0;p<=5;p++){// Places
  382. var place = game.add.image(66+p*placeDistance, 526, 'place_a');
  383. place.anchor.setTo(0.5);
  384. place.scale.setTo(0.3);
  385. game.add.text(66+p*placeDistance, 560, p , stylePlace).anchor.setTo(0.5);
  386. }
  387. //Control variables
  388. clicked = false; //Air ballon positioned
  389. hideLabels = false; //Labels animations
  390. animate = false; //Start move animation
  391. checkCollide = false; //Check kid inside ballon's basket
  392. result = false; //Game is correct
  393. fly = false; //Start ballon fly animation
  394. flyCounter = 0; //Fly counter
  395. flyEnd = 140; //Fly end counter
  396. hasFigure = false; //If has level figure
  397. //trace
  398. trace = this.add.bitmapData(this.game.width, this.game.height);
  399. trace.addToWorld();
  400. trace.clear();
  401. //generator
  402. //Circles and fractions
  403. var maxBlocks = onePosition+1; //Maximum blocks according to difficulty
  404. if(oneType=='B' || oneOperator=='Mixed') maxBlocks = 6;
  405. blocks = game.add.group(); //Fraction arrays
  406. numBlocks = game.rnd.integerInRange(onePosition, maxBlocks); //Number of blocks
  407. curBlock = 0; //Actual index block
  408. blockDirection = []; //Directions right(plus), left (minus)
  409. blockDistance = []; //Displacement distance of the blocks
  410. blockLabel = game.add.group(); //Labels of the blocks
  411. blockSeparator = game.add.group(); //Separator of the labels
  412. blockAngle = []; //Angles of blocks
  413. blockTraceColor = []; //Trace colors
  414. endPosition = startX; //Ending position, accumulative
  415. //Game B exclusive variables
  416. balloonPlace = this.game.world.centerX; //Fixed place for ballon (game B)
  417. fractionClicked = false; //If clicked a fraction (game B)
  418. fractionIndex = -1; //Index of clicked fraction (game B)
  419. numPlus = game.rnd.integerInRange(1, numBlocks-1);
  420. for(var p=0;p<numBlocks;p++){
  421. var portion = game.rnd.integerInRange(1, oneDifficulty); //Portion of the circle, according to difficulty
  422. detail += portion+",";
  423. if(portion==oneDifficulty){
  424. hasFigure = true;
  425. }
  426. var direction = '';
  427. var lineColor = '';
  428. if(oneOperator=='Mixed'){
  429. if(p<=numPlus){
  430. direction = 'Right';
  431. lineColor = 0x31314e;
  432. }else{
  433. direction = 'Left';
  434. lineColor = 0xb30000;
  435. }
  436. /*var directions = ['Right','Left'];
  437. var rndIndex = game.rnd.integerInRange(0, 1);
  438. direction = directions[rndIndex];
  439. if(rndIndex==0) lineColor = 0x31314e;
  440. else lineColor = 0xb30000;*/
  441. }else if(oneOperator=='Plus'){
  442. direction = 'Right';
  443. lineColor = 0x31314e;
  444. }else if(oneOperator=='Minus'){
  445. direction = 'Left';
  446. lineColor = 0xb30000;
  447. }
  448. blockTraceColor[p] = lineColor;
  449. var block = game.add.graphics(startX, 490-p*blockSize);
  450. block.anchor.setTo(0.5,0.5);
  451. block.lineStyle(2, lineColor);
  452. block.beginFill(0xefeff5);
  453. if (direction == 'Right') block.scale.y *= -1;
  454. blockDirection[p] = direction;
  455. if(portion==1){
  456. block.drawCircle(0, 0, blockSize);
  457. blockDistance.push(placeDistance);
  458. blockAngle.push(360);
  459. if(oneLabel){
  460. var labelX = startX;
  461. if(oneOperator=='Minus') labelX -= 65;
  462. else labelX += 65;
  463. var label = game.add.text(labelX, 490-p*blockSize, portion , styleLabel);
  464. label.anchor.setTo(0.5, 0.5);
  465. blockLabel.add(label);
  466. }
  467. }else{
  468. var distance = 360/portion+5;
  469. block.arc(0, 0, blockSize/2, game.math.degToRad(distance), 0, true);
  470. blockDistance.push(Math.floor(placeDistance/portion));
  471. blockAngle.push(distance);
  472. if(oneLabel){
  473. var labelX = startX;
  474. if(oneOperator=='Minus') labelX -= 65;
  475. else labelX += 65;
  476. var separator = game.add.sprite(labelX, 485-p*blockSize, 'separator');
  477. separator.anchor.setTo(0.5, 0.5);
  478. blockSeparator.add(separator);
  479. var label = game.add.text(labelX, 488-p*blockSize, '1\n'+portion , styleLabel);
  480. label.anchor.setTo(0.5, 0.5);
  481. blockLabel.add(label);
  482. }
  483. }
  484. if(direction=='Right'){
  485. endPosition += Math.floor(placeDistance/portion);
  486. }else if(direction=='Left'){
  487. endPosition -= Math.floor(placeDistance/portion);
  488. }
  489. block.endFill();
  490. block.angle +=90;
  491. //If game is type B, (select fractions, adding event)
  492. if(oneType=='B'){
  493. block.alpha = 0.5;
  494. block.inputEnabled = true;
  495. block.input.useHandCursor = true;
  496. block.events.onInputDown.add(this.clickCircle, {indice: p});
  497. block.events.onInputOver.add(this.overCircle, {indice: p});
  498. block.events.onInputOut.add(this.outCircle, {indice: p});
  499. }
  500. blocks.add(block);
  501. }
  502. //Calculate next block
  503. if(blockDirection[curBlock]=='Right'){
  504. nextEnd = startX+blockDistance[curBlock];
  505. }else{
  506. nextEnd = startX-blockDistance[curBlock];
  507. }
  508. //If game is type B, selectiong a random balloon place
  509. if(oneType=='B'){
  510. balloonPlace = startX;
  511. endIndex = game.rnd.integerInRange(numPlus, numBlocks);
  512. for(var i=0;i<endIndex;i++){
  513. if(blockDirection[i]=='Right')
  514. balloonPlace += blockDistance[i];
  515. else if(blockDirection[i]=='Left')
  516. balloonPlace -= blockDistance[i];
  517. }
  518. if(balloonPlace<66 || balloonPlace>66+5*placeDistance || !hasFigure){
  519. game.state.start('gameCOne');
  520. }
  521. }
  522. //If end position is out of bounds, restart
  523. if (endPosition<66 || endPosition>66+3*260 || !hasFigure){
  524. game.state.start('gameCOne');
  525. }
  526. //kid
  527. kid_walk = game.add.sprite(startX, 495-numBlocks*blockSize, 'kid_walk');
  528. kid_walk.anchor.setTo(0.5, 0.8);
  529. kid_walk.scale.setTo(0.8);
  530. kid_walk.animations.add('right',[0,1,2,3,4,5,6,7,8,9,10,11]);
  531. kid_walk.animations.add('left',[23,22,21,20,19,18,17,16,15,14,13,12]);
  532. if(oneOperator=='Minus'){
  533. kid_walk.animations.play('left', 6, true);
  534. kid_walk.animations.stop();
  535. }
  536. //globo
  537. balloon = game.add.sprite(balloonPlace, 350, 'balloon');
  538. balloon.anchor.setTo(0.5, 0.5);
  539. balloon.alpha = 0.5;
  540. basket = game.add.sprite(balloonPlace, 472, 'balloon_basket');
  541. basket.anchor.setTo(0.5, 0.5);
  542. // Menu options
  543. //information label
  544. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  545. if(oneMenu){
  546. // Return to language button
  547. m_world = game.add.sprite(10, 10, 'world');
  548. m_world.inputEnabled = true;
  549. m_world.input.useHandCursor = true;
  550. m_world.events.onInputDown.add(this.loadState, {state: "boot", beep: beepSound});
  551. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  552. m_world.events.onInputOut.add(this.showOption, {message: ""});
  553. // Return to menu button
  554. m_list = game.add.sprite(60, 10, 'list');
  555. m_list.inputEnabled = true;
  556. m_list.input.useHandCursor = true;
  557. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  558. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  559. m_list.events.onInputOut.add(this.showOption, {message: ""});
  560. // Return to diffculty
  561. m_back = game.add.sprite(110, 10, 'back');
  562. m_back.inputEnabled = true;
  563. m_back.input.useHandCursor = true;
  564. m_back.events.onInputDown.add(this.loadState, {state: "menuCOne", beep: beepSound});
  565. m_back.events.onInputOver.add(this.showOption, {message: words.menu_back});
  566. m_back.events.onInputOut.add(this.showOption, {message: ""});
  567. }
  568. // Help button
  569. m_help = game.add.sprite(160, 10, 'help');
  570. m_help.inputEnabled = true;
  571. m_help.input.useHandCursor = true;
  572. m_help.events.onInputDown.add(this.viewHelp, {beep: this.beepSound});
  573. m_help.events.onInputOver.add(this.showOption, {message: words.menu_help});
  574. m_help.events.onInputOut.add(this.showOption, {message: ""});
  575. //ok and error images
  576. okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  577. okImg.anchor.setTo(0.5);
  578. okImg.alpha = 0;
  579. errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  580. errorImg.anchor.setTo(0.5);
  581. errorImg.alpha = 0;
  582. },
  583. updateCounter: function() {
  584. totalTime++;
  585. },
  586. overCircle: function(){
  587. if(!clicked){
  588. for(var i=0;i<numBlocks;i++){
  589. if(i<=this.indice){
  590. blocks.children[i].alpha = 1;
  591. }else{
  592. blocks.children[i].alpha = 0.5;
  593. }
  594. }
  595. }
  596. },
  597. outCircle: function(){
  598. if(!clicked){
  599. for(var i=0;i<=this.indice;i++){
  600. blocks.children[i].alpha = 0.5;
  601. }
  602. }
  603. },
  604. clickCircle: function(){
  605. if(!clicked){
  606. var minusBlocks = 0;
  607. for(var i=0;i<numBlocks;i++){
  608. if(i<=this.indice){
  609. fractionIndex = this.indice;
  610. blocks.children[i].alpha = 1;
  611. }else{
  612. blocks.children[i].visible = false; //Delete unselected block
  613. minusBlocks +=1; //number of blocks to reduce
  614. kid_walk.y += blockSize; //Lowering kid
  615. }
  616. }
  617. numBlocks -= minusBlocks; //Final reduced blocks
  618. balloon.alpha = 1;
  619. clicked = true;
  620. animate = true;
  621. beepSound.play();
  622. if(blockDirection[curBlock]=='Right'){
  623. kid_walk.animations.play('right', 6, true);
  624. }else{
  625. kid_walk.animations.play('left', 6, true);
  626. }
  627. if(oneLabel){ //Hiding labels
  628. blockLabel.visible = false;
  629. blockSeparator.visible = false;
  630. }
  631. }
  632. },
  633. setPlace: function(){
  634. if(!clicked){
  635. balloon.x = game.input.x;
  636. basket.x = game.input.x;
  637. balloon.alpha = 1;
  638. clicked = true;
  639. animate = true;
  640. beepSound.play();
  641. if(blockDirection[curBlock]=='Right'){
  642. kid_walk.animations.play('right', 6, true);
  643. }else{
  644. kid_walk.animations.play('left', 6, true);
  645. }
  646. if(oneLabel){ //Hiding labels
  647. blockLabel.visible = false;
  648. blockSeparator.visible = false;
  649. }
  650. }
  651. },
  652. postScore: function (){
  653. var abst = "numCircles:"+numBlocks+", valCircles: " + detail+" balloonX: " + basket.x + ", selIndex: " + fractionIndex;
  654. var hr = new XMLHttpRequest();
  655. // Create some variables we need to send to our PHP file
  656. var url = "resource/cn/save.php";
  657. var vars = "s_ip="+hip+"&s_name="+name+"&s_lang="+lang+"&s_game="+oneShape+"&s_mode="+oneType;
  658. vars += "&s_oper="+oneOperator+"&s_leve="+oneDifficulty+"&s_posi="+onePosition+"&s_resu="+result+"&s_time="+totalTime+"&s_deta="+abst;
  659. hr.open("POST", url, true);
  660. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  661. hr.onreadystatechange = function() {
  662. console.log(hr);
  663. if(hr.readyState == 4 && hr.status == 200) {
  664. var return_data = hr.responseText;
  665. console.log(return_data);
  666. }
  667. }
  668. // Send the data to PHP now... and wait for response to update the status div
  669. hr.send(vars); // Actually execute the request
  670. console.log("processing...");
  671. },
  672. update: function() {
  673. if (game.input.activePointer.isDown && !fly && !clicked){
  674. //Positionate balloon - Game A
  675. if(oneType=='A'){
  676. if(game.input.mousePointer.y>60){ //Dead zone for click
  677. balloon.x = game.input.mousePointer.x;
  678. balloon.alpha = 1;
  679. clicked = true;
  680. animate = true;
  681. beepSound.play();
  682. if(blockDirection[curBlock]=='Right'){
  683. kid_walk.animations.play('right', 6, true);
  684. }else{
  685. kid_walk.animations.play('left', 6, true);
  686. }
  687. if(oneLabel){ //Hiding labels
  688. blockLabel.visible = false;
  689. blockSeparator.visible = false;
  690. }
  691. }
  692. }
  693. }
  694. if(!clicked){
  695. if(!fly){
  696. if(oneType=="A"){
  697. //Follow mouse
  698. if (game.physics.arcade.distanceToPointer(balloon, game.input.activePointer) > 8){
  699. balloon.x = game.input.mousePointer.x;
  700. basket.x = game.input.mousePointer.x;
  701. }
  702. }
  703. }
  704. }
  705. //Start animation
  706. if(animate){
  707. var color = '';
  708. if(blockDirection[curBlock]=='Right'){
  709. kid_walk.x+=2;
  710. color = 'rgba(0, 51, 153, 1)';
  711. }else if(blockDirection[curBlock]=='Left'){
  712. kid_walk.x-=2;
  713. color = 'rgba(179, 0, 0, 1)';
  714. }
  715. trace.rect(kid_walk.x, 526, 2, 2, color);
  716. for(var i=0;i<numBlocks;i++){ //Moving every block
  717. if(blockDirection[curBlock]=='Right'){
  718. blocks.children[i].x +=2;
  719. }else{
  720. blocks.children[i].x -=2;
  721. }
  722. }
  723. blockAngle[curBlock] -= 4.6;
  724. blocks.children[curBlock].clear();
  725. blocks.children[curBlock].lineStyle(2, blockTraceColor[curBlock]);
  726. blocks.children[curBlock].beginFill(0xefeff5);
  727. blocks.children[curBlock].arc(0, 0, blockSize/2, game.math.degToRad(blockAngle[curBlock]), 0, true);
  728. blocks.children[curBlock].endFill();
  729. if(blockDirection[curBlock]=='Right'){
  730. if(blocks.children[curBlock].x>=nextEnd){
  731. blocks.children[curBlock].visible = false;
  732. blocks.y += blockSize;
  733. kid_walk.y += blockSize;
  734. curBlock+=1;
  735. if(blockDirection[curBlock]=='Right'){
  736. nextEnd += blockDistance[curBlock];
  737. kid_walk.animations.play('right', 6, true);
  738. }else if(blockDirection[curBlock]=='Left'){
  739. nextEnd -= blockDistance[curBlock];
  740. kid_walk.animations.play('left', 6, true);
  741. }
  742. }
  743. }else{
  744. if(blocks.children[curBlock].x<=nextEnd){
  745. blocks.children[curBlock].visible = false;
  746. blocks.y += blockSize;
  747. kid_walk.y += blockSize;
  748. curBlock+=1;
  749. if(blockDirection[curBlock]=='Right'){
  750. nextEnd += blockDistance[curBlock];
  751. kid_walk.animations.play('right', 6, true);
  752. }else if(blockDirection[curBlock]=='Left'){
  753. nextEnd -= blockDistance[curBlock];
  754. kid_walk.animations.play('left', 6, true);
  755. }
  756. }
  757. }
  758. if(curBlock==numBlocks ){ //Final position
  759. animate= false;
  760. checkCollide = true;
  761. }
  762. }
  763. //Check if kid is inside the basket
  764. if(checkCollide){
  765. kid_walk.animations.stop();
  766. timer.stop();
  767. if(this.checkOverlap(basket,kid_walk)){
  768. result = true;
  769. }else{
  770. result = false;
  771. }
  772. this.postScore();
  773. fly = true;
  774. checkCollide = false;
  775. }
  776. //Fly animation
  777. if(fly){
  778. if(flyCounter==0){
  779. if(result){
  780. okSound.play();
  781. okImg.alpha = 1;
  782. }else{
  783. errorSound.play();
  784. errorImg.alpha = 1;
  785. }
  786. }
  787. flyCounter += 1;
  788. balloon.y -= 2;
  789. basket.y -= 2;
  790. if(result){
  791. kid_walk.y -=2;
  792. }
  793. if(flyCounter>=flyEnd){
  794. if(result){
  795. oneMove = true;
  796. }else{
  797. oneMove = false;
  798. }
  799. game.state.start('mapCOne');
  800. }
  801. }
  802. },
  803. //Navigation functions,
  804. showOption: function(){
  805. m_info.text = this.message;
  806. },
  807. loadState: function(){
  808. this.beep.play();
  809. game.state.start(this.state);
  810. },
  811. viewHelp: function(){
  812. if(!clicked){
  813. var pointer;
  814. if(oneType=='A'){
  815. var pointer = game.add.image(endPosition, 490, 'pointer');
  816. }else{
  817. var pointer = game.add.image(blocks.children[endIndex-1].x, blocks.children[endIndex-1].y-blockSize/2, 'pointer');
  818. }
  819. pointer.anchor.setTo(0.5, 0);
  820. pointer.alpha = 0.7;
  821. }
  822. },
  823. checkOverlap: function (spriteA, spriteB) {
  824. var xA = spriteA.x;
  825. var xB = spriteB.x;
  826. if(Math.abs(xA-xB)>25){
  827. return false;
  828. }else{
  829. return true;
  830. }
  831. }
  832. };
  833. /****************************** END ****************************/
  834. var endCircleOne={
  835. create: function() {
  836. // Creating sound variable
  837. beepSound = game.add.audio('sound_beep');
  838. okSound = game.add.audio('sound_ok');
  839. errorSound = game.add.audio('sound_error');
  840. // Reading dictionary
  841. var words = game.cache.getJSON('dictionary');
  842. // Background
  843. game.add.image(0, 0, 'bgimage');
  844. //Clouds
  845. game.add.image(300, 100, 'cloud');
  846. game.add.image(660, 80, 'cloud');
  847. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  848. // Styles for labels
  849. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  850. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  851. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  852. //Floor
  853. for(var i=0;i<9;i++){
  854. game.add.image(i*100, 501, 'floor');
  855. }
  856. // Progress bar
  857. for(var p=1;p<=5;p++){
  858. var block = game.add.image(672+(p-1)*30, 10, 'block');
  859. block.scale.setTo(2, 1); //Scaling to double width
  860. }
  861. game.add.text(820, 10, '100%', styleMenu);
  862. game.add.text(660, 10, words.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  863. game.add.image(670, 10, 'pgbar');
  864. //School and trees
  865. game.add.sprite(600, 222 , 'school').scale.setTo(0.7);
  866. game.add.sprite(30, 280 , 'tree4');
  867. game.add.sprite(360, 250 , 'tree2');
  868. //kid
  869. this.kid = game.add.sprite(0, -152 , 'kid_run');
  870. this.kid.anchor.setTo(0.5,0.5);
  871. this.kid.scale.setTo(0.7);
  872. var walk = this.kid.animations.add('walk', [0,1,2,3,4,5,6,7,8,9,10,11]);
  873. //globo
  874. this.balloon = game.add.sprite(0, -260, 'balloon');
  875. this.balloon.anchor.setTo(0.5,0.5);
  876. this.basket = game.add.sprite(0, -150, 'balloon_basket');
  877. this.basket.anchor.setTo(0.5,0.5);
  878. },
  879. update: function() {
  880. if(this.kid.y>=460){
  881. this.kid.animations.play('walk', 6, true);
  882. if(this.kid.x<=700){
  883. this.kid.x += 2;
  884. }else{
  885. if(oneMenu){
  886. game.state.start('menu');
  887. }else{
  888. this.kid.animations.stop();
  889. }
  890. }
  891. }else{
  892. this.balloon.y += 2;
  893. this.basket.y += 2;
  894. this.kid.y +=2;
  895. this.balloon.x += 1;
  896. this.basket.x += 1;
  897. this.kid.x +=1;
  898. }
  899. },
  900. verPrincipal: function(){
  901. game.state.start('welcome');
  902. },
  903. verMenu: function(){
  904. if(oneMenu){
  905. game.state.start('menu');
  906. }
  907. }
  908. };