1
0

squareOne.js 40 KB

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