squareOne.js 39 KB

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