squareOne.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /**
  2. * GAME STATE
  3. *
  4. * LEVELS - SQUARE I & II: tractor level
  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 for each level : 3
  14. *
  15. * Levels can be : 'A' or 'B' (in variable 'levelType')
  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. * Sublevels can be : 'Plus' or 'Minus' (in variable 'sublevelType')
  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 func_postScore)
  44. this.DIREC_LEVEL = (sublevelType == '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 = (sublevelType == '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 (sublevelType == '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, // (levelType '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, // (levelType '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, // (levelType '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, // (levelType '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 (levelType == '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 func_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 ((sublevelType == 'Plus' && stck.blocks[0].x >= (stck.curBlockEnd + restOfCurBlock)) ||
  139. (sublevelType == '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 ((sublevelType == 'Plus' && curEnd > selectedEnd) || (sublevelType == '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 ((sublevelType == 'Plus' && floor.blocks[i].x < curEnd) || (sublevelType == '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 (levelType == '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 passed 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.func_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 (levelType == '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 (levelType == '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 (levelType == '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 leveltype A
  291. if (levelType == '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 leveltype 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 level type A
  314. if (levelType == '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 level type 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 leveltype A
  335. if (levelType == '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 leveltype 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 => {
  362. cur.alpha = 0;
  363. });
  364. });
  365. }
  366. // Hide solution pointer
  367. if (self.help != undefined) self.help.alpha = 0;
  368. // Turn tractir animation on
  369. game.animation.play(self.tractor.animation[0]);
  370. self.hasClicked = true;
  371. self.animate = true;
  372. }
  373. },
  374. /* GAME FUNCTIONS */
  375. /**
  376. * Create stacked blocks for the level (called in create())
  377. * @returns {boolean}
  378. */
  379. func_createStckBlocks: function () {
  380. let hasBaseDifficulty = false; // Will be true after next for loop if level has at least one '1/difficulty' fraction (if false, restart)
  381. const max = (levelType == 'B') ? 10 : mapPosition + 4; // Maximum number of stacked blocks for the level
  382. const total = game.math.randomInRange(mapPosition + 2, max); // Current number of stacked blocks for the level
  383. self.floor.correctXA = self.startX + self.defaultBlockWidth * self.DIREC_LEVEL;
  384. for (let i = 0; i < total; i++) { // For each stacked block
  385. let divisor = game.math.randomInRange(1, gameDifficulty); // Set divisor for fraction
  386. if (divisor == gameDifficulty) hasBaseDifficulty = true;
  387. if (divisor == 3) divisor = 4; // Make sure valid divisors are 1, 2 and 4 (not 3)
  388. self.divisorsList += divisor + ','; // List of divisors (for func_postScore())
  389. const curBlockWidth = self.defaultBlockWidth / divisor; // Current width is a fraction of the default
  390. self.floor.correctXA += curBlockWidth * self.DIREC_LEVEL;
  391. // Create stacked block (close to tractor)
  392. const lineColor = (sublevelType == 'Minus') ? colors.red : colors.darkBlue;
  393. const lineSize = 2;
  394. const block = game.add.graphic.rect(
  395. self.startX,
  396. 462 - i * (self.defaultBlockHeight - lineSize),
  397. curBlockWidth - lineSize,
  398. self.defaultBlockHeight - lineSize,
  399. lineColor,
  400. lineSize,
  401. colors.white,
  402. 1);
  403. const anchor = (sublevelType == 'Minus') ? 1 : 0;
  404. block.anchor(anchor, 0);
  405. // If game is type B, adding events to stacked blocks
  406. if (levelType == 'B') {
  407. block.alpha = 0.5;
  408. block.index = i;
  409. }
  410. self.stck.blocks.push(block);
  411. // If 'show fractions' is turned on, create labels that display the fractions on the side of each block
  412. if (fractionLabel) {
  413. const x = self.startX + (curBlockWidth + 15) * self.DIREC_LEVEL;
  414. const y = self.defaultBlockHeight - lineSize;
  415. const label = [];
  416. if (divisor == 1) {
  417. label[0] = game.add.text(x, 488 - i * y, divisor, textStyles.h2_blue);
  418. } else {
  419. label[0] = game.add.text(x, 479 - i * y + 16, divisor, textStyles.p_blue);
  420. label[1] = game.add.text(x, 479 - i * y, '1', textStyles.p_blue);
  421. label[2] = game.add.text(x, 479 - i * y, '_', textStyles.p_blue);
  422. }
  423. // Add current label to group of labels
  424. self.stck.labels.push(label);
  425. }
  426. }
  427. // 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
  428. self.stck.curBlockEnd = self.startX + self.stck.blocks[0].width * self.DIREC_LEVEL;
  429. let restart = false;
  430. // Check for errors (level too easy for its difficulty or end position out of bounds)
  431. if (!hasBaseDifficulty ||
  432. (sublevelType == 'Plus' && (self.floor.correctXA < (self.startX + self.defaultBlockWidth) ||
  433. self.floor.correctXA > (self.startX + 8 * self.defaultBlockWidth))) ||
  434. (sublevelType == 'Minus' && (self.floor.correctXA < (self.startX - (8 * self.defaultBlockWidth)) ||
  435. self.floor.correctXA > (self.startX - self.defaultBlockWidth)))
  436. ) {
  437. restart = true; // If any error is found restart the level
  438. }
  439. if (debugMode) console.log('Stacked blocks: ' + total + ' (min: ' + (mapPosition + 2) + ', max: ' + max + ')');
  440. return restart;
  441. },
  442. /**
  443. * Create floor blocks for the level (called in create())
  444. */
  445. func_createFloorBlocks: function () { // For each floor block
  446. const divisor = (gameDifficulty == 3) ? 4 : gameDifficulty; // Make sure valid divisors are 1, 2 and 4 (not 3)
  447. let total = 8 * divisor; // Number of floor blocks
  448. const blockWidth = self.defaultBlockWidth / divisor; // Width of each floor block
  449. // If game is type B, selectiong a random floor x position
  450. if (levelType == 'B') {
  451. self.stck.correctIndex = game.math.randomInRange(0, (self.stck.blocks.length - 1)); // Correct stacked index
  452. self.floor.correctXB = self.startX + self.defaultBlockWidth * self.DIREC_LEVEL;
  453. for (let i = 0; i <= self.stck.correctIndex; i++) {
  454. self.floor.correctXB += self.stck.blocks[i].width * self.DIREC_LEVEL; // Equivalent x position on the floor
  455. }
  456. }
  457. let flag = true;
  458. for (let i = 0; i < total; i++) { // For each floor block
  459. // 'x' coordinate for floor block
  460. const x = self.startX + (self.defaultBlockWidth + i * blockWidth) * self.DIREC_LEVEL;
  461. if (flag && levelType == 'A') {
  462. if (
  463. (sublevelType == 'Plus' && x >= self.floor.correctXA) ||
  464. (sublevelType == 'Minus' && x <= self.floor.correctXA)
  465. ) {
  466. self.floor.correctIndex = i - 1; // Set index of correct floor block
  467. flag = false;
  468. }
  469. }
  470. if (levelType == 'B') {
  471. if (
  472. (sublevelType == 'Plus' && x >= self.floor.correctXB) ||
  473. (sublevelType == 'Minus' && x <= self.floor.correctXB)
  474. ) {
  475. total = i;
  476. break;
  477. }
  478. }
  479. // Create floor block
  480. const lineSize = 0.9;
  481. const block = game.add.graphic.rect(
  482. x,
  483. 462 + self.defaultBlockHeight - lineSize,
  484. blockWidth - lineSize,
  485. self.defaultBlockHeight - lineSize,
  486. colors.blueBckg,
  487. lineSize,
  488. colors.blueBckgInsideLevel,
  489. 1);
  490. const anchor = (sublevelType == 'Minus') ? 1 : 0;
  491. block.anchor(anchor, 0);
  492. // If game is type A, adding events to floor blocks
  493. if (levelType == 'A') {
  494. block.alpha = 0.5;
  495. block.index = i;
  496. }
  497. // Add current label to group of labels
  498. self.floor.blocks.push(block);
  499. }
  500. if (levelType == 'A') self.floor.correctX = self.floor.correctXA;
  501. else if (levelType == 'B') self.floor.correctX = self.floor.correctXB;
  502. // Creates labels on the floor to display the numbers
  503. for (let i = 1; i < 10; i++) {
  504. const x = self.startX + (i * self.defaultBlockWidth * self.DIREC_LEVEL);
  505. game.add.text(x, 462 + self.defaultBlockHeight + 58, i - 1, textStyles.h2_blue);
  506. }
  507. },
  508. /**
  509. * Display correct answer
  510. */
  511. func_viewHelp: function () {
  512. if (!self.hasClicked) {
  513. // On levelType A
  514. if (levelType == 'A') {
  515. const aux = self.floor.blocks[0];
  516. self.help.x = self.floor.correctX - aux.width / 2 * self.DIREC_LEVEL;
  517. self.help.y = 501;
  518. // On levelType B
  519. } else {
  520. const aux = self.stck.blocks[self.stck.correctIndex];
  521. self.help.x = aux.x + aux.width / 2 * self.DIREC_LEVEL;
  522. self.help.y = aux.y;
  523. }
  524. self.help.alpha = 0.7;
  525. }
  526. },
  527. /* METADATA FOR GAME */
  528. /**
  529. * Saves players data after level
  530. */
  531. func_postScore: function () {
  532. // Create some variables we need to send to our PHP file
  533. const data = '&s_game=' + gameShape
  534. + '&s_mode=' + levelType
  535. + '&s_oper=' + sublevelType
  536. + '&s_leve=' + gameDifficulty
  537. + '&s_posi=' + mapPosition
  538. + '&s_resu=' + self.result
  539. + '&s_time=' + game.timer.elapsed
  540. + '&s_deta='
  541. + 'numBlocks:' + self.stck.blocks.length
  542. + ', valBlocks: ' + self.divisorsList // Ends in ','
  543. + ' blockIndex: ' + self.stck.index
  544. + ', floorIndex: ' + self.floor.index;
  545. postScore(data);
  546. },
  547. };