squareOne.js 38 KB

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