squareOne.js 40 KB

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