circleOne.js 42 KB

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