1
0

squareTwo.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. // Fractions Comparison Square states
  2. /****************************** MENU ****************************/
  3. var menuSquareTwo={
  4. create: function() {
  5. // Creating sound variable
  6. var beepSound = game.add.audio('sound_beep');
  7. // Reading dictionary
  8. var words = game.cache.getJSON('dictionary');
  9. // Menu options
  10. //information label
  11. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  12. // Return to language button
  13. // Remove language icon ::Igor
  14. m_world = game.add.sprite(10, 10, 'about');
  15. m_world.inputEnabled = true;
  16. m_world.input.useHandCursor = true;
  17. m_world.events.onInputDown.add(showInfo);
  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 = 80; //Width of a stair
  35. var startStair = 240;
  36. var startSymbol = 150;
  37. var startSquare = (startSymbol/2)+startStair+stairWidth*5;
  38. var bplus = game.add.sprite(startSymbol, 300, 'equal');
  39. bplus.frame = 0;
  40. bplus.scale.setTo(0.7);
  41. bplus.anchor.setTo(0.5,0.5);
  42. //First stairs, 6 levels
  43. var stairsA = [];
  44. for(var i=1;i<=5;i++){
  45. //stair
  46. var x1 = startStair+(stairWidth*(i-1));
  47. var y1 = 100+maxHeight-i*stairHeight;
  48. var x2 = stairWidth;//x1 + 40;
  49. var y2 = stairHeight*i;//y1 + 24;
  50. stairsA[i] = game.add.graphics(0, 0);
  51. stairsA[i].lineStyle(1, 0xFFFFFF, 1);
  52. stairsA[i].beginFill(0x99b3ff);
  53. stairsA[i].drawRect(x1, y1, x2, y2);
  54. stairsA[i].endFill();
  55. //event
  56. stairsA[i].inputEnabled = true;
  57. stairsA[i].input.useHandCursor = true;
  58. stairsA[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, type: 'A' });
  59. stairsA[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  60. stairsA[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  61. //label
  62. var xl = x1+stairWidth/2; //x label
  63. var yl = y1+(stairHeight*i)/2; //y label
  64. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  65. label.anchor.setTo(0.5, 0.4);
  66. }
  67. var stairsB = [];
  68. for(var i=1;i<=5;i++){
  69. //stair
  70. var x1 = startStair+(stairWidth*(i-1));
  71. var y1 = 270+maxHeight-i*stairHeight;
  72. var x2 = stairWidth;//x1 + 40;
  73. var y2 = stairHeight*i;//y1 + 24;
  74. stairsB[i] = game.add.graphics(0, 0);
  75. stairsB[i].lineStyle(1, 0xFFFFFF, 1);
  76. stairsB[i].beginFill(0xff6666);
  77. stairsB[i].drawRect(x1, y1, x2, y2);
  78. stairsB[i].endFill();
  79. //event
  80. stairsB[i].inputEnabled = true;
  81. stairsB[i].input.useHandCursor = true;
  82. stairsB[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, type: 'B' });
  83. stairsB[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  84. stairsB[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  85. //label
  86. var xl = x1+stairWidth/2; //x label
  87. var yl = y1+(stairHeight*i)/2; //y label
  88. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  89. label.anchor.setTo(0.5, 0.4);
  90. }
  91. var stairsC = [];
  92. for(var i=1;i<=5;i++){
  93. //stair
  94. var x1 = startStair+(stairWidth*(i-1));
  95. var y1 = 440+maxHeight-i*stairHeight;
  96. var x2 = stairWidth;//x1 + 40;
  97. var y2 = stairHeight*i;//y1 + 24;
  98. stairsC[i] = game.add.graphics(0, 0);
  99. stairsC[i].lineStyle(1, 0xFFFFFF, 1);
  100. stairsC[i].beginFill(0xb366ff);
  101. stairsC[i].drawRect(x1, y1, x2, y2);
  102. stairsC[i].endFill();
  103. //event
  104. stairsC[i].inputEnabled = true;
  105. stairsC[i].input.useHandCursor = true;
  106. stairsC[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, type: 'C' });
  107. stairsC[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  108. stairsC[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  109. //label
  110. var xl = x1+stairWidth/2; //x label
  111. var yl = y1+(stairHeight*i)/2; //y label
  112. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  113. label.anchor.setTo(0.5, 0.4);
  114. }
  115. // ::Igor
  116. //this.beep.play();
  117. twoPosition = 0; //Map position
  118. twoMove = true; //Move no next point
  119. twoDifficulty = jogo.difficulty; //Number of difficulty (1 to 5)
  120. twoType = jogo.modo;
  121. game.state.start('mapSTwo');
  122. /// ::Igor
  123. },
  124. //Navigation functions,
  125. showOption: function(){
  126. m_info.text = this.message;
  127. },
  128. loadState: function(){
  129. this.beep.play();
  130. game.state.start(this.state);
  131. },
  132. //MapLoading function
  133. loadMap: function(){
  134. this.beep.play();
  135. twoPosition = 0; //Map position
  136. twoMove = true; //Move no next point
  137. twoDifficulty = this.difficulty; //Number of difficulty (1 to 5)
  138. twoType = this.type; //Operator of game
  139. if(twoPosition<5){
  140. game.state.start('mapSTwo');
  141. }else{
  142. game.state.start('unofinal');
  143. }
  144. }
  145. };
  146. /****************************** MAP ****************************/
  147. var mapSquareTwo={
  148. create: function() {
  149. // Creating sound variable
  150. beepSound = game.add.audio('sound_beep');
  151. // Reading dictionary
  152. var words = game.cache.getJSON('dictionary');
  153. // Background
  154. game.add.image(0, 40, 'bgmap');
  155. if(oneMenu){
  156. // Menu options
  157. //information label
  158. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  159. // Return to language button
  160. // Remove language icon ::Igor
  161. m_world = game.add.sprite(10, 10, 'about');
  162. m_world.inputEnabled = true;
  163. m_world.input.useHandCursor = true;
  164. m_world.events.onInputDown.add(showInfo);
  165. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  166. m_world.events.onInputOut.add(this.showOption, {message: ""});
  167. // Return to menu button
  168. m_list = game.add.sprite(60, 10, 'list');
  169. m_list.inputEnabled = true;
  170. m_list.input.useHandCursor = true;
  171. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  172. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  173. m_list.events.onInputOut.add(this.showOption, {message: ""});
  174. // Return to diffculty
  175. m_back = game.add.sprite(110, 10, 'back');
  176. m_back.inputEnabled = true;
  177. m_back.input.useHandCursor = true;
  178. m_back.events.onInputDown.add(this.loadState, {state: "menuSTwo", beep: beepSound});
  179. m_back.events.onInputOver.add(this.showOption, {message: words.menu_back});
  180. m_back.events.onInputOut.add(this.showOption, {message: ""});
  181. }
  182. // Styles for labels
  183. var stylePlace = { font: '26px Arial', fill: '#ffffff', align: 'center'};
  184. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  185. // Progress bar
  186. var percentText = onePosition*20;
  187. var percentBlocks = onePosition;
  188. for(var p=1;p<=percentBlocks;p++){
  189. var block = game.add.image(680+(p-1)*30, 10, 'block');
  190. block.scale.setTo(2, 1); //Scaling to double width
  191. }
  192. game.add.text(840, 10, percentText+'%', styleMenu);
  193. game.add.text(670, 10, words.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  194. game.add.image(680, 10, 'pgbar');
  195. //Road
  196. this.points = {
  197. 'x': [ 90, 204, 318, 432, 546, 660 ],
  198. 'y': [ 486, 422, 358, 294, 230, 166 ]
  199. };
  200. //Road
  201. this.points = {
  202. 'x': [ 90, 204, 318, 432, 546, 660 ],
  203. 'y': [ 486, 422, 358, 294, 230, 166 ]
  204. };
  205. //House
  206. var house = game.add.image(this.points.x[0], this.points.y[0], 'house');
  207. house.scale.setTo(0.7);
  208. house.anchor.setTo(0.7, 0.8);
  209. //School
  210. var school = game.add.image(this.points.x[5], this.points.y[5], 'school');
  211. school.scale.setTo(0.35);
  212. school.anchor.setTo(0.2, 0.7);
  213. //Trees and Rocks
  214. this.rocks = {
  215. 'x': [156, 275, 276, 441, 452, 590, 712],
  216. 'y': [309, 543, 259, 156, 419, 136, 316]
  217. }
  218. this.r_types = [1, 1, 2, 1, 2, 2, 2];
  219. for(var i=0; i<this.r_types.length; i++){
  220. if(this.r_types[i]==1){
  221. var sprite = game.add.image(this.rocks.x[i], this.rocks.y[i], 'rock');
  222. sprite.scale.setTo(0.32);
  223. sprite.anchor.setTo(0.5, 0.95);
  224. }else if(this.r_types[i]==2){
  225. var sprite = game.add.image(this.rocks.x[i], this.rocks.y[i], 'birch');
  226. sprite.scale.setTo(0.4);
  227. sprite.anchor.setTo(0.5, 0.95);
  228. }
  229. }
  230. this.trees = {
  231. 'x': [105, 214, 354, 364, 570, 600, 740, 779],
  232. 'y': [341, 219, 180, 520, 550, 392, 488, 286]
  233. }
  234. this.t_types = [2, 4, 3, 4, 1, 2, 4, 4];
  235. for(var i=0; i<this.t_types.length; i++){
  236. var sprite = game.add.image(this.trees.x[i], this.trees.y[i], 'tree'+this.t_types[i]);
  237. sprite.scale.setTo(0.6);
  238. sprite.anchor.setTo(0.5, 0.95);
  239. }
  240. // places
  241. for (var p = 1; p < this.points.x.length -1; p++){
  242. var place;
  243. if(p<twoPosition)
  244. place = game.add.image(this.points.x[p], this.points.y[p], 'place_b');
  245. else if (twoMove && p==twoPosition)
  246. place = game.add.image(this.points.x[p], this.points.y[p], 'place_b');
  247. else
  248. place = game.add.image(this.points.x[p], this.points.y[p], 'place_a');
  249. place.anchor.setTo(0.5, 0.5);
  250. place.scale.setTo(0.3);
  251. var sign = game.add.image(this.points.x[p]-20, this.points.y[p]-60, 'sign');
  252. sign.anchor.setTo(0.5, 1);
  253. sign.scale.setTo(0.4);
  254. if(p>0 && p<this.points.x.length-1){
  255. var text = game.add.text(this.points.x[p]-23, this.points.y[p]-84, p, stylePlace);
  256. text.anchor.setTo(0.35, 0.5);
  257. }
  258. }
  259. // Kid start position
  260. this.kid = game.add.sprite(this.points.x[twoPosition], this.points.y[twoPosition], 'kid_run');
  261. this.kid.anchor.setTo(0.5,1);
  262. this.kid.scale.setTo(0.5);
  263. game.physics.arcade.enable(this.kid);
  264. this.kid.animations.add('run');
  265. this.kid.animations.play('run', 6, true);
  266. // Delay to next level
  267. this.count = 0;
  268. this.wait = 60;
  269. },
  270. update: function() {
  271. // Wait before moving or staring a game
  272. this.count ++;
  273. if(this.count<=this.wait) return;
  274. // If movement is stopped or position is 5 (final), load game
  275. if(twoPosition==5){
  276. twoMove = false;
  277. }
  278. if(!twoMove){
  279. this.loadGame();
  280. }
  281. // If momevent is enabled, move to next point from actual
  282. if(twoMove){
  283. game.physics.arcade.moveToXY(
  284. this.kid,
  285. this.points.x[twoPosition+1],
  286. this.points.y[twoPosition+1],
  287. 100
  288. );
  289. // I tractor reached the end, stop movement
  290. if(Math.ceil(this.kid.x)==this.points.x[twoPosition+1] || Math.ceil(this.kid.y)==this.points.y[twoPosition+1]){
  291. twoMove=false;
  292. twoPosition += 1; //Update position
  293. }
  294. }
  295. },
  296. //Navigation functions
  297. showOption: function(){
  298. m_info.text = this.message;
  299. },
  300. loadState: function(){
  301. this.beep.play();
  302. game.state.start(this.state);
  303. },
  304. //MapLoading function
  305. loadGame: function(){
  306. beepSound.play();
  307. if(twoPosition<5){
  308. game.state.start('gameSTwo');
  309. }else{
  310. game.state.start('endSTwo');
  311. }
  312. }
  313. };
  314. /****************************** GAME ****************************/
  315. var sizeA, sizeB, valueA, valueB;
  316. var clickA, clickB, animateA, animateB, result, animate, cDelay, eDelay;
  317. var blocksA, blocksB, auxblqA, auxblqB;
  318. var labelA, fractionA, separatorA, labelB, fractionB, separatorB;
  319. var kid, kidDirection, equals, counter, endCounter;
  320. var xA, yA, xB, yB, blockW, blockH;
  321. var okImg, errorImg;
  322. var gameSquareTwo={
  323. create: function() {
  324. // ::Igor
  325. if (conta == true) {
  326. start[iterator] = Math.floor(Date.now()/1000);
  327. conta = false;
  328. }
  329. //timer
  330. totalTime = 0;
  331. timer = game.time.create(false);
  332. timer.loop(1000, this.updateCounter, this);
  333. timer.start();
  334. points = [2,4,6,8,9,10,12,14,15,16,18,20];
  335. // Creating sound variable
  336. beepSound = game.add.audio('sound_beep');
  337. okSound = game.add.audio('sound_ok');
  338. errorSound = game.add.audio('sound_error');
  339. // Reading dictionary
  340. var words = game.cache.getJSON('dictionary');
  341. // Background
  342. game.add.image(0, 0, 'bgimage');
  343. //Clouds
  344. game.add.image(300, 100, 'cloud');
  345. game.add.image(660, 80, 'cloud');
  346. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  347. // Styles for labels
  348. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  349. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  350. var styleFraction = { font: '20px Arial', fill: '#000080', align: 'center'};
  351. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  352. //Floor
  353. for(var i=0;i<9;i++){
  354. game.add.image(i*100, 501, 'floor');
  355. }
  356. //kid
  357. kid = game.add.sprite(100, 470, 'kid_walk');
  358. kid.anchor.setTo(0.5, 0.7);
  359. kid.scale.setTo(0.8);
  360. kid.animations.add('right',[0,1,2,3,4,5,6,7,8,9,10,11]);
  361. kid.animations.add('left',[23,22,21,20,19,18,17,16,15,14,13,12]);
  362. kidDirection = 'right';
  363. kid.animations.play('right', 6, true);
  364. //Control variables
  365. sizeA = 0; //Size of first block
  366. sizeB = 0; //Size of second block
  367. valueA = 0; //Number of clicked blocks for a
  368. valueB = 0; //Number of clicked blocks for b
  369. clickA = false; //If block A is clicked
  370. clickB = false; //If block B is clicked
  371. animateA = false; //Animate A selected blocks to position
  372. animateB = false; //Animate B selected blocks to position
  373. result = false; //Game is correct
  374. animate = null; //Final animation sequence
  375. //generator
  376. console.log("Diff " + twoDifficulty + ", ini " + ((twoDifficulty-1)*2+1) + ", end " + ((twoDifficulty-1)*2+3));
  377. var rPoint = game.rnd.integerInRange((twoDifficulty-1)*2+1,(twoDifficulty-1)*2+3);
  378. sizeA = points[rPoint];
  379. console.log("Rpoint " + rPoint + ", val " + sizeA);
  380. sizeB = this.getRndDivisor(sizeA);
  381. blockB = game.rnd.integerInRange(1, sizeB);
  382. blockA = (sizeA/sizeB) * blockB;
  383. console.log("SA " + sizeA + ", SB " + sizeB + ", BA " + blockA + ", BB " + blockB );
  384. //Blocks and fractions group
  385. blocksA = game.add.group(); //Main blocks A
  386. blocksB = game.add.group(); //Main blocks B
  387. auxblqA = game.add.group(); //Auxiliar blocks A
  388. auxblqB = game.add.group(); //Auxiliar blocks B
  389. //Creating blocks
  390. blockW = 400;
  391. blockH = 50;
  392. if(twoType!="C"){
  393. xA=230, yA=90;
  394. xB=xA, yB=yA+3*blockH+30;
  395. }else{
  396. xB=230, yB=90;
  397. xA=xB, yA=yB+3*blockH+30;
  398. }
  399. //Blocks A
  400. var widthA = blockW/sizeA;
  401. var lineColor = 0x1e2f2f;
  402. var fillColor = 0x83afaf;
  403. var fillColorS = 0xe0ebeb;
  404. for(var i=0; i<sizeA; i++){
  405. //console.log("Block A"+i+": x:"+(xA+i*widthA)+", y:"+yA);
  406. var block = game.add.graphics(xA+i*widthA, yA);
  407. block.anchor.setTo(0.5, 0.5);
  408. block.lineStyle(2, lineColor);
  409. block.beginFill(fillColor);
  410. block.drawRect(0, 0, widthA, blockH);
  411. block.alpha = 0.5;
  412. block.endFill();
  413. block.inputEnabled = true;
  414. block.input.useHandCursor = true;
  415. block.events.onInputDown.add(this.clickSquare, {who: 'A',indice: i});
  416. block.events.onInputOver.add(this.overSquare, {who: 'A',indice: i});
  417. block.events.onInputOut.add(this.outSquare, {who: 'A',indice: i});
  418. blocksA.add(block);
  419. //aux blocks
  420. var xAux = xA+i*widthA, yAux = yA+blockH+10;
  421. if(twoType == 'C') yAux = yA;
  422. var block = game.add.graphics(xAux, yAux );
  423. block.anchor.setTo(0.5, 0.5);
  424. block.lineStyle(1, lineColor);
  425. block.beginFill(fillColorS);
  426. block.drawRect(0, 0, widthA, blockH);
  427. if(twoType!='A') block.alpha = 0;
  428. else block.alpha = 0.2;
  429. auxblqA.add(block);
  430. }
  431. //label block A
  432. var labelX = xA+blockW+30;
  433. var labelY = yA+blockH/2;
  434. labelA = game.add.text(labelX, labelY, sizeA , styleFraction);
  435. labelA.anchor.setTo(0.5, 0.41);
  436. //label fraction
  437. labelX = xA+(blockA*widthA)+40;
  438. labelY = yA+blockH+34;
  439. fractionA = game.add.text(labelX, labelY, "0\n"+sizeA , styleFraction);
  440. fractionA.anchor.setTo(0.5, 0.41);
  441. separatorA = game.add.sprite(labelX, labelY, 'separator');
  442. separatorA.anchor.setTo(0.5, 0.5);
  443. fractionA.alpha = 0;
  444. separatorA.alpha = 0;
  445. //Blocks B
  446. var widthB = blockW/sizeB;
  447. lineColor = 0x260d0d;
  448. fillColor = 0xd27979;
  449. fillColorS = 0xf2d9d9;
  450. for(var i=0; i<sizeB; i++){
  451. var block = game.add.graphics(xB+i*widthB, yB);
  452. block.anchor.setTo(0.5, 0.5);
  453. block.lineStyle(2, lineColor);
  454. block.beginFill(fillColor);
  455. block.drawRect(0, 0, widthB, blockH);
  456. block.endFill();
  457. block.inputEnabled = true;
  458. block.input.useHandCursor = true;
  459. block.events.onInputDown.add(this.clickSquare, {who: 'B',indice: i});
  460. block.events.onInputOver.add(this.overSquare, {who: 'B',indice: i});
  461. block.events.onInputOut.add(this.outSquare, {who: 'B',indice: i});
  462. blocksB.add(block);
  463. //aux blocks
  464. var xAux = xB+i*widthB, yAux = yB+blockH+10;
  465. if(twoType == 'C') yAux = yB;
  466. var block = game.add.graphics(xAux, yAux);
  467. block.anchor.setTo(0.5, 0.5);
  468. block.lineStyle(1, lineColor);
  469. block.beginFill(fillColorS);
  470. block.drawRect(0, 0, widthB, blockH);
  471. if(twoType!='A') block.alpha = 0;
  472. else block.alpha = 0.2;
  473. auxblqB.add(block);
  474. }
  475. //label block B
  476. labelX = xA+blockW+30;
  477. labelY = yB+blockH/2;
  478. labelB = game.add.text(labelX, labelY, sizeB , styleFraction);
  479. labelB.anchor.setTo(0.5, 0.41);
  480. //label fraction
  481. labelX = xA+(blockB*widthB)+40;
  482. labelY = yB+blockH+34;
  483. fractionB = game.add.text(labelX, labelY, "0\n"+sizeB , styleFraction);
  484. fractionB.anchor.setTo(0.5, 0.41);
  485. separatorB = game.add.sprite(labelX, labelY, 'separator');
  486. separatorB.anchor.setTo(0.5, 0.5);
  487. fractionB.alpha = 0;
  488. separatorB.alpha = 0;
  489. //information label
  490. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  491. if(twoMenu){
  492. // Return to language button
  493. // Remove language icon ::Igor
  494. m_world = game.add.sprite(10, 10, 'about');
  495. m_world.inputEnabled = true;
  496. m_world.input.useHandCursor = true;
  497. m_world.events.onInputDown.add(showInfo);
  498. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  499. m_world.events.onInputOut.add(this.showOption, {message: ""});
  500. // Return to menu button
  501. m_list = game.add.sprite(60, 10, 'list');
  502. m_list.inputEnabled = true;
  503. m_list.input.useHandCursor = true;
  504. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  505. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  506. m_list.events.onInputOut.add(this.showOption, {message: ""});
  507. // Return to diffculty
  508. m_back = game.add.sprite(110, 10, 'back');
  509. m_back.inputEnabled = true;
  510. m_back.input.useHandCursor = true;
  511. m_back.events.onInputDown.add(this.loadState, {state: "menuSTwo", beep: beepSound});
  512. m_back.events.onInputOver.add(this.showOption, {message: words.menu_back});
  513. m_back.events.onInputOut.add(this.showOption, {message: ""});
  514. }
  515. //ok and error images
  516. okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  517. okImg.anchor.setTo(0.5);
  518. okImg.alpha = 0;
  519. errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  520. errorImg.anchor.setTo(0.5);
  521. errorImg.alpha = 0;
  522. counter = 0;
  523. endCounter = 100;
  524. cDelay = 0;
  525. eDelay = 60;
  526. },
  527. updateCounter: function() {
  528. totalTime++;
  529. },
  530. overSquare: function(){
  531. if(!clickA && this.who=="A"){
  532. for(var i=0;i<sizeA;i++){
  533. if(i<=this.indice){
  534. blocksA.children[i].alpha = 1;
  535. }else{
  536. blocksA.children[i].alpha = 0.5;
  537. }
  538. }
  539. fractionA.x = xA+((this.indice +1)*(blockW/sizeA))+40;
  540. fractionA.alpha = 1;
  541. fractionA.setText(this.indice +1);
  542. }
  543. if(!clickB && this.who=="B"){
  544. for(var i=0;i<sizeB;i++){
  545. if(i<=this.indice){
  546. blocksB.children[i].alpha = 1;
  547. }else{
  548. blocksB.children[i].alpha = 0.5;
  549. }
  550. }
  551. fractionB.x = xB+((this.indice +1)*(blockW/sizeB))+40;
  552. fractionB.alpha = 1;
  553. fractionB.setText(this.indice +1);
  554. }
  555. },
  556. outSquare: function(){
  557. if(!clickA && this.who=="A"){
  558. for(var i=0;i<=this.indice;i++){
  559. blocksA.children[i].alpha = 0.5;
  560. }
  561. fractionA.alpha = 0;
  562. }
  563. if(!clickB && this.who=="B"){
  564. for(var i=0;i<=this.indice;i++){
  565. blocksB.children[i].alpha = 0.5;
  566. }
  567. fractionB.alpha = 0;
  568. }
  569. },
  570. clickSquare: function(){
  571. if(!clickA && this.who=="A"){
  572. for(var i=0;i<sizeA;i++){
  573. blocksA.children[i].inputEnabled = false;
  574. if(i<=this.indice){
  575. blocksA.children[i].alpha = 1;
  576. }else{
  577. blocksA.children[i].alpha = 0.5;
  578. auxblqA.children[i].alpha = 0;
  579. }
  580. }
  581. labelA.alpha = 0;
  582. beepSound.play();
  583. clickA = true;
  584. valueA = this.indice+1;
  585. fractionA.x = xA+(valueA*(blockW/sizeA))+40;
  586. separatorA.x = fractionA.x
  587. animateA = true;
  588. }
  589. if(!clickB && this.who=="B"){
  590. for(var i=0;i<sizeB;i++){
  591. blocksB.children[i].inputEnabled = false;
  592. if(i<=this.indice){
  593. blocksB.children[i].alpha = 1;
  594. }else{
  595. blocksB.children[i].alpha = 0.5;
  596. auxblqB.children[i].alpha = 0;
  597. }
  598. }
  599. labelB.alpha = 0;
  600. beepSound.play();
  601. clickB = true;
  602. valueB = this.indice+1;
  603. fractionB.x = xB+(valueB*(blockW/sizeB))+40;
  604. separatorB.x = fractionB.x
  605. animateB = true;
  606. }
  607. },
  608. postScore: function (){
  609. // ::Igor
  610. var fi = 0;
  611. if (result == true) { // Correct student's result:
  612. hits[twoPosition - 1] ++;
  613. end[twoPosition - 1] = Math.floor(Date.now()/1000);
  614. conta = true;
  615. if (twoPosition == 4) {
  616. fi = 1;
  617. }
  618. } else { // Error student's result:
  619. errors[twoPosition - 1] ++;
  620. }
  621. iterator = twoPosition;
  622. sendResults(fi);
  623. /*var abst = "numBlocksA:"+sizeA+", valueA: " + valueA +", numBlocksB: " + sizeB + ", valueB: " + valueB;
  624. var hr = new XMLHttpRequest();
  625. // Create some variables we need to send to our PHP file
  626. var url = "resource/cn/save.php";
  627. var vars = "s_ip="+hip+"&s_name="+name+"&s_lang="+lang+"&s_game="+twoShape+"&s_mode="+twoType;
  628. vars += "&s_oper=Equal&s_leve="+twoDifficulty+"&s_posi="+twoPosition+"&s_resu="+result+"&s_time="+totalTime+"&s_deta="+abst;
  629. hr.open("POST", url, true);
  630. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  631. hr.onreadystatechange = function() {
  632. console.log(hr);
  633. if(hr.readyState == 4 && hr.status == 200) {
  634. var return_data = hr.responseText;
  635. console.log(return_data);
  636. }
  637. }
  638. // Send the data to PHP now... and wait for response to update the status div
  639. hr.send(vars); // Actually execute the request
  640. console.log("processing...");*/
  641. },
  642. update: function() {
  643. if(!(clickA && clickB) && !animate){
  644. if(kidDirection=='right'){
  645. kid.x += 1;
  646. if(kid.x>=800){
  647. kidDirection='left';
  648. kid.animations.play('left', 8, true);
  649. }
  650. }else{
  651. kid.x -= 1;
  652. if(kid.x<=100){
  653. kidDirection='right';
  654. kid.animations.play('right', 8, true);
  655. }
  656. }
  657. }
  658. if(animateA){ //If clicked A only, animate
  659. for(var i=0;i<valueA;i++){
  660. blocksA.children[i].y +=2;
  661. }
  662. if(blocksA.children[0].y>=auxblqA.children[0].y){
  663. animateA = false;
  664. fractionA.alpha = 1;
  665. fractionA.setText(valueA+"\n"+sizeA);
  666. separatorA.alpha = 1;
  667. }
  668. }
  669. if(animateB){ //If clicked B only, animate
  670. for(var i=0;i<valueB;i++){
  671. blocksB.children[i].y +=2;
  672. }
  673. if(blocksB.children[0].y>=auxblqB.children[0].y){
  674. animateB = false;
  675. fractionB.alpha = 1;
  676. fractionB.setText(valueB+"\n"+sizeB);
  677. separatorB.alpha = 1;
  678. }
  679. }
  680. if(clickA && clickB && !this.animate){
  681. //Check result
  682. timer.stop();
  683. cDelay++;
  684. if(cDelay>=eDelay){
  685. if((valueA/sizeA) == (valueB/sizeB)){
  686. result = true;
  687. twoMove = true;
  688. okSound.play();
  689. okImg.alpha = 1;
  690. }else{
  691. result = false;
  692. twoMove = false;
  693. errorSound.play();
  694. kid.animations.stop();
  695. errorImg.alpha = 1;
  696. }
  697. this.postScore();
  698. clickA = false;
  699. clickB = false;
  700. animate = true;
  701. }
  702. }
  703. if(animate){
  704. counter++;
  705. if(result){
  706. kid.x += 2;
  707. kidDirection='right';
  708. kid.animations.play('right', 8, true);
  709. }
  710. if(counter>endCounter){
  711. game.state.start('mapSTwo');
  712. }
  713. }
  714. },
  715. //Navigation functions,
  716. showOption: function(){
  717. m_info.text = this.message;
  718. },
  719. loadState: function(){
  720. this.beep.play();
  721. game.state.start(this.state);
  722. },
  723. //MapLoading function
  724. loadGame: function(){
  725. beepSound.play();
  726. if(onePosition<5){
  727. game.state.start('gameSOne');
  728. }else{
  729. game.state.start('endSOne');
  730. }
  731. },
  732. //Calculation help functions
  733. getRndDivisor: function(number){ //Get random divisor for a number
  734. var div = []; //Divisors found
  735. var p = 0; //current dividor index
  736. for(var i=2; i<number;i++){
  737. if(number%i==0){
  738. div[p] = i;
  739. p++;
  740. }
  741. }
  742. var x = game.rnd.integerInRange(0,p-1);
  743. return div[x];
  744. }
  745. };
  746. /****************************** END ****************************/
  747. var endSquareTwo={
  748. create: function() {
  749. // Creating sound variable
  750. beepSound = game.add.audio('sound_beep');
  751. okSound = game.add.audio('sound_ok');
  752. errorSound = game.add.audio('sound_error');
  753. // Reading dictionary
  754. var words = game.cache.getJSON('dictionary');
  755. // Background
  756. game.add.image(0, 0, 'bgimage');
  757. //Clouds
  758. game.add.image(300, 100, 'cloud');
  759. game.add.image(660, 80, 'cloud');
  760. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  761. // Styles for labels
  762. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  763. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  764. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  765. //Floor
  766. for(var i=0;i<9;i++){
  767. game.add.image(i*100, 501, 'floor');
  768. }
  769. // Progress bar
  770. for(var p=1;p<=5;p++){
  771. var block = game.add.image(672+(p-1)*30, 10, 'block');
  772. block.scale.setTo(2, 1); //Scaling to double width
  773. }
  774. game.add.text(820, 10, '100%', styleMenu);
  775. game.add.text(660, 10, words.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  776. game.add.image(670, 10, 'pgbar');
  777. //School and trees
  778. game.add.sprite(600, 222 , 'school').scale.setTo(0.7);
  779. game.add.sprite(30, 280 , 'tree4');
  780. game.add.sprite(360, 250 , 'tree2');
  781. //kid
  782. this.kid = game.add.sprite(0, 460 , 'kid_run');
  783. this.kid.anchor.setTo(0.5,0.5);
  784. this.kid.scale.setTo(0.7);
  785. this.kid.animations.add('walk', [0,1,2,3,4,5,6,7,8,9,10,11]);
  786. this.kid.animations.play('walk', 6, true);
  787. },
  788. update: function() {
  789. if(this.kid.x<=700){
  790. this.kid.x += 2;
  791. }else{
  792. if(twoMenu){
  793. // REDIRECIONAR AQUI!! ::Igor
  794. if (redir == true) {
  795. this.kid.animations.stop();
  796. finish_redirect();
  797. redir = false;
  798. }
  799. }else{
  800. this.kid.animations.stop();
  801. }
  802. }
  803. },
  804. verPrincipal: function(){
  805. game.state.start('welcome');
  806. },
  807. verMenu: function(){
  808. if(twoMenu){
  809. game.state.start('menu');
  810. }
  811. }
  812. };