squareOne.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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(660+(p-1)*30, 10, 'block');
  166. block.scale.setTo(2, 1); //Scaling to double width
  167. }
  168. game.add.text(820, 10, percentText+'%', styleMenu);
  169. game.add.text(650, 10, lang.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  170. game.add.image(660, 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. {
  543. var xPos = game.input.mousePointer.x;
  544. //set left limit to the arrow
  545. if (xPos < 250){
  546. xPos = 250;
  547. }
  548. arrow.x = xPos;
  549. }
  550. }
  551. }
  552. }
  553. //Start animation
  554. if(animate){
  555. if(blockDirection[curBlock]=='Right'){
  556. tractor.x+=2;
  557. }else if(blockDirection[curBlock]=='Left'){
  558. tractor.x-=2;
  559. }
  560. for(var i=0;i<numBlocks;i++){ //Moving every block
  561. if(blockDirection[curBlock]=='Right'){
  562. blocks.children[i].x +=2;
  563. }else{
  564. blocks.children[i].x -=2;
  565. }
  566. }
  567. var extra = 80-blockDistance[curBlock];
  568. if(blockDirection[curBlock]=='Right'){
  569. if(blocks.children[curBlock].x>=nextEnd+extra){
  570. blocks.children[curBlock].alpha = 0;
  571. blocks.y += 40;
  572. curBlock +=1;
  573. nextEnd += blockDistance[curBlock];
  574. for(var i=0; i<=floorIndex; i++ ){
  575. if(floorBlocks.children[i].x<(blocks.children[curBlock-1].x+blockDistance[curBlock-1])){
  576. floorBlocks.children[i].alpha = 0.2;
  577. curFloor = i;
  578. }
  579. }
  580. }
  581. }else if(blockDirection[curBlock]=='Left'){
  582. if(blocks.children[curBlock].x<=(nextEnd-extra)){
  583. blocks.children[curBlock].alpha = 0;
  584. blocks.y += 40;
  585. curBlock+=1;
  586. nextEnd -= blockDistance[curBlock];
  587. for(var i=0; i<=floorIndex; i++ ){
  588. if(floorBlocks.children[i].x>(blocks.children[curBlock-1].x-blockDistance[curBlock-1])){
  589. floorBlocks.children[i].alpha = 0.2;
  590. curFloor = i;
  591. }
  592. }
  593. }
  594. }
  595. if( curBlock>blockIndex || curFloor>=floorIndex){ //Final position
  596. animate= false;
  597. checkCollide = true;
  598. }
  599. }
  600. //Check if tractor has blocks left or floor holes
  601. if(checkCollide){
  602. tractor.animations.stop();
  603. timer.stop();
  604. //Check left blocks
  605. var resultBlock = true;
  606. for(var i=0; i<=blockIndex; i++){
  607. if(blocks.children[i].alpha==1) resultBlock = false;
  608. }
  609. //check floor Holes
  610. var resultFloor = true;
  611. for(var i=0; i<=floorIndex; i++){
  612. if(floorBlocks.children[i].alpha==1) resultFloor = false;
  613. }
  614. if(resultBlock && resultFloor){
  615. result = true;
  616. }else{
  617. result = false;
  618. }
  619. this.postScore();
  620. move = true;
  621. checkCollide = false;
  622. }
  623. //Continue moving animation
  624. if(move){
  625. if(moveCounter==0){
  626. if(result){
  627. tractor.animations.play('right', 6, true);
  628. if(audioStatus){
  629. okSound.play();
  630. }
  631. okImg.alpha = 1;
  632. }else{
  633. if(audioStatus){
  634. errorSound.play();
  635. }
  636. errorImg.alpha = 1;
  637. }
  638. }
  639. moveCounter += 1;
  640. if(result){
  641. if(oneOperator=='Minus'){
  642. tractor.x -=2;
  643. }else{
  644. tractor.x +=2;
  645. }
  646. }
  647. if(moveCounter>=moveEnd){
  648. if(result){
  649. oneMove = true;
  650. }else{
  651. oneMove = false;
  652. }
  653. game.state.start('mapSOne');
  654. }
  655. }
  656. },
  657. updateCounter: function() {
  658. totalTime++;
  659. },
  660. overSquare: function(){
  661. if(!clicked){
  662. if(oneType=="A"){
  663. for(var i=0;i<floorCount;i++){
  664. if(i<=this.indice){
  665. floorBlocks.children[i].alpha = 1;
  666. }else{
  667. floorBlocks.children[i].alpha = 0.5;
  668. }
  669. }
  670. floorIndex = this.indice;
  671. }else if(oneType=="B"){
  672. for(var i=0;i<numBlocks;i++){
  673. if(i<=this.indice){
  674. blocks.children[i].alpha = 1;
  675. }else{
  676. blocks.children[i].alpha = 0.5;
  677. }
  678. }
  679. blockIndex = this.indice;
  680. }
  681. }
  682. },
  683. outSquare: function(){
  684. if(!clicked){
  685. if(oneType=="A"){
  686. for(var i=0;i<floorCount;i++){
  687. floorBlocks.children[i].alpha = 0.5;
  688. }
  689. floorIndex = -1;
  690. }else if(oneType=="B"){
  691. for(var i=0;i<=this.indice;i++){
  692. blocks.children[i].alpha = 0.5;
  693. }
  694. blockIndex = -1;
  695. }
  696. }
  697. },
  698. clickSquare: function(){
  699. if(!clicked && !move){
  700. if(oneType=='A'){
  701. arrow.alpha = 1;
  702. clicked = true;
  703. animate = true;
  704. if(audioStatus){
  705. beepSound.play();
  706. }
  707. tractor.animations.play('right', 5, true);
  708. if(oneLabel){ //Hiding labels
  709. blockLabel.visible = false;
  710. blockSeparator.visible = false;
  711. }
  712. //cleaning path
  713. if(oneOperator=='Minus'){
  714. for(var i=0; i< floorCount; i++){
  715. if(i>floorIndex){
  716. floorBlocks.children[i].alpha = 0;
  717. }
  718. }
  719. }else{
  720. for(var i=0; i< floorCount; i++){
  721. if(i>floorIndex){
  722. floorBlocks.children[i].alpha = 0;
  723. }
  724. }
  725. }
  726. blockIndex = numBlocks - 1;
  727. }else if(oneType=='B'){ //Delete unselected blocks
  728. var minusBlocks = 0;
  729. for(var i=0;i<numBlocks;i++){
  730. if(i<=blockIndex){
  731. blocks.children[i].alpha = 1;
  732. }else{
  733. blocks.children[i].visible = false; //Delete unselected block
  734. minusBlocks +=1; //number of blocks to reduce
  735. }
  736. }
  737. numBlocks -= minusBlocks; //Final reduced blocks
  738. arrow.alpha = 1;
  739. clicked = true;
  740. animate = true;
  741. if(audioStatus){
  742. beepSound.play();
  743. }
  744. tractor.animations.play('right', 5, true);
  745. if(oneLabel){ //Hiding labels
  746. blockLabel.visible = false;
  747. blockSeparator.visible = false;
  748. }
  749. }
  750. }
  751. },
  752. postScore: function (){
  753. var lang_str = "pt_BR"; //TODO NAO esta pegando a lingua definida pelo usuario!
  754. var aux;
  755. // Get correct information about username and default language
  756. // Variables 'username' and 'lang' is define on: js/menu.js
  757. // Variable 'lang' has all the JSON content of 'assets/languages/pt_BR.json', 'lang.lang' defined in 'js/preMenu.js' (setLang())
  758. // TODO: nao descobri esquema para pegar 'lang' (como 'pt_BR' ou 'en_US'), 'lang' esta' com o dicinario de lingua (definido em 'assets/languages/pt_BR.json').
  759. if (name=='') name = username; //leo the correct is 'username'
  760. // assets/languages/pt_BR.json
  761. //DEBUG Testar 'lang'
  762. /*
  763. var contact = "";
  764. if (typeof lang === 'object') {
  765. //contact = JSON.parse(lang);
  766. for (var i in lang) // will enumerate values of 'assets/languages/pt_BR.json'
  767. contact += lang[i];
  768. // Finaliza com os valores de 'assets/languages/pt_BR.json': 'CARREGANDOFraçõesSELECIONE UM JOGODificuldadeNívelSELECINAR OPERAÇÃO E DIFICULDADEBom trabalho!Tente novamente!SELECIONAR IDIOMAMENU PRINCIPALVOLTARVER
  769. // SOLUÇÃOCírculosQuadriláteroslegendaMODOCOMSEMDIGITE SEU NOMEPRONTOOláVocê deve selecionar uma porção menor que o seu tamanho totalVocê esqueceu de digitar seu nomeÁUDIO'
  770. lang_str = contact; aux = 1;
  771. }
  772. else
  773. // if (lang === 'undefined') lang_str = "pt_BR";
  774. */
  775. var abst = "numBlocks:"+numBlocks+", valBlocks: " + detail+" blockIndex: " + blockIndex + ", floorIndex: " + floorIndex;
  776. var hr = new XMLHttpRequest();
  777. // Create some variables we need to send to our PHP file
  778. var url = "assets/cn/save.php";
  779. var vars = "s_ip="+hip+"&s_name=" + name+"&s_lang=" + lang_str + "&s_game=" + oneShape + "&s_mode=" + oneType;
  780. vars += "&s_oper=" + oneOperator + "&s_leve=" + oneDifficulty + "&s_posi=" + onePosition + "&s_resu=" + result + "&s_time=" + totalTime + "&s_deta=" + abst;
  781. //D alert('/js/squareOne.js: url=' + url + '; aux=' + aux + ', lang_str=' + lang_str + ', lang=' + lang); // + ', this.lang=" + this.lang
  782. //D /js/squareOne.js: url=assets/cn/save.php; aux=1, lang_str=CARREGANDOFraçõesSELECIONE UM JOGODificuldadeNívelSELECINAR OPERAÇÃO E DIFICULDADEBom trabalho!Tente novamente!SELECIONAR IDIOMAMENU PRINCIPALVOLTARVER
  783. //D SOLUÇÃOCírculosQuadriláteroslegendaMODOCOMSEMDIGITE SEU NOMEPRONTOOláVocê deve selecionar uma porção menor que o seu tamanho totalVocê esqueceu de digitar seu nomeÁUDIO, lang=[object Object]
  784. // Sobre nome do usuario:
  785. // * js/squareOne.js: name
  786. // * js/pt_BR.json: welcome="Ola'", insert_name="DIGITE SEU NOME"
  787. // * assets/cn/save.php : $play = $_REQUEST["s_name"];
  788. // * js/preMenu.js : insert_name, game.add.text(...), username = document.getElementById("name_id").value;
  789. // Pegar valor de PHP para JS: echo("<script language='javascript'>location.href='download.php?arquivo=$nome_arquivo&dir=$dir&id_exer=$id_exer'</script>");
  790. hr.open("POST", url, true);
  791. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  792. hr.onreadystatechange = function() {
  793. console.log(hr);
  794. if(hr.readyState == 4 && hr.status == 200) {
  795. var return_data = hr.responseText;
  796. console.log(return_data);
  797. }
  798. }
  799. // Send the data to PHP now... and wait for response to update the status div
  800. hr.send(vars); // Actually execute the request
  801. console.log("processing...");
  802. },
  803. viewHelp: function(){
  804. if(!clicked){
  805. var pointer;
  806. if(oneType=='A'){
  807. var pointer = game.add.image(endPosition, 490, 'pointer');
  808. }else{
  809. var pointer = game.add.image(blocks.children[endIndex-1].x, blocks.children[endIndex-1].y-blockSize/2, 'pointer');
  810. }
  811. pointer.anchor.setTo(0.5, 0);
  812. pointer.alpha = 0.7;
  813. }
  814. }
  815. };
  816. /****************************** END ****************************/
  817. var endSquareOne = {
  818. create: function() {
  819. // Creating sound variable
  820. this.beepSound = game.add.audio('sound_beep');
  821. this.okSound = game.add.audio('sound_ok');
  822. this.errorSound = game.add.audio('sound_error');
  823. // Background
  824. game.add.image(0, 0, 'bgimage');
  825. //Clouds
  826. game.add.image(300, 100, 'cloud');
  827. game.add.image(660, 80, 'cloud');
  828. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  829. // Styles for labels
  830. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  831. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  832. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  833. //Floor
  834. for(var i=0;i<9;i++){
  835. game.add.image(i*100, 501, 'floor');
  836. }
  837. // Progress bar
  838. for(var p=1;p<=5;p++){
  839. var block = game.add.image(660+(p-1)*30, 10, 'block');
  840. block.scale.setTo(2, 1); //Scaling to double width
  841. }
  842. game.add.text(820, 10, '100%', styleMenu);
  843. game.add.text(650, 10, lang.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  844. game.add.image(660, 10, 'pgbar');
  845. //Farm and trees
  846. game.add.sprite(650, 260 , 'farm').scale.setTo(1.1);
  847. game.add.sprite(30, 280 , 'tree4');
  848. game.add.sprite(360, 250 , 'tree2');
  849. //tractor
  850. this.tractor = game.add.sprite(0, 490 , 'tractor');
  851. this.tractor.anchor.setTo(0.5,0.5);
  852. this.tractor.scale.setTo(0.8);
  853. this.tractor.animations.add('right',[0,1,2,3,4]);
  854. this.tractor.animations.play('right', 5, true);
  855. },
  856. update: function() {
  857. if(this.tractor.x<=700){
  858. this.tractor.x += 2;
  859. }else{
  860. if(oneMenu){
  861. game.state.start('menu');
  862. }else{
  863. this.tractor.animations.stop();
  864. }
  865. }
  866. },
  867. verPrincipal: function(){
  868. game.state.start('welcome');
  869. },
  870. verMenu: function(){
  871. if(oneMenu){
  872. game.state.start('menu');
  873. }
  874. }
  875. };