squareOne.js 22 KB

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