squareOne.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. let gameSquareOne = {
  3. create: function(){},
  4. update: function(){},
  5. ---------------------------- end of phaser functions
  6. func_overSquare: function(){},
  7. func_outSquare: function(){},
  8. func_clickSquare: function(){},
  9. //func_setPlace: function(){},
  10. //func_checkOverlap: function(){}
  11. //func_getRndDivisor: function(){}
  12. func_viewHelp: function(){},
  13. func_updateCounter: function(){},
  14. func_postScore: function(){},
  15. };
  16. GAME LEVELS - SQUARE I & II: tractor level
  17. Name of game state : 'squareOne'
  18. Shape : square
  19. Character : tractor
  20. Theme : farm
  21. Concept : Player associates 'blocks carried by the tractor' and 'floor spaces to be filled by them'
  22. Represent fractions as : blocks
  23. # of different difficulties for each level : 3
  24. Levels can be : 'A' or 'B' (in variable 'levelType')
  25. A : Player can select # of 'floor blocks' (hole in the ground)
  26. Selects size of hole to be made in the ground (to fill with the blocks in front of the truck)
  27. B : Player can select # of 'stacked blocks' (in front of the truck)
  28. Selects number of blocks in front of the truck (to fill the hole on the ground)
  29. Sublevels can be : 'Plus' or 'Minus' (in variable 'sublevelType')
  30. Plus : addition of fractions
  31. Represented by : tractor going to the right (floor positions 0..8)
  32. Minus : subtraction of fractions
  33. Represented by: tractor going to the left (floor positions 8..0)
  34. */
  35. let gameSquareOne = {
  36. create: function () {
  37. // CONTROL VARIABLES
  38. self = this; // saves a reference to gameSquareOne : to be used in functions called by phaser events
  39. this.checkAnswer = false; // When true allows game to run 'check answer' code in update
  40. this.animate = false; // When true allows game to run 'tractor animation' code in update (turns animation of the moving tractor ON/OFF)
  41. this.animateEnding = false; // When true allows game to run 'tractor ending animation' code in update (turns 'ending' animation of the moving tractor ON/OFF)
  42. this.animateCount = 0; // A 'x' position counter used in the final tractor animation
  43. this.hasClicked = false; // Checks if player 'clicked' on a block
  44. this.result = false; // Checks player 'answer'
  45. let hasBaseDifficulty = false; // Checks if level is too easy for its difficulty
  46. this.divisorsList = ""; // Hold the divisors for each fraction on stacked blocks (created for func_postScore)
  47. this.DIREC_LEVEL = (sublevelType == 'Minus') ? -1 : 1; // Will be multiplied to values to easily change tractor direction when needed
  48. this.animationSṕeed = 2 * this.DIREC_LEVEL; // Distance in which the tractor moves in each iteration of the animation
  49. // GAME VARIABLES
  50. const defaultBlockWidth = 80; // Base block width
  51. const defaultBlockHeight = 40; // Base block height
  52. const startX = (sublevelType == 'Minus') ? 730 : 170; // Initial 'x' coordinate for the tractor and the blocks
  53. // BACKGROUND AND TRACTOR
  54. // Add background image
  55. game.add.image(0, 0, 'bgimage');
  56. // Add clouds
  57. game.add.image(300, 100, 'cloud');
  58. game.add.image(660, 80, 'cloud');
  59. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  60. // Add floor of grass
  61. for (let i = 0; i < 9; i++) { game.add.image(i * 100, 501, 'floor'); }
  62. // Calls function that loads navigation icons
  63. iconSettings.func_addIcons(true, true,
  64. true, true, true,
  65. true, false,
  66. 'difficulty', this.func_viewHelp);
  67. // Add tractor
  68. this.tractor = game.add.sprite(startX, 445, 'tractor');
  69. this.tractor.scale.setTo(0.8);
  70. if (sublevelType == 'Plus'){
  71. this.tractor.anchor.setTo(1, 0.5);
  72. this.tractor.animations.add('run', [0, 1, 2, 3, 4]);
  73. } else {
  74. this.tractor.anchor.setTo(0, 0.5);
  75. this.tractor.animations.add('run', [5, 6, 7, 8, 9]);
  76. this.tractor.frame = 5;
  77. }
  78. // FLOOR BLOCKS variables
  79. this.floor_blocks = game.add.group(); // Group of 'floor' blocks
  80. this.floor_numBlocks; // Total of 'floor' blocks of the level
  81. this.floor_correctIndex; // In levelType 'A' will save the index of the correct 'floor' block
  82. this.floor_index = -1; // In levelType 'A' will save the index of the selected 'floor' block
  83. this.floor_curIndex = 0;
  84. this.floor_correctPositionX = startX + defaultBlockWidth * this.DIREC_LEVEL; // x coodinate of correct block (accumulative)
  85. // STACKED BLOCKS variables
  86. this.stck_blocks = game.add.group(); // Group of 'stacked' blocks
  87. this.stck_numBlocks; // Total of 'stacked' blocks of the level
  88. this.stck_correctIndex; // In levelType 'B' will save the index of the correct 'stacked' block
  89. this.stck_index = -1; // In levelType 'B' will save the index of the selected 'stacked' block
  90. this.stck_curIndex = 0;
  91. this.stck_fractionLines = game.add.group(); // Group of sprites that serves as line in the middle of a fraction
  92. this.stck_blockLabels = game.add.group(); // Group of labels that display fraction on side of stacked blocks
  93. this.nextStartX;
  94. // CREATING STACKED BLOCKS
  95. // Maximum number of stacked blocks for the level
  96. const max = (levelType == 'B') ? 10 : mapPosition + 4;
  97. // Set total number of stacked blocks for the level
  98. this.stck_numBlocks = game.rnd.integerInRange(mapPosition + 2, max);
  99. if (debugMode) console.log("Stacked blocks: " + this.stck_numBlocks + " (min: " + (mapPosition + 2) + ", max: " + max + ")");
  100. let stck_finalPosition = startX + defaultBlockWidth * this.DIREC_LEVEL;
  101. for (let i = 0; i < this.stck_numBlocks; i++) {
  102. // Set divisor of fraction for the current block (depends on difficulty)
  103. let divisor = game.rnd.integerInRange(1, levelDifficulty);
  104. // will validate that level isnt too easy (has at least one '1/difficulty' fraction)
  105. if (divisor == levelDifficulty) hasBaseDifficulty = true;
  106. // Make sure valid divisors are 1, 2 and 4 (not 3)
  107. if (divisor == 3) divisor = 4;
  108. // Add this divisor to the list of divisors (for func_postScore)
  109. this.divisorsList += divisor + ",";
  110. // Set the width of the current block to a fraction of the default size
  111. const blockWidth = defaultBlockWidth / divisor;
  112. // Updates width of all stacked blocks lined horizontally
  113. stck_finalPosition += blockWidth * this.DIREC_LEVEL;
  114. // Create stacked block (close to tractor)
  115. const lineSize = 2;
  116. const lineColor = (sublevelType == 'Minus') ? colors.red : colors.darkBlue;
  117. const block = game.add.graphics(startX, 462 - i * (defaultBlockHeight - lineSize));
  118. block.anchor.setTo(0.5, 0.5);
  119. block.lineStyle(lineSize, lineColor);
  120. block.beginFill(colors.lightBlue);
  121. block.drawRect(0, 0, blockWidth - lineSize, defaultBlockHeight - lineSize);
  122. block.endFill();
  123. block.scale.x *= this.DIREC_LEVEL; // Important: this will also change the width of the block to a negative value if this.DIREC_LEVEL == -1
  124. // If game is type B, adding events to stacked blocks
  125. if (levelType == 'B') {
  126. block.alpha = 0.5;
  127. block.inputEnabled = true;
  128. block.input.useHandCursor = true;
  129. block.events.onInputDown.add(this.func_clickSquare);
  130. block.events.onInputOver.add(this.func_overSquare, { index: i });
  131. block.events.onInputOut.add(this.func_outSquare);
  132. }
  133. // Add current block to the group of stacked blocks
  134. this.stck_blocks.add(block);
  135. // If 'show labels' is turned on, create labels that display the fractions on the side of each block
  136. if (levelLabel) {
  137. const x = startX + (blockWidth + 15) * this.DIREC_LEVEL;
  138. let label;
  139. if (divisor == 1) {
  140. label = game.add.text(x, 483 - i * (defaultBlockHeight - lineSize), divisor, textStyles.valueLabelBlue1);
  141. } else {
  142. label = game.add.text(x, 484 - i * (defaultBlockHeight - lineSize), '1\n' + divisor, textStyles.valueLabelBlue3);
  143. // Sprite that serves as line in the middle of a fraction
  144. const fractionLine = game.add.sprite(x, 481 - i * (defaultBlockHeight - lineSize), 'fractionLine');
  145. fractionLine.scale.setTo(0.6);
  146. fractionLine.anchor.setTo(0.5, 0.5);
  147. this.stck_fractionLines.add(fractionLine);
  148. }
  149. label.anchor.setTo(0.5, 0.5);
  150. // Add current label to group of labels
  151. this.stck_blockLabels.add(label);
  152. }
  153. }
  154. // check for errors (level too easy for its difficulty or end position out of bounds)
  155. if ((!hasBaseDifficulty) ||
  156. (sublevelType == 'Plus' && (stck_finalPosition < (startX + defaultBlockWidth) || stck_finalPosition > (startX + 8 * defaultBlockWidth))) ||
  157. (sublevelType == 'Minus' && (stck_finalPosition < (startX - (8 * defaultBlockWidth)) || stck_finalPosition > (startX - defaultBlockWidth)))
  158. ) {
  159. // If any error is found restart the level
  160. game.state.start('gameSquareOne');
  161. }
  162. // will be used as a counter in update, adding in the width of each stacked block to check if the end matches the floor selected position
  163. this.nextStartX = startX + this.stck_blocks.children[0].width;
  164. // CREATING FLOOR BLOCKS
  165. // Make sure valid divisors are 1, 2 and 4 (not 3)
  166. const divisor = (levelDifficulty == 3) ? 4 : levelDifficulty;
  167. // Define the total number of floor blocks
  168. this.floor_numBlocks = 8 * divisor;
  169. // Define the with of each floor block
  170. const floor_blockWidth = defaultBlockWidth / divisor;
  171. // If game is type B, selectiong a random 'x' coordinate for the floor
  172. if (levelType == 'B') {
  173. this.stck_correctIndex = game.rnd.integerInRange(0, (this.stck_numBlocks - 1));
  174. for (let i = 0; i <= this.stck_correctIndex; i++) {
  175. this.floor_correctPositionX += this.stck_blocks.children[i].width;
  176. }
  177. }
  178. let flag = true;
  179. for (let i = 0; i < this.floor_numBlocks; i++) {
  180. // 'x' coordinate for floor block
  181. const x = startX + (defaultBlockWidth + i * floor_blockWidth) * this.DIREC_LEVEL;
  182. if ((flag) &&
  183. (levelType == 'A') &&
  184. ((sublevelType == 'Plus' && x >= stck_finalPosition) || (sublevelType == 'Minus' && x <= stck_finalPosition))
  185. ) {
  186. this.floor_correctIndex = i - 1; // Set index of correct floor block
  187. flag = false;
  188. }
  189. if ((levelType == 'B') &&
  190. ((sublevelType == 'Plus' && x >= this.floor_correctPositionX) || (sublevelType == 'Minus' && x <= this.floor_correctPositionX))
  191. ) {
  192. this.floor_numBlocks = i;
  193. break; // Leaves for loop
  194. }
  195. // Create floor block
  196. const lineSize = 0.9;
  197. const block = game.add.graphics(x, 462 + defaultBlockHeight - lineSize);
  198. block.anchor.setTo(0.5, 0);
  199. block.lineStyle(lineSize, colors.blueBckg);
  200. block.beginFill(colors.blueBckgLevel);
  201. block.drawRect(0, 0, floor_blockWidth - lineSize, defaultBlockHeight - lineSize);
  202. block.endFill();
  203. block.scale.x *= this.DIREC_LEVEL; // Important: this will also change the width of the block to a negative value if this.DIREC_LEVEL == -1
  204. // If game is type A, adding events to floor blocks
  205. if (levelType == "A") {
  206. block.alpha = 0.5;
  207. block.inputEnabled = true;
  208. block.input.useHandCursor = true;
  209. block.events.onInputDown.add(this.func_clickSquare);
  210. block.events.onInputOver.add(this.func_overSquare, { index: i });
  211. block.events.onInputOut.add(this.func_outSquare);
  212. }
  213. // Add current label to group of labels
  214. this.floor_blocks.add(block);
  215. }
  216. if (levelType == "A") this.floor_correctPositionX = stck_finalPosition;
  217. // Creates labels on the floor to display the numbers
  218. for (let i = 0; i <= 8; i++) {
  219. const x = (sublevelType == 'Minus') ? startX - ((9 - i) * defaultBlockWidth) : startX + ((1 + i) * defaultBlockWidth);
  220. game.add.text(x, 462 + defaultBlockHeight + 58, i, textStyles.valueLabelBlue1).anchor.setTo(0.5, 0.5);
  221. }
  222. /*
  223. // temporarily used for debug
  224. if (debugMode){
  225. if (levelType == 'A') {
  226. console.log("Type A",
  227. "\nStacked index:", // todos
  228. this.stck_numBlocks - 1, "[fixo]",
  229. "\nFloor index:", // correto / todos
  230. this.floor_correctIndex, "[CORRETO]", "/", this.floor_numBlocks - 1,
  231. "\nFloor 'x' position:", this.floor_correctPositionX,
  232. );
  233. } else {
  234. console.log("Type B",
  235. "\nStacked index:", // corretor / todos
  236. this.stck_correctIndex, "[CORRETO]", "/", this.stck_numBlocks - 1,
  237. "\nFloor index:", // todos
  238. this.floor_numBlocks - 1, "[fixo]",
  239. "\nFloor 'x' position:", this.floor_correctPositionX,
  240. );
  241. }
  242. }
  243. */
  244. // SELECTION ARROW
  245. this.arrow = game.add.sprite(startX + defaultBlockWidth * this.DIREC_LEVEL, 480, 'down');
  246. this.arrow.anchor.setTo(0.5, 0.5);
  247. this.arrow.alpha = 0.5;
  248. if (levelType == "B") this.arrow.visible = false;
  249. // OUTPUT ICONS
  250. // Ok image
  251. this.okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  252. this.okImg.anchor.setTo(0.5);
  253. this.okImg.visible = false;
  254. // Error image
  255. this.errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  256. this.errorImg.anchor.setTo(0.5);
  257. this.errorImg.visible = false;
  258. // TIMER
  259. // Set a timer for the current level (used in func_postScore)
  260. this.totalTime = 0;
  261. this.timer = game.time.create(false);
  262. this.timer.loop(1000, this.func_updateCounter, this);
  263. this.timer.start();
  264. },
  265. update: function () {
  266. // WAITING PLAYER SELECTION
  267. // Make the arrow on the floor follow the cursor 'x' position
  268. if (!this.hasClicked && !this.animateEnding && levelType == 'A') {
  269. // Checks distance between the cursor and the arrow
  270. if (game.physics.arcade.distanceToPointer(this.arrow, game.input.activePointer) > 8) {
  271. const x = game.input.mousePointer.x;
  272. this.arrow.x = (x < 250) ? 250 : x; // Limits the arrow left position to 250
  273. }
  274. }
  275. // AFTER PLAYER SELECTION
  276. // Starts tractor moving animation
  277. if (this.animate) {
  278. // Move 'tractor' to the side
  279. this.tractor.x += this.animationSṕeed;
  280. // Move 'stacked blocks' to the side
  281. for (let i = 0; i < this.stck_numBlocks; i++) { this.stck_blocks.children[i].x += this.animationSṕeed; }
  282. // extra distance (if needed) before getting to next floor block
  283. const extra = (80 - this.stck_blocks.children[this.stck_curIndex].width * this.DIREC_LEVEL) * this.DIREC_LEVEL;
  284. let updateBlocks = true;
  285. // Make current block fall to the ground if it fits
  286. if (sublevelType == 'Plus') {
  287. if (this.stck_blocks.children[0].x >= (this.nextStartX + extra)) {
  288. let stck_curEnd = this.stck_blocks.children[0].x + this.stck_blocks.children[this.stck_curIndex].width;
  289. if (this.stck_curIndex == this.stck_index) {
  290. const floor_curEnd = this.floor_blocks.children[this.floor_index].x + this.floor_blocks.children[this.floor_index].width;
  291. if (stck_curEnd > floor_curEnd) updateBlocks = false;
  292. }
  293. if (updateBlocks) {
  294. // Blocks on the floor
  295. for (let i = 0; i <= this.floor_index; i++) {
  296. if (this.floor_blocks.children[i].x < stck_curEnd) {
  297. this.floor_blocks.children[i].alpha = 0.2; // Make the floor look like it's filled
  298. this.floor_curIndex = i;
  299. }
  300. }
  301. // Blocks on the stack
  302. this.stck_blocks.children[this.stck_curIndex].alpha = 0; // Make block invisible
  303. this.stck_blocks.y += this.stck_blocks.children[this.stck_curIndex].height - 2; // Lower stacked blocks
  304. }
  305. if (this.stck_curIndex != this.stck_index) {
  306. this.nextStartX += this.stck_blocks.children[this.stck_curIndex + 1].width;
  307. }
  308. this.stck_curIndex++;
  309. }
  310. }
  311. if (sublevelType == 'Minus') {
  312. if (this.stck_blocks.children[0].x <= (this.nextStartX + extra)) {
  313. let stck_curEnd = this.stck_blocks.children[0].x + this.stck_blocks.children[this.stck_curIndex].width;
  314. if (this.stck_curIndex == this.stck_index) {
  315. const floor_curEnd = this.floor_blocks.children[this.floor_index].x + this.floor_blocks.children[this.floor_index].width;
  316. if (stck_curEnd < floor_curEnd) updateBlocks = false;
  317. }
  318. if (updateBlocks) {
  319. // Blocks on the floor
  320. for (let i = 0; i <= this.floor_index; i++) {
  321. if (this.floor_blocks.children[i].x > stck_curEnd) {
  322. this.floor_curIndex = i;
  323. this.floor_blocks.children[i].alpha = 0.2; // Make the floor look like it's filled
  324. }
  325. }
  326. // Blocks on the stack
  327. this.stck_blocks.children[this.stck_curIndex].alpha = 0; // Make block invisible
  328. this.stck_blocks.y += this.stck_blocks.children[this.stck_curIndex].height - 2; // Lower stacked blocks
  329. }
  330. if (this.stck_curIndex != this.stck_index) {
  331. this.nextStartX += this.stck_blocks.children[this.stck_curIndex + 1].width;
  332. }
  333. this.stck_curIndex++;
  334. }
  335. }
  336. // Checks if it reached the clicked position
  337. if (this.stck_curIndex > this.stck_index || this.floor_curIndex >= this.floor_index) {
  338. this.animate = false;
  339. this.checkAnswer = true;
  340. }
  341. }
  342. // When animation ends check if there are any blocks left
  343. if (this.checkAnswer) {
  344. this.timer.stop();
  345. this.tractor.animations.stop();
  346. if (levelType=='A') {
  347. this.result = (this.floor_index == this.floor_correctIndex);
  348. } else {
  349. this.result = (this.stck_index == this.stck_correctIndex);
  350. }
  351. // Give feedback to player and turns on sprite animation
  352. if (this.result) { // Correct answer
  353. this.tractor.animations.play('run', 6, true);
  354. // Displays feedback image and sound
  355. this.okImg.visible = true;
  356. if (audioStatus) okSound.play();
  357. completedLevels++; // increases number os passed levels
  358. if (debugMode) console.log("completedLevels = " + completedLevels);
  359. } else { // Incorrect answer
  360. // Displays feedback image and sound
  361. this.errorImg.visible = true;
  362. if (audioStatus) errorSound.play();
  363. }
  364. this.func_postScore();
  365. this.animateCount = 0;
  366. this.checkAnswer = false;
  367. this.animateEnding = true;
  368. }
  369. // Starts 'ending' tractor moving animation
  370. if (this.animateEnding) {
  371. this.animateCount++;
  372. // If correct answer executes final tractor animation (else tractor desn't move)
  373. if (this.result) this.tractor.x += this.animationSṕeed;
  374. // If tractor animation reaches final point calls map state
  375. if (this.animateCount >= 140) {
  376. // If correct, let player go to next level in map
  377. if (this.result) mapCanMove = true;
  378. else mapCanMove = false;
  379. game.state.start('map');
  380. }
  381. }
  382. },
  383. func_overSquare: function () {
  384. if (!self.hasClicked) {
  385. // On leveltype A
  386. if (levelType == "A") {
  387. for (let i = 0; i < self.floor_numBlocks; i++) {
  388. self.floor_blocks.children[i].alpha = (i <= this.index) ? 1 : 0.5;
  389. }
  390. // Saves the index of the selected 'floor' block
  391. self.floor_index = this.index;
  392. // On leveltype B
  393. } else {
  394. for (let i = 0; i < self.stck_numBlocks; i++) {
  395. self.stck_blocks.children[i].alpha = (i <= this.index) ? 0.5 : 0.2;
  396. }
  397. // Saves the index of the selected 'stack' block
  398. self.stck_index = this.index;
  399. }
  400. }
  401. },
  402. func_outSquare: function () {
  403. if (!self.hasClicked) {
  404. //on level type A
  405. if (levelType == "A") {
  406. for (let i = 0; i < self.floor_numBlocks; i++) {
  407. self.floor_blocks.children[i].alpha = 0.5; // back to normal
  408. }
  409. self.floor_index = -1;
  410. //on level type B
  411. } else {
  412. for (let i = 0; i < self.stck_numBlocks; i++) {
  413. self.stck_blocks.children[i].alpha = 0.5; // back to normal
  414. }
  415. self.stck_index = -1;
  416. }
  417. }
  418. },
  419. func_clickSquare: function () {
  420. if (!self.hasClicked && !self.animateEnding) {
  421. // On leveltype A
  422. if (levelType == 'A') {
  423. // Turns selection arrow completely visible
  424. self.arrow.alpha = 1;
  425. // Make the unselected blocks invisible (look like there's only the ground)
  426. for (let i = 0; i < self.floor_numBlocks; i++) {
  427. if (i > self.floor_index) {
  428. self.floor_blocks.children[i].visible = false; // Make unselected 'floor' blocks invisible
  429. }
  430. }
  431. // save the 'stacked' blocks index to compare to the floor index in update
  432. self.stck_index = self.stck_numBlocks - 1;
  433. // On leveltype B
  434. } else {
  435. for (let i = 0; i < self.stck_numBlocks; i++) {
  436. if (i > self.stck_index) {
  437. self.stck_blocks.children[i].visible = false; // Make unselected 'stacked' blocks invisible
  438. }
  439. }
  440. // save the updated total stacked blocks to compare in update
  441. self.stck_numBlocks = self.stck_index + 1;
  442. // save the 'floor' blocks index to compare to the stacked index in update
  443. self.floor_index = self.floor_numBlocks - 1;
  444. }
  445. // Play beep sound
  446. if (audioStatus) beepSound.play();
  447. // Hide labels
  448. if (levelLabel) {
  449. self.stck_blockLabels.visible = false;
  450. self.stck_fractionLines.visible = false;
  451. }
  452. // Hide solution pointer
  453. if (self.pointer != undefined) self.pointer.visible = false;
  454. // Turn tractir animation on
  455. self.tractor.animations.play('run', 5, true);
  456. self.hasClicked = true;
  457. self.animate = true;
  458. }
  459. },
  460. func_viewHelp: function () {
  461. if (!self.hasClicked) {
  462. let aux;
  463. // On leveltype A
  464. if (levelType == 'A') {
  465. aux = self.floor_blocks.children[0];
  466. self.pointer = game.add.image(self.floor_correctPositionX - aux.width / 2, 501, 'pointer');
  467. // On leveltype B
  468. } else {
  469. aux = self.stck_blocks.children[self.stck_correctIndex];
  470. self.pointer = game.add.image(aux.x + aux.width / 2, aux.y, 'pointer');
  471. }
  472. self.pointer.scale.setTo(0.5);
  473. self.pointer.anchor.setTo(0.5, 0);
  474. self.pointer.alpha = 0.7;
  475. }
  476. },
  477. // Game information
  478. func_updateCounter: function () {
  479. this.totalTime++;
  480. },
  481. func_postScore: function () {
  482. // Get correct information about player name and default language
  483. // Variables 'playerName' and 'lang' are defined on: js/preMenu.js
  484. // Variable 'lang' has all the JSON content of 'assets/lang/pt_BR.json', 'lang.lang' defined in 'js/preMenu.js' (func_setLang())
  485. const abst = "numBlocks:" + this.stck_numBlocks
  486. + ", valBlocks: " + this.divisorsList
  487. + " blockIndex: " + this.stck_index
  488. + ", floorIndex: " + this.floor_index;
  489. let hr = new XMLHttpRequest();
  490. // Create some variables we need to send to our PHP file
  491. const url = "php/save.php";
  492. let vars = "s_ip=" + hip
  493. + "&s_name=" + playerName
  494. + "&s_lang=" + langString
  495. + "&s_game=" + levelShape
  496. + "&s_mode=" + levelType;
  497. vars += "&s_oper=" + sublevelType
  498. + "&s_leve=" + levelDifficulty
  499. + "&s_posi=" + mapPosition
  500. + "&s_resu=" + this.result
  501. + "&s_time=" + this.totalTime
  502. + "&s_deta=" + abst;
  503. // Sobre nome do usuario:
  504. // * js/squareOne.js: name
  505. // * js/pt_BR.json: welcome="Ola'", insert_name="DIGITE SEU NOME"
  506. // * php/save.php : $play = $_REQUEST["stacked.stck_name"];
  507. // * js/preMenu.js : insert_name, game.add.text(...), playerName = document.getElementById("name_id").value;
  508. // Pegar valor de PHP para JS: echo("<script language='javascript'>location.href='download.php?arquivo=$nome_arquivo&dir=$dir&id_exer=$id_exer'</script>");
  509. hr.open("POST", url, true);
  510. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  511. hr.onreadystatechange = function () {
  512. if (debugMode) console.log(hr);
  513. if (hr.readyState == 4 && hr.status == 200) {
  514. let return_data = hr.responseText;
  515. if (debugMode) console.log(return_data);
  516. }
  517. }
  518. // Send the data to PHP now... and wait for response to update the status div
  519. hr.send(vars); // Actually execute the request
  520. if (debugMode) console.log("processing...");
  521. if (debugMode) console.log(vars);
  522. }
  523. };