squareOne.js 40 KB

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