squareOne.js 22 KB

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