squareOne.js 23 KB

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