squareOne.js 39 KB

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