squareOne.js 41 KB

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