squareOne.js 23 KB

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