squareOne.js 22 KB

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