squareOne.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /**
  2. * LInE - Free Education, Private Data
  3. * iFractions GAME STATE
  4. *
  5. * LEVELS - SQUARE I & II: tractor level
  6. *
  7. * Name of game state : squareOne
  8. * Shape : square
  9. * Character : tractor
  10. * Theme : farm
  11. * Concept : Player associates 'blocks carried by the tractor' and 'floor spaces to be filled by them'
  12. * Represent fractions as : blocks
  13. *
  14. * # of different difficulties for each level : 3
  15. *
  16. * Levels can be : 'A' or 'B' (in variable 'levelType')
  17. *
  18. * A : Player can select # of 'floor blocks' (hole in the ground)
  19. * Selects size of hole to be made in the ground (to fill with the blocks in front of the truck)
  20. * B : Player can select # of 'stacked blocks' (in front of the truck)
  21. * Selects number of blocks in front of the truck (to fill the hole on the ground)
  22. *
  23. * Sublevels can be : 'Plus' or 'Minus' (in variable 'sublevelType')
  24. *
  25. * Plus : addition of fractions
  26. * Represented by : tractor going to the right (floor positions 0..8
  27. * Minus : subtraction of fractions
  28. * Represented by: tractor going to the left (floor positions 8..0)
  29. *
  30. * @namespace
  31. */
  32. const squareOne = {
  33. /**
  34. * Main code
  35. */
  36. create: function () {
  37. // CONTROL VARIABLES
  38. this.checkAnswer = false; // When true allows game to run 'check answer' code in update
  39. this.animate = false; // When true allows game to run 'tractor animation' code in update (turns animation of the moving tractor ON/OFF)
  40. this.animateEnding = false; // When true allows game to run 'tractor ending animation' code in update (turns 'ending' animation of the moving tractor ON/OFF)
  41. this.hasClicked = false; // Checks if player 'clicked' on a block
  42. this.result = false; // Checks player 'answer'
  43. this.count = 0; // An 'x' position counter used in the tractor animation
  44. this.divisorsList = ''; // Hold the divisors for each fraction on stacked blocks (created for postScore())
  45. this.DIREC_LEVEL = (sublevelType == 'Minus') ? -1 : 1; // Will be multiplied to values to easily change tractor direction when needed
  46. this.animationSpeed = 2 * this.DIREC_LEVEL; // X distance in which the tractor moves in each iteration of the animation
  47. // GAME VARIABLES
  48. this.defaultBlockWidth = 80; // Base block width
  49. this.defaultBlockHeight = 40; // Base block height
  50. this.startX = (sublevelType == 'Minus') ? 730 : 170; // Initial 'x' coordinate for the tractor and stacked blocks
  51. // BACKGROUND
  52. // Add background image
  53. game.add.image(0, 0, 'bgimage');
  54. // Add clouds
  55. game.add.image(300, 100, 'cloud');
  56. game.add.image(660, 80, 'cloud');
  57. game.add.image(110, 85, 'cloud', 0.8);
  58. // Add floor of grass
  59. for (let i = 0; i < 9; i++) { game.add.image(i * 100, 501, 'floor'); }
  60. // Calls function that loads navigation icons
  61. navigationIcons.func_addIcons(
  62. true, true, true, // Left icons
  63. true, false, // Right icons
  64. 'customMenu', this.func_viewHelp
  65. );
  66. // TRACTOR
  67. this.tractor = game.add.sprite(this.startX, 445, 'tractor', 0, 0.8);
  68. if (sublevelType == 'Plus') {
  69. this.tractor.anchor(1, 0.5);
  70. this.tractor.animation = ['move', [0, 1, 2, 3, 4], 4];
  71. } else {
  72. this.tractor.anchor(0, 0.5);
  73. this.tractor.animation = ['move', [5, 6, 7, 8, 9], 4];
  74. this.tractor.curFrame = 5;
  75. }
  76. // STACKED BLOCKS variables
  77. this.stck = {
  78. blocks: [], // Group of 'stacked' block objects
  79. labels: [], // Group of fraction labels on the side of 'stacked' blocks
  80. index: undefined, // (levelType 'B') index of 'stacked' block selected by player
  81. // Control variables for animation
  82. curIndex: 0, // (needs to be 0)
  83. curBlockEnd: undefined,
  84. // Correct values
  85. correctIndex: undefined, // (levelType 'B') index of the CORRECT 'stacked' block
  86. };
  87. // FLOOR BLOCKS variables
  88. this.floor = {
  89. blocks: [], // Group of 'floor' block objects
  90. index: undefined, // (levelType 'A') index of 'floor' block selected by player
  91. // Control variables for animation
  92. curIndex: -1, // (needs to be -1)
  93. // Correct values
  94. correctIndex: undefined, // (levelType 'A') index of the CORRECT 'floor' block
  95. correctX: undefined, // 'x' coordinate of CORRECT 'floor' block
  96. correctXA: undefined, // Temporary variable
  97. correctXB: undefined, // Temporary variable
  98. };
  99. // CREATING STACKED BLOCKS
  100. this.restart = this.func_createStckBlocks();
  101. // CREATING FLOOR BLOCKS
  102. this.func_createFloorBlocks();
  103. // SELECTION ARROW
  104. if (levelType == 'A') {
  105. this.arrow = game.add.image(this.startX + this.defaultBlockWidth * this.DIREC_LEVEL, 480, 'arrow_down');
  106. this.arrow.anchor(0.5, 0.5);
  107. this.arrow.alpha = 0.5;
  108. }
  109. // Help pointer
  110. this.help = game.add.image(0, 0, 'help_pointer', 0.5);
  111. this.help.anchor(0.5, 0);
  112. this.help.alpha = 0;
  113. if (!this.restart) {
  114. game.timer.start(); // Set a timer for the current level (used in postScore())
  115. game.event.add('click', this.func_onInputDown);
  116. game.event.add('mousemove', this.func_onInputOver);
  117. }
  118. },
  119. /**
  120. * Game loop
  121. */
  122. update: function () {
  123. // AFTER PLAYER SELECTION
  124. // Starts tractor moving animation
  125. if (self.animate) {
  126. const stck = self.stck;
  127. const floor = self.floor;
  128. // MANAGE HORIZONTAL MOVEMENT
  129. // Move 'tractor'
  130. self.tractor.x += self.animationSpeed;
  131. // Move 'stacked blocks'
  132. for (let i in stck.blocks) {
  133. stck.blocks[i].x += self.animationSpeed;
  134. }
  135. // MANAGE BLOCKS AND FLOOR GAPS
  136. // If block is 1/n (not 1/1) there's an extra block space to go through before the start of next block
  137. const restOfCurBlock = (self.defaultBlockWidth - stck.blocks[stck.curIndex].width) * self.DIREC_LEVEL;
  138. // Check if block falls
  139. if ((sublevelType == 'Plus' && stck.blocks[0].x >= (stck.curBlockEnd + restOfCurBlock)) ||
  140. (sublevelType == 'Minus' && stck.blocks[0].x <= (stck.curBlockEnd + restOfCurBlock))) {
  141. let lowerBlock = true;
  142. const curEnd = stck.blocks[0].x + stck.blocks[stck.curIndex].width * self.DIREC_LEVEL;
  143. // If current index is (A) last stacked index (correct index - fixed)
  144. // If current index is (B) selected stacked index
  145. if (stck.curIndex == stck.index) {
  146. // floor.index : (A) selected floor index
  147. // floor.index : (B) last floor index (correct index - fixed)
  148. const selectedEnd = floor.blocks[floor.index].x + floor.blocks[0].width * self.DIREC_LEVEL;
  149. // (A) last stacked block (fixed) doesnt fit selected gap AKA NOT ENOUGH FLOOR BLOCKS (DOESNT CHECK TOO MANY)
  150. // (B) selected stacked index doesnt fit last floor gap (fixed) AKA TOO MANY STACKED BLOCKS (DOESNT CHECK NOT ENOUGH)
  151. if ((sublevelType == 'Plus' && curEnd > selectedEnd) || (sublevelType == 'Minus' && curEnd < selectedEnd)) {
  152. lowerBlock = false;
  153. }
  154. } else {
  155. // Update to next block end
  156. stck.curBlockEnd += stck.blocks[stck.curIndex + 1].width * self.DIREC_LEVEL;
  157. }
  158. // Fill floor gap
  159. if (lowerBlock) {
  160. // Until (A) selected floor index
  161. // Until (B) last floor index (correct index - fixed)
  162. // Updates floor index to be equivalent to stacked index (and change alpha so floor appears to be filled)
  163. for (let i = 0; i <= floor.index; i++) {
  164. if ((sublevelType == 'Plus' && floor.blocks[i].x < curEnd) || (sublevelType == 'Minus' && floor.blocks[i].x > curEnd)) {
  165. floor.blocks[i].alpha = 0.2;
  166. floor.curIndex = i;
  167. }
  168. }
  169. // Lower
  170. stck.blocks[stck.curIndex].alpha = 0;
  171. stck.blocks.forEach(cur => { cur.y += self.defaultBlockHeight - 2; }); // Lower stacked blocks
  172. }
  173. stck.curIndex++;
  174. }
  175. // WHEN REACHED END POSITION
  176. if (stck.curIndex > stck.index || floor.curIndex == floor.index) {
  177. self.animate = false;
  178. self.checkAnswer = true;
  179. }
  180. }
  181. // When animation ends check answer
  182. if (self.checkAnswer) {
  183. game.timer.stop();
  184. game.animation.stop(self.tractor.animation[0]);
  185. if (levelType == 'A') {
  186. self.result = self.floor.index == self.floor.correctIndex;
  187. } else {
  188. self.result = self.stck.index == self.stck.correctIndex;
  189. }
  190. // Give feedback to player and turns on sprite animation
  191. if (self.result) { // Correct answer
  192. game.animation.play(self.tractor.animation[0]);
  193. // Displays feedback image and sound
  194. game.add.image(defaultWidth / 2, defaultHeight / 2, 'ok').anchor(0.5, 0.5);
  195. if (audioStatus) game.audio.okSound.play();
  196. completedLevels++; // Increases number os passed levels
  197. if (debugMode) console.log('completedLevels = ' + completedLevels);
  198. } else { // Incorrect answer
  199. // Displays feedback image and sound
  200. game.add.image(defaultWidth / 2, defaultHeight / 2, 'error').anchor(0.5, 0.5);
  201. if (audioStatus) game.audio.errorSound.play();
  202. }
  203. self.postScore();
  204. // AFTER CHECK ANSWER
  205. self.checkAnswer = false;
  206. self.animateEnding = true;
  207. }
  208. // Starts 'ending' tractor moving animation
  209. if (self.animateEnding) {
  210. // ANIMATE ENDING
  211. self.count++;
  212. // If CORRECT ANSWER runs final tractor animation (else tractor desn't move, just wait)
  213. if (self.result) self.tractor.x += self.animationSpeed;
  214. // WHEN REACHED END POSITION calls map state
  215. if (self.count >= 140) {
  216. // If CORRECT ANSWER, player goes to next level in map
  217. if (self.result) mapMove = true;
  218. else mapMove = false;
  219. game.state.start('map');
  220. }
  221. }
  222. game.render.all();
  223. },
  224. /* EVENT HANDLER */
  225. /**
  226. * Called by mouse click event
  227. *
  228. * @param {object} mouseEvent contains the mouse click coordinates
  229. */
  230. func_onInputDown: function (mouseEvent) {
  231. const x = mouseEvent.offsetX;
  232. const y = mouseEvent.offsetY;
  233. if (levelType == 'A') {
  234. self.floor.blocks.forEach(cur => {
  235. if (game.math.isOverIcon(x, y, cur)) self.func_clickSquare(cur);
  236. });
  237. } else {
  238. self.stck.blocks.forEach(cur => {
  239. if (game.math.isOverIcon(x, y, cur)) self.func_clickSquare(cur);
  240. });
  241. }
  242. navigationIcons.func_onInputDown(x, y);
  243. game.render.all();
  244. },
  245. /**
  246. * Called by mouse move event
  247. *
  248. * @param {object} mouseEvent contains the mouse move coordinates
  249. */
  250. func_onInputOver: function (mouseEvent) {
  251. const x = mouseEvent.offsetX;
  252. const y = mouseEvent.offsetY;
  253. let flagA = false;
  254. let flagB = false;
  255. if (levelType == 'A') {
  256. // Make arrow follow mouse
  257. if (!self.hasClicked && !self.animateEnding) {
  258. if (game.math.distanceToPointer(self.arrow.x, x, self.arrow.y, y) > 8) {
  259. self.arrow.x = (x < 250) ? 250 : x; // Limits the arrow left position to 250
  260. }
  261. }
  262. self.floor.blocks.forEach(cur => {
  263. if (game.math.isOverIcon(x, y, cur)) {
  264. flagA = true;
  265. self.func_overSquare(cur);
  266. }
  267. });
  268. if (!flagA) self.func_outSquare('A');
  269. }
  270. if (levelType == 'B') {
  271. self.stck.blocks.forEach(cur => {
  272. if (game.math.isOverIcon(x, y, cur)) {
  273. flagB = true;
  274. self.func_overSquare(cur);
  275. }
  276. });
  277. if (!flagB) self.func_outSquare('B');
  278. }
  279. navigationIcons.func_onInputOver(x, y);
  280. game.render.all();
  281. },
  282. /* CALLED BY EVENT HANDLER */
  283. /**
  284. * Function called when cursor is over a valid rectangle
  285. *
  286. * @param {object} cur rectangle the cursor is over
  287. */
  288. func_overSquare: function (cur) {
  289. if (!self.hasClicked) {
  290. document.body.style.cursor = 'pointer';
  291. // On leveltype A
  292. if (levelType == 'A') {
  293. for (let i in self.floor.blocks) {
  294. self.floor.blocks[i].alpha = (i <= cur.index) ? 1 : 0.5;
  295. }
  296. // Saves the index of the selected 'floor' block
  297. self.floor.index = cur.index;
  298. // On leveltype B
  299. } else {
  300. for (let i in self.stck.blocks) {
  301. self.stck.blocks[i].alpha = (i <= cur.index) ? 0.5 : 0.2;
  302. }
  303. // Saves the index of the selected 'stack' block
  304. self.stck.index = cur.index;
  305. }
  306. }
  307. },
  308. /**
  309. * Function called when cursos is out of a valid rectangle
  310. */
  311. func_outSquare: function () {
  312. if (!self.hasClicked) {
  313. document.body.style.cursor = 'auto';
  314. // On level type A
  315. if (levelType == 'A') {
  316. for (let i in self.floor.blocks) {
  317. self.floor.blocks[i].alpha = 0.5; // Back to normal
  318. }
  319. self.floor.index = -1;
  320. // On level type B
  321. } else {
  322. for (let i in self.stck.blocks) {
  323. self.stck.blocks[i].alpha = 0.5; // Back to normal
  324. }
  325. self.stck.index = -1;
  326. }
  327. }
  328. },
  329. /**
  330. * Function called when player clicks on a valid rectangle
  331. */
  332. func_clickSquare: function () {
  333. if (!self.hasClicked && !self.animateEnding) {
  334. document.body.style.cursor = 'auto';
  335. // On leveltype A
  336. if (levelType == 'A') {
  337. // Turns selection arrow completely visible
  338. self.arrow.alpha = 1;
  339. // Make the unselected blocks invisible (look like there's only the ground)
  340. for (let i in self.floor.blocks) {
  341. // (SELECTION : self.FLOOR.index)
  342. if (i > self.floor.index) self.floor.blocks[i].alpha = 0; // Make unselected 'floor' blocks invisible
  343. }
  344. // (FIXED : self.STCK.index) save the 'stacked' blocks index
  345. self.stck.index = self.stck.blocks.length - 1;
  346. // On leveltype B
  347. } else {
  348. for (let i in self.stck.blocks) {
  349. // (FIXED : self.STCK.index)
  350. if (i > self.stck.index) self.stck.blocks[i].alpha = 0; // Make unselected 'stacked' blocks invisible
  351. }
  352. // (SELECTION : self.FLOOR.index) save the 'floor' blocks index to compare to the stacked index in update
  353. self.floor.index = self.floor.blocks.length - 1;
  354. // Save the updated total stacked blocks to compare in update
  355. self.stck.blocks.length = self.stck.index + 1;
  356. }
  357. // Play beep sound
  358. if (audioStatus) game.audio.beepSound.play();
  359. // Hide labels
  360. if (fractionLabel) {
  361. self.stck.labels.forEach(cur => {
  362. cur.forEach(cur => { cur.alpha = 0; });
  363. });
  364. }
  365. // Hide solution pointer
  366. if (self.help != undefined) self.help.alpha = 0;
  367. // Turn tractir animation on
  368. game.animation.play(self.tractor.animation[0]);
  369. self.hasClicked = true;
  370. self.animate = true;
  371. }
  372. },
  373. /* GAME FUNCTIONS */
  374. /**
  375. * Create stacked blocks for the level (called in create())
  376. * @returns {boolean}
  377. */
  378. func_createStckBlocks: function () {
  379. let hasBaseDifficulty = false; // Will be true after next for loop if level has at least one '1/difficulty' fraction (if false, restart)
  380. const max = (levelType == 'B') ? 10 : mapPosition + 4; // Maximum number of stacked blocks for the level
  381. const total = game.math.randomInRange(mapPosition + 2, max); // Current number of stacked blocks for the level
  382. self.floor.correctXA = self.startX + self.defaultBlockWidth * self.DIREC_LEVEL;
  383. for (let i = 0; i < total; i++) { // For each stacked block
  384. let divisor = game.math.randomInRange(1, gameDifficulty); // Set divisor for fraction
  385. if (divisor == gameDifficulty) hasBaseDifficulty = true;
  386. if (divisor == 3) divisor = 4; // Make sure valid divisors are 1, 2 and 4 (not 3)
  387. self.divisorsList += divisor + ','; // List of divisors (for postScore())
  388. const curBlockWidth = self.defaultBlockWidth / divisor; // Current width is a fraction of the default
  389. self.floor.correctXA += curBlockWidth * self.DIREC_LEVEL;
  390. // Create stacked block (close to tractor)
  391. const lineColor = (sublevelType == 'Minus') ? colors.red : colors.darkBlue;
  392. const lineSize = 2;
  393. const block = game.add.graphic.rect(
  394. self.startX,
  395. 462 - i * (self.defaultBlockHeight - lineSize),
  396. curBlockWidth - lineSize,
  397. self.defaultBlockHeight - lineSize,
  398. lineColor,
  399. lineSize,
  400. colors.white,
  401. 1);
  402. const anchor = (sublevelType == 'Minus') ? 1 : 0;
  403. block.anchor(anchor, 0);
  404. // If game is type B, adding events to stacked blocks
  405. if (levelType == 'B') {
  406. block.alpha = 0.5;
  407. block.index = i;
  408. }
  409. self.stck.blocks.push(block);
  410. // If 'show fractions' is turned on, create labels that display the fractions on the side of each block
  411. if (fractionLabel) {
  412. const x = self.startX + (curBlockWidth + 15) * self.DIREC_LEVEL;
  413. const y = self.defaultBlockHeight - lineSize;
  414. const label = [];
  415. if (divisor == 1) {
  416. label[0] = game.add.text(x, 488 - i * y, divisor, textStyles.h2_blue);
  417. } else {
  418. label[0] = game.add.text(x, 479 - i * y + 16, divisor, textStyles.p_blue);
  419. label[1] = game.add.text(x, 479 - i * y, '1', textStyles.p_blue);
  420. label[2] = game.add.text(x, 479 - i * y, '_', textStyles.p_blue);
  421. }
  422. // Add current label to group of labels
  423. self.stck.labels.push(label);
  424. }
  425. }
  426. // 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
  427. self.stck.curBlockEnd = self.startX + self.stck.blocks[0].width * self.DIREC_LEVEL;
  428. let restart = false;
  429. // Check for errors (level too easy for its difficulty or end position out of bounds)
  430. if (!hasBaseDifficulty ||
  431. (sublevelType == 'Plus' && (self.floor.correctXA < (self.startX + self.defaultBlockWidth) ||
  432. self.floor.correctXA > (self.startX + 8 * self.defaultBlockWidth))) ||
  433. (sublevelType == 'Minus' && (self.floor.correctXA < (self.startX - (8 * self.defaultBlockWidth)) ||
  434. self.floor.correctXA > (self.startX - self.defaultBlockWidth)))
  435. ) {
  436. restart = true; // If any error is found restart the level
  437. }
  438. if (debugMode) console.log('Stacked blocks: ' + total + ' (min: ' + (mapPosition + 2) + ', max: ' + max + ')');
  439. return restart;
  440. },
  441. /**
  442. * Create floor blocks for the level (called in create())
  443. */
  444. func_createFloorBlocks: function () { // For each floor block
  445. const divisor = (gameDifficulty == 3) ? 4 : gameDifficulty; // Make sure valid divisors are 1, 2 and 4 (not 3)
  446. let total = 8 * divisor; // Number of floor blocks
  447. const blockWidth = self.defaultBlockWidth / divisor; // Width of each floor block
  448. // If game is type B, selectiong a random floor x position
  449. if (levelType == 'B') {
  450. self.stck.correctIndex = game.math.randomInRange(0, (self.stck.blocks.length - 1)); // Correct stacked index
  451. self.floor.correctXB = self.startX + self.defaultBlockWidth * self.DIREC_LEVEL;
  452. for (let i = 0; i <= self.stck.correctIndex; i++) {
  453. self.floor.correctXB += self.stck.blocks[i].width * self.DIREC_LEVEL; // Equivalent x position on the floor
  454. }
  455. }
  456. let flag = true;
  457. for (let i = 0; i < total; i++) { // For each floor block
  458. // 'x' coordinate for floor block
  459. const x = self.startX + (self.defaultBlockWidth + i * blockWidth) * self.DIREC_LEVEL;
  460. if (flag && levelType == 'A') {
  461. if ((sublevelType == 'Plus' && x >= self.floor.correctXA) || (sublevelType == 'Minus' && x <= self.floor.correctXA)) {
  462. self.floor.correctIndex = i - 1; // Set index of correct floor block
  463. flag = false;
  464. }
  465. }
  466. if (levelType == 'B') {
  467. if ((sublevelType == 'Plus' && x >= self.floor.correctXB) || (sublevelType == 'Minus' && x <= self.floor.correctXB)) {
  468. total = i;
  469. break;
  470. }
  471. }
  472. // Create floor block
  473. const lineSize = 0.9;
  474. const block = game.add.graphic.rect(
  475. x,
  476. 462 + self.defaultBlockHeight - lineSize,
  477. blockWidth - lineSize,
  478. self.defaultBlockHeight - lineSize,
  479. colors.blueBckg,
  480. lineSize,
  481. colors.blueBckgInsideLevel,
  482. 1);
  483. const anchor = (sublevelType == 'Minus') ? 1 : 0;
  484. block.anchor(anchor, 0);
  485. // If game is type A, adding events to floor blocks
  486. if (levelType == 'A') {
  487. block.alpha = 0.5;
  488. block.index = i;
  489. }
  490. // Add current label to group of labels
  491. self.floor.blocks.push(block);
  492. }
  493. if (levelType == 'A') self.floor.correctX = self.floor.correctXA;
  494. else if (levelType == 'B') self.floor.correctX = self.floor.correctXB;
  495. // Creates labels on the floor to display the numbers
  496. for (let i = 1; i < 10; i++) {
  497. const x = self.startX + (i * self.defaultBlockWidth * self.DIREC_LEVEL);
  498. game.add.text(x, 462 + self.defaultBlockHeight + 58, i - 1, textStyles.h2_blue);
  499. }
  500. },
  501. /**
  502. * Display correct answer
  503. */
  504. func_viewHelp: function () {
  505. if (!self.hasClicked) {
  506. // On levelType A
  507. if (levelType == 'A') {
  508. const aux = self.floor.blocks[0];
  509. self.help.x = self.floor.correctX - aux.width / 2 * self.DIREC_LEVEL;
  510. self.help.y = 501;
  511. // On levelType B
  512. } else {
  513. const aux = self.stck.blocks[self.stck.correctIndex];
  514. self.help.x = aux.x + aux.width / 2 * self.DIREC_LEVEL;
  515. self.help.y = aux.y;
  516. }
  517. self.help.alpha = 0.7;
  518. }
  519. },
  520. /* METADATA FOR GAME */
  521. /**
  522. * Saves players data after level ends - to be sent to database
  523. *
  524. * Attention: the "line_" prefix data table must be compatible to data table fields (MySQL server)
  525. * @see /php/save.php
  526. */
  527. postScore: function () {
  528. // Creates string that is going to be sent to db
  529. const data = '&line_game=' + gameShape
  530. + '&line_mode=' + levelType
  531. + '&line_oper=' + sublevelType
  532. + '&line_leve=' + gameDifficulty
  533. + '&line_posi=' + mapPosition
  534. + '&line_resu=' + self.result
  535. + '&line_time=' + game.timer.elapsed
  536. + '&line_deta='
  537. + 'numBlocks:' + self.stck.blocks.length
  538. + ', valBlocks: ' + self.divisorsList // Ends in ','
  539. + ' blockIndex: ' + self.stck.index
  540. + ', floorIndex: ' + self.floor.index;
  541. sendToDB(data);
  542. },
  543. };