squareOne.js 35 KB

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