squareOne.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. let gameSquareOne = {
  3. create: function(){},
  4. update: function(){},
  5. ---------------------------- end of phaser functions
  6. func_updateCounter: function(){},
  7. func_overSquare: function(){},
  8. func_outSquare: function(){},
  9. func_clickSquare: function(){},
  10. //func_setPlace: function(){},
  11. func_postScore: function(){},
  12. func_viewHelp: function(){},
  13. //func_checkOverlap: function(){}
  14. //func_getRndDivisor: function(){}
  15. };
  16. */
  17. // Tractor and Square states
  18. let gameSquareOne = {
  19. create: function() {
  20. //timer
  21. totalTime = 0;
  22. timer = game.time.create(false);
  23. timer.loop(1000, this.func_updateCounter, this);
  24. timer.start();
  25. detail="";
  26. // Background
  27. game.add.image(0, 0, 'bgimage');
  28. // Calls function that loads navigation icons
  29. iconSettings.func_addIcons(true,true,
  30. true,true,true,
  31. true,false,
  32. 'difficulty', this.func_viewHelp);
  33. // Clouds
  34. game.add.image(300, 100, 'cloud');
  35. game.add.image(660, 80, 'cloud');
  36. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  37. //Initial tractor and place position
  38. let startX = 170;
  39. if(sublevelType=='Minus') startX = 730;
  40. startX = startX; //Workaround for initial position inside update
  41. // Width and height of blocks and 'floor gaps'
  42. const blockWidth = 80;
  43. const blockHeight = 40;
  44. // Floor gaps
  45. for(let i=0;i<9;i++){
  46. game.add.image(i*100, 501, 'floor');
  47. }
  48. //Control variables
  49. clicked = false; //Floor blocks or apilled blocks clicked
  50. hideLabels = false; //Labels of blocks
  51. animate = false; //Start move animation
  52. checkCollide = false; //Check if tractor fon't any block left or floor hole
  53. result = false; //Game is correct
  54. move = false; //Continue tractor animation
  55. moveCounter = 0; //Move counter
  56. moveEnd = 140; //Move end counter
  57. //tractor
  58. let tractorAlign = -80;
  59. if(sublevelType=='Minus'){
  60. tractorAlign *= -1;
  61. }
  62. this.tractor = game.add.sprite(startX+tractorAlign, 445, 'tractor');
  63. this.tractor.anchor.setTo(0.5, 0.5);
  64. this.tractor.scale.setTo(0.8);
  65. this.tractor.animations.add('right',[0,1,2,3,4]);
  66. if(sublevelType=='Minus'){
  67. this.tractor.scale.x *= -1;
  68. }
  69. //generator
  70. //Blocks and fractions
  71. if(debugMode) console.log("pos " +levelPosition); // position in the game map
  72. maxBlocks = levelPosition+4; //Maximum blocks
  73. if(levelType=='B' || sublevelType=='Mixed') maxBlocks = 10;
  74. blocks = game.add.group(); //Fraction arrays (apilled)
  75. numBlocks = game.rnd.integerInRange(levelPosition+2, maxBlocks); //Number of blocks
  76. if(debugMode) console.log("num " + numBlocks+", min " + (levelPosition+2) + ", max " + maxBlocks);
  77. curBlock = 0; //Actual index block
  78. blockDirection = []; //Directions right(plus), left (minus)
  79. blockDistance = []; //Displacement distance of the blocks
  80. blockLabel = game.add.group(); //Labels of the blocks
  81. blockSeparator = game.add.group(); //Separator of the labels
  82. //blockAngle = []; //Angles of blocks
  83. //blockTraceColor = []; //Trace colors
  84. endPosition = startX; //Ending position, accumulative
  85. if(sublevelType=='Minus') endPosition -= blockWidth;
  86. else endPosition += blockWidth;
  87. //Game A exclusive variables
  88. floorBlocks = game.add.group(); //Selectable floor blocks
  89. floorIndex = -1; //Selected floor block
  90. floorCount = 8; //Number of floor blocks
  91. floorClicked = false; //If clicked portion of floor
  92. curFloor = -1;
  93. //Game B exclusive variables
  94. arrowPlace = startX; //Fixed place for help arrow
  95. if(sublevelType=='Minus') arrowPlace -= blockWidth;
  96. else arrowPlace += blockWidth;
  97. fractionClicked = false; //If clicked a fraction (game B)
  98. fractionIndex = -1; //Index of clicked fraction (game B)
  99. hasFigure = false;
  100. for(let p=0;p<numBlocks;p++){
  101. let portion = game.rnd.integerInRange(1, levelDifficulty); //Portion of the square, according to difficulty
  102. if(portion==3) detail+= "4,";
  103. else detail += portion+",";
  104. if(portion==levelDifficulty) hasFigure = true;
  105. let direction = '';
  106. let lineColor = '';
  107. if(sublevelType=='Plus'){
  108. direction = 'Right';
  109. lineColor = 0x31314e; //plus block: "black'
  110. }else if(sublevelType=='Minus'){
  111. direction = 'Left';
  112. lineColor = 0xb30000;//minus block : "red"
  113. }
  114. //blocks close to tractor
  115. let block = game.add.graphics(startX, 460-p*blockHeight);
  116. block.anchor.setTo(0.5, 0.5);
  117. block.lineStyle(2, lineColor);
  118. block.beginFill(0xefeff5);
  119. blockDirection[p] = direction;
  120. if(portion==1){
  121. block.drawRect(0, 0, blockWidth, blockHeight);
  122. blockDistance.push(blockWidth);
  123. //blockAngle.push(360);
  124. if(levelLabel){
  125. let labelX = startX;
  126. if(sublevelType=='Minus') labelX -= (15+blockWidth);
  127. else labelX += blockWidth+15;
  128. let label = game.add.text(labelX, 480-p*blockHeight, portion , textStyles.valueLabelBlue);
  129. label.anchor.setTo(0.5, 0.5);
  130. blockLabel.add(label);
  131. }
  132. }else{
  133. if(portion==3) portion = 4;
  134. let distance = blockWidth/portion;
  135. block.drawRect(0, 0, distance, blockHeight);
  136. blockDistance.push(distance);
  137. if(levelLabel){
  138. let labelX = startX;
  139. if(sublevelType=='Minus') labelX -= (15+distance);
  140. else labelX += 15+distance;
  141. let separator = game.add.sprite(labelX, 480-p*blockHeight, 'separator');
  142. separator.scale.setTo(0.6);
  143. separator.anchor.setTo(0.5, 0.5);
  144. blockSeparator.add(separator);
  145. let label = game.add.text(labelX, 483-p*blockHeight, '1\n'+portion , textStyles.valueLabelBlue2);
  146. label.anchor.setTo(0.5, 0.5);
  147. blockLabel.add(label);
  148. }
  149. }
  150. if(direction=='Right'){
  151. endPosition += Math.floor(blockWidth/portion);
  152. }else if(direction=='Left'){
  153. endPosition -= Math.floor(blockWidth/portion);
  154. block.scale.x *= -1;
  155. }
  156. block.endFill();
  157. //If game is type B, (select fractions, adding event)
  158. if(levelType=='B'){
  159. block.alpha = 0.5;
  160. block.inputEnabled = true;
  161. block.input.useHandCursor = true;
  162. block.events.onInputDown.add(this.func_clickSquare, {indice: p, tractor: this.tractor});
  163. block.events.onInputOver.add(this.func_overSquare, {indice: p});
  164. block.events.onInputOut.add(this.func_outSquare, {indice: p});
  165. }
  166. blocks.add(block);
  167. }
  168. //Calculate next block
  169. if(blockDirection[curBlock]=='Right'){
  170. nextEnd = startX+blockDistance[curBlock];
  171. }else{
  172. nextEnd = startX-blockDistance[curBlock];
  173. }
  174. //If end position is out of bounds, restart
  175. if(!hasFigure) game.state.start('gameSquareOne');
  176. if (sublevelType=='Plus' && (endPosition<(startX+blockWidth) || endPosition>(startX+8*blockWidth))){
  177. game.state.start('gameSquareOne');
  178. }else if (sublevelType=='Minus' && (endPosition>(startX) || endPosition<(startX-(8*blockWidth)))){
  179. game.state.start('gameSquareOne');
  180. }
  181. //If game is type B, selectiong a random block floor place
  182. if(levelType=='B'){
  183. let end = game.rnd.integerInRange(1, numBlocks);
  184. for(let i=0;i<end;i++){
  185. if(blockDirection[i]=='Right')
  186. arrowPlace += blockDistance[i];
  187. else if(blockDirection[i]=='Left')
  188. arrowPlace -= blockDistance[i];
  189. }
  190. }
  191. //Selectable floor
  192. floorCount = 8*levelDifficulty;
  193. let widFloor = blockWidth/levelDifficulty;
  194. if(levelDifficulty==3){
  195. floorCount = 8*4;
  196. widFloor = blockWidth/4;
  197. }
  198. for(let i = 0; i < floorCount; i++){
  199. let posX = startX;
  200. if(sublevelType=='Minus') posX -= (blockWidth + i*widFloor);
  201. else posX += (blockWidth + i*widFloor);
  202. if(levelType=='B'){
  203. if(sublevelType=='Minus'){
  204. if(posX<=arrowPlace){
  205. floorCount = i+1;
  206. floorIndex = i-1;
  207. break;
  208. }
  209. }else{
  210. if(posX>=arrowPlace){
  211. floorCount = i+1;
  212. floorIndex = i-1;
  213. break;
  214. }
  215. }
  216. }
  217. // blocks on the floor
  218. let block = game.add.graphics(posX, 500);
  219. block.anchor.setTo(0.5, 0);
  220. block.lineStyle(0.9, 0xffffff);
  221. block.beginFill(0xa8c0e6);
  222. block.drawRect(0, 0, widFloor, blockHeight);
  223. block.endFill();
  224. if(sublevelType=='Minus') block.scale.x *= -1;
  225. if(levelType=="A"){
  226. block.alpha = 0.5;
  227. block.inputEnabled = true;
  228. block.input.useHandCursor = true;
  229. block.events.onInputDown.add(this.func_clickSquare, {indice: i, tractor: this.tractor});
  230. block.events.onInputOver.add(this.func_overSquare, {indice: i});
  231. block.events.onInputOut.add(this.func_outSquare, {indice: i});
  232. }
  233. floorBlocks.add(block);
  234. }
  235. for(let i=0;i<=8;i++){
  236. let posX = startX;
  237. if(sublevelType=='Minus')posX -= ((9-i)*blockWidth);
  238. else posX+=((i+1)*blockWidth);
  239. game.add.text(posX, 560, i, textStyles.valueLabelBlue).anchor.setTo(0.5, 0.5);
  240. }
  241. //ok and error images
  242. okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  243. okImg.anchor.setTo(0.5);
  244. okImg.alpha = 0;
  245. errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  246. errorImg.anchor.setTo(0.5);
  247. errorImg.alpha = 0;
  248. //Help arrow
  249. arrow = game.add.sprite(this.arrowPlace, 480, 'down');
  250. arrow.anchor.setTo(0.5, 0.5);
  251. if(levelType=="B")
  252. arrow.alpha = 0;
  253. else if(levelType=="A")
  254. arrow.alpha = 0.5;
  255. },
  256. update: function() {
  257. if(!clicked){
  258. if(!move){
  259. if(levelType=='A'){
  260. //Follow mouse
  261. if (game.physics.arcade.distanceToPointer(arrow, game.input.activePointer) > 8 )
  262. {
  263. let xPos = game.input.mousePointer.x;
  264. //set left limit to the arrow
  265. if (xPos < 250){
  266. xPos = 250;
  267. }
  268. arrow.x = xPos;
  269. }
  270. }
  271. }
  272. }
  273. //Start animation
  274. if(animate){
  275. if(blockDirection[curBlock]=='Right'){
  276. this.tractor.x+=2;
  277. }else if(blockDirection[curBlock]=='Left'){
  278. this.tractor.x-=2;
  279. }
  280. for(let i=0;i<numBlocks;i++){ //Moving every block
  281. if(blockDirection[curBlock]=='Right'){
  282. blocks.children[i].x +=2;
  283. }else{
  284. blocks.children[i].x -=2;
  285. }
  286. }
  287. let extra = 80-blockDistance[curBlock];
  288. if(blockDirection[curBlock]=='Right'){
  289. if(blocks.children[curBlock].x>=nextEnd+extra){
  290. blocks.children[curBlock].alpha = 0;
  291. blocks.y += 40;
  292. curBlock +=1;
  293. nextEnd += blockDistance[curBlock];
  294. for(let i=0; i<=floorIndex; i++ ){
  295. if(floorBlocks.children[i].x<(blocks.children[curBlock-1].x+blockDistance[curBlock-1])){
  296. floorBlocks.children[i].alpha = 0.2;
  297. curFloor = i;
  298. }
  299. }
  300. }
  301. }else if(blockDirection[curBlock]=='Left'){
  302. if(blocks.children[curBlock].x<=(nextEnd-extra)){
  303. blocks.children[curBlock].alpha = 0;
  304. blocks.y += 40;
  305. curBlock+=1;
  306. nextEnd -= blockDistance[curBlock];
  307. for(let i=0; i<=floorIndex; i++ ){
  308. if(floorBlocks.children[i].x>(blocks.children[curBlock-1].x-blockDistance[curBlock-1])){
  309. floorBlocks.children[i].alpha = 0.2;
  310. curFloor = i;
  311. }
  312. }
  313. }
  314. }
  315. if( curBlock>blockIndex || curFloor>=floorIndex){ //Final position
  316. animate= false;
  317. checkCollide = true;
  318. }
  319. }
  320. //Check if tractor has blocks left or floor holes
  321. if(checkCollide){
  322. this.tractor.animations.stop();
  323. timer.stop();
  324. //Check left blocks
  325. let resultBlock = true;
  326. for(let i=0; i<=blockIndex; i++){
  327. if(blocks.children[i].alpha==1) resultBlock = false;
  328. }
  329. //check floor Holes
  330. let resultFloor = true;
  331. for(let i=0; i<=floorIndex; i++){
  332. if(floorBlocks.children[i].alpha==1) resultFloor = false;
  333. }
  334. if(resultBlock && resultFloor){
  335. result = true;
  336. }else{
  337. result = false;
  338. }
  339. this.func_postScore();
  340. move = true;
  341. checkCollide = false;
  342. }
  343. //Continue moving animation
  344. if(move){
  345. if(moveCounter==0){
  346. if(result){
  347. this.tractor.animations.play('right', 6, true);
  348. if(audioStatus) okSound.play();
  349. passedLevels++;
  350. if(debugMode) console.log("passedLevels = "+passedLevels);
  351. okImg.alpha = 1;
  352. }else{
  353. if(audioStatus) errorSound.play();
  354. errorImg.alpha = 1;
  355. }
  356. }
  357. moveCounter += 1;
  358. if(result){
  359. if(sublevelType=='Minus'){
  360. this.tractor.x -=2;
  361. }else{
  362. this.tractor.x +=2;
  363. }
  364. }
  365. if(moveCounter>=moveEnd){
  366. if(result){
  367. levelMove = true;
  368. }else{
  369. levelMove = false;
  370. }
  371. game.state.start('map');
  372. }
  373. }
  374. },
  375. func_updateCounter: function() {
  376. totalTime++;
  377. },
  378. func_overSquare: function(){
  379. if(!clicked){
  380. //on level type A
  381. if(levelType=="A"){
  382. for(let i=0;i<floorCount;i++){
  383. if(i<=this.indice){
  384. floorBlocks.children[i].alpha = 1;
  385. }else{
  386. floorBlocks.children[i].alpha = 0.5;
  387. }
  388. }
  389. floorIndex = this.indice;
  390. //on level type B
  391. }else if(levelType=="B"){
  392. for(let i=0;i<numBlocks;i++){
  393. if(i<=this.indice){
  394. blocks.children[i].alpha = 0.5;
  395. }else{
  396. blocks.children[i].alpha = 0.2;
  397. }
  398. }
  399. blockIndex = this.indice;
  400. }
  401. }
  402. },
  403. func_outSquare: function(){
  404. if(!clicked){
  405. //on level type A
  406. if(levelType=="A"){
  407. for(let i=0;i<floorCount;i++){
  408. floorBlocks.children[i].alpha = 0.5;
  409. }
  410. floorIndex = -1;
  411. //on level type B
  412. }else if(levelType=="B"){
  413. for(let i=0;i<numBlocks;i++){
  414. blocks.children[i].alpha = 0.5;
  415. }
  416. blockIndex = -1;
  417. }
  418. }
  419. },
  420. func_clickSquare: function(){
  421. if(!clicked && !move){
  422. //on level type A
  423. if(levelType=='A'){
  424. arrow.alpha = 1;
  425. clicked = true;
  426. animate = true;
  427. if(audioStatus) beepSound.play();
  428. this.tractor.animations.play('right', 5, true);
  429. if(levelLabel){ //Hiding labels
  430. blockLabel.visible = false;
  431. blockSeparator.visible = false;
  432. }
  433. //cleaning path
  434. if(sublevelType=='Minus'){
  435. for(let i=0; i< floorCount; i++){
  436. if(i>floorIndex){
  437. floorBlocks.children[i].alpha = 0;
  438. }
  439. }
  440. }else{
  441. for(let i=0; i< floorCount; i++){
  442. if(i>floorIndex){
  443. floorBlocks.children[i].alpha = 0;
  444. }
  445. }
  446. }
  447. blockIndex = numBlocks - 1;
  448. //on level type B
  449. }else if(levelType=='B'){ //Delete unselected blocks
  450. let minusBlocks = 0;
  451. for(let i=0;i<numBlocks;i++){
  452. if(i<=blockIndex){
  453. blocks.children[i].alpha = 1;
  454. }else{
  455. blocks.children[i].visible = false; //Delete unselected block
  456. minusBlocks +=1; //number of blocks to reduce
  457. }
  458. }
  459. numBlocks -= minusBlocks; //Final reduced blocks
  460. arrow.alpha = 0;
  461. clicked = true;
  462. animate = true;
  463. if(audioStatus) beepSound.play();
  464. this.tractor.animations.play('right', 5, true);
  465. if(levelLabel){ //Hiding labels
  466. blockLabel.visible = false;
  467. blockSeparator.visible = false;
  468. }
  469. }
  470. }
  471. },
  472. func_postScore: function (){
  473. // Get correct information about username and default language
  474. // Variables 'username' and 'lang' is define on: js/menu.js
  475. // Variable 'lang' has all the JSON content of 'assets/languages/pt_BR.json', 'lang.lang' defined in 'js/preMenu.js' (func_setLang())
  476. let abst = "numBlocks:" + numBlocks + ", valBlocks: " + detail + " blockIndex: " + blockIndex + ", floorIndex: " + floorIndex;
  477. let hr = new XMLHttpRequest();
  478. // Create some variables we need to send to our PHP file
  479. let url = "php/save.php";
  480. let vars = "s_ip=" + hip + "&s_name=" + username + "&s_lang=" + langString + "&s_game=" + levelShape + "&s_mode=" + levelType;
  481. vars += "&s_oper=" + sublevelType + "&s_leve=" + levelDifficulty + "&s_posi=" + levelPosition + "&s_resu=" + result + "&s_time=" + totalTime + "&s_deta=" + abst;
  482. // Sobre nome do usuario:
  483. // * js/squareOne.js: name
  484. // * js/pt_BR.json: welcome="Ola'", insert_name="DIGITE SEU NOME"
  485. // * php/save.php : $play = $_REQUEST["s_name"];
  486. // * js/preMenu.js : insert_name, game.add.text(...), username = document.getElementById("name_id").value;
  487. // Pegar valor de PHP para JS: echo("<script language='javascript'>location.href='download.php?arquivo=$nome_arquivo&dir=$dir&id_exer=$id_exer'</script>");
  488. hr.open("POST", url, true);
  489. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  490. hr.onreadystatechange = function() {
  491. if(debugMode) console.log(hr);
  492. if(hr.readyState == 4 && hr.status == 200) {
  493. let return_data = hr.responseText;
  494. if(debugMode) console.log(return_data);
  495. }
  496. }
  497. // Send the data to PHP now... and wait for response to update the status div
  498. hr.send(vars); // Actually execute the request
  499. if(debugMode) console.log("processing...");
  500. },
  501. func_viewHelp: function(){
  502. if(!clicked){
  503. let pointer;
  504. if(levelType=='A'){
  505. pointer = game.add.image(endPosition, 490, 'pointer');
  506. }else{
  507. pointer = game.add.image(blocks.children[endIndex-1].x, blocks.children[endIndex-1].y-blockSize/2, 'pointer');
  508. }
  509. pointer.anchor.setTo(0.5, 0);
  510. pointer.alpha = 0.7;
  511. }
  512. }
  513. };