squareOne.js 22 KB

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