1
0

squareOne.js 40 KB

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