circleOne.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [GAME STATE]
  5. *
  6. * .....circleOne.... = gameName
  7. * ....../...\.......
  8. * .....a.....b...... = gameMode
  9. * .......\./........
  10. * ........|.........
  11. * ....../.|.\.......
  12. * .plus.minus.mixed. = gameOperation
  13. * ......\.|./.......
  14. * ........|.........
  15. * ......1,2,3....... = gameDifficulty
  16. *
  17. * Character : kid/balloon
  18. * Theme : flying in a balloon
  19. * Concept : 'How much the kid has to walk to get to the balloon?'
  20. * Represent fractions as : circles/arcs
  21. *
  22. * Game modes can be :
  23. *
  24. * a : Player can place balloon position
  25. * Place balloon in position (so the kid can get to it)
  26. * b : Player can select # of circles
  27. * Selects number of circles (that represent distance kid needs to walk to get to the balloon)
  28. *
  29. * Operations can be :
  30. *
  31. * plus : addition of fractions
  32. * Represented by : kid going to the right (floor positions 0..5)
  33. * minus : subtraction of fractions
  34. * Represented by: kid going to the left (floor positions 5..0)
  35. * mixed : Mix addition and subtraction of fractions in same
  36. * Represented by: kid going to the left (floor positions 0..5)
  37. *
  38. * @namespace
  39. */
  40. const circleOne = {
  41. ui: undefined,
  42. control: undefined,
  43. animation: undefined,
  44. road: undefined,
  45. circles: undefined,
  46. kid: undefined,
  47. balloon: undefined,
  48. basket: undefined,
  49. walkedPath: undefined,
  50. /**
  51. * Main code
  52. */
  53. create: function () {
  54. this.ui = {
  55. help: undefined,
  56. message: undefined,
  57. continue: {
  58. // modal: undefined,
  59. button: undefined,
  60. text: undefined,
  61. },
  62. };
  63. const roadMapper = () => {
  64. const _pointWidth = (game.sprite['map_place'].width / 2) * 0.45;
  65. const defaultX = 150;
  66. const defaultY =
  67. context.canvas.height - game.image['floor_grass'].width * 1.5;
  68. const defaultWidth = 1620;
  69. // Initial 'x' coordinate for the kid and the baloon
  70. const x =
  71. gameOperation === 'minus'
  72. ? context.canvas.width - defaultX - _pointWidth / 2
  73. : defaultX + _pointWidth / 2;
  74. const y = defaultY;
  75. const width = defaultWidth - _pointWidth;
  76. const _divisions = 5;
  77. const _subdivisions = gameDifficulty === 3 ? 4 : gameDifficulty;
  78. const numberOfBlocks = _divisions * _subdivisions;
  79. return { x, y, width, numberOfBlocks, defaultX, defaultY, defaultWidth };
  80. };
  81. this.road = roadMapper();
  82. const blocksMapper = () => {
  83. let width = this.road.width / this.road.numberOfBlocks;
  84. if (gameOperation === 'minus') width = -width;
  85. const height = 50;
  86. return { width, height, list: [], cur: undefined };
  87. };
  88. this.blocks = blocksMapper();
  89. this.walkedPath = [];
  90. const pointWidth = (game.sprite['map_place'].width / 2) * 0.45;
  91. const distanceBetweenPoints =
  92. (context.canvas.width - this.road.defaultX * 2 - pointWidth) / 5; // Distance between road points
  93. const y0 = this.road.defaultY + 20;
  94. const x0 = this.road.x;
  95. this.circles = {
  96. diameter: game.math.getRadiusFromCircunference(distanceBetweenPoints) * 2, // (Fixed) Circles Diameter
  97. cur: 0, // Current circle index
  98. list: [], // Circle objects
  99. };
  100. this.control = {
  101. directionModifier: gameOperation === 'minus' ? -1 : 1,
  102. correctX: x0, // Correct position (accumulative)
  103. nextX: undefined, // Next point position
  104. divisorsList: '', // used in postScore (Accumulative)
  105. hasClicked: false, // Checks if user has clicked
  106. checkAnswer: false, // Check kid inside ballon's basket
  107. isCorrect: false, // Informs answer is correct
  108. showEndInfo: false,
  109. endSignX: undefined,
  110. curWalkedPath: 0,
  111. // mode 'b' exclusive
  112. correctIndex: undefined,
  113. selectedIndex: -1, // Index of clicked circle (game (b))
  114. numberOfPlusFractions: undefined,
  115. };
  116. const walkOffsetX = 2;
  117. const walksPerDistanceBetweenPoints = distanceBetweenPoints / walkOffsetX;
  118. this.animation = {
  119. list: {
  120. left: undefined,
  121. right: undefined,
  122. },
  123. invertDirection: undefined,
  124. animateKid: false,
  125. animateBalloon: false,
  126. counter: undefined,
  127. walkOffsetX,
  128. angleOffset: 360 / walksPerDistanceBetweenPoints,
  129. };
  130. renderBackground('farmRoad');
  131. // Calls function that loads navigation icons
  132. // FOR MOODLE
  133. if (moodle) {
  134. navigation.add.right(['audio']);
  135. } else {
  136. navigation.add.left(['back', 'menu', 'show_answer'], 'customMenu');
  137. navigation.add.right(['audio']);
  138. }
  139. const validPath = { x0, y0, distanceBetweenPoints };
  140. this.utils.renderRoadBlocks();
  141. this.utils.renderRoad(validPath);
  142. const [restart, balloonX] = this.utils.renderCircles(validPath);
  143. this.restart = restart;
  144. this.utils.renderCharacters(validPath, balloonX);
  145. this.utils.renderMainUI();
  146. if (!this.restart) {
  147. game.timer.start(); // Set a timer for the current level (used in postScore())
  148. game.event.add('click', this.events.onInputDown);
  149. game.event.add('mousemove', this.events.onInputOver);
  150. }
  151. },
  152. /**
  153. * Game loop
  154. */
  155. update: function () {
  156. // Starts kid animation
  157. if (self.animation.animateKid) {
  158. self.utils.animateKidHandler();
  159. }
  160. // Check if kid is inside the basket
  161. if (self.control.checkAnswer) {
  162. self.utils.checkAnswerHandler();
  163. }
  164. // Starts balloon flying animation
  165. if (self.animation.animateBalloon) {
  166. self.utils.animateBalloonHandler();
  167. }
  168. game.render.all();
  169. },
  170. utils: {
  171. renderRoadBlocks: function () {
  172. for (let i = 0; i < self.road.numberOfBlocks; i++) {
  173. const block = game.add.geom.rect(
  174. self.road.x + i * self.blocks.width,
  175. self.road.y,
  176. self.blocks.width, // If gameOperation is minus, block width is negative
  177. self.blocks.height,
  178. 'transparent',
  179. 0.5,
  180. colors.red,
  181. 2
  182. );
  183. block.info = { index: i };
  184. self.blocks.list.push(block);
  185. }
  186. },
  187. // RENDERS
  188. renderRoad: function (validPath) {
  189. const offset = 40;
  190. for (let i = 0; i <= 5; i++) {
  191. // Gray place
  192. game.add
  193. .sprite(
  194. validPath.x0 +
  195. i *
  196. validPath.distanceBetweenPoints *
  197. self.control.directionModifier,
  198. validPath.y0,
  199. 'map_place',
  200. 0,
  201. 0.45
  202. )
  203. .anchor(0.5, 0.5);
  204. // White circle behind number
  205. const curX =
  206. validPath.x0 +
  207. i * validPath.distanceBetweenPoints * self.control.directionModifier;
  208. game.add.geom
  209. .circle(
  210. curX,
  211. validPath.y0 + 60 + offset,
  212. 60,
  213. undefined,
  214. 0,
  215. colors.white,
  216. 0.8
  217. )
  218. .anchor(0, 0.25);
  219. game.add.geom.rect(
  220. curX,
  221. validPath.y0 + 60 - 28,
  222. 4,
  223. 25,
  224. colors.white,
  225. 0.8,
  226. undefined,
  227. 0
  228. );
  229. // Number
  230. game.add.text(
  231. curX,
  232. validPath.y0 + 60 + offset,
  233. i * self.control.directionModifier,
  234. {
  235. ...textStyles.h2_,
  236. font: 'bold ' + textStyles.h2_.font,
  237. fill: gameOperation === 'minus' ? colors.red : colors.green,
  238. }
  239. );
  240. }
  241. self.utils.renderWalkedPath(
  242. validPath.x0 - 1,
  243. validPath.y0 - 5,
  244. gameOperation === 'minus' ? colors.red : colors.green
  245. );
  246. },
  247. renderWalkedPath: function (x, y, color) {
  248. const path = game.add.geom.rect(x, y, 1, 1, 'transparent', 1, color, 4);
  249. self.walkedPath.push(path);
  250. return path;
  251. },
  252. renderCircles: function (validPath) {
  253. let restart = false;
  254. let hasBaseDifficulty = false;
  255. let balloonX = context.canvas.width / 2;
  256. const fractionX =
  257. validPath.x0 -
  258. (self.circles.diameter - 10) * self.control.directionModifier;
  259. const font = {
  260. ...textStyles.h2_,
  261. font: 'bold ' + textStyles.h2_.font,
  262. };
  263. // Number of circles
  264. const max = gameOperation === 'mixed' ? 6 : curMapPosition + 1;
  265. const min =
  266. curMapPosition === 1 && (gameOperation === 'mixed' || gameMode === 'b')
  267. ? 2
  268. : curMapPosition; // Mixed level has at least 2 fractions
  269. const total = game.math.randomInRange(min, max); // Total number of circles
  270. // For mode 'b'
  271. self.control.numberOfPlusFractions = game.math.randomInRange(
  272. 1,
  273. total - 1
  274. );
  275. for (let i = 0; i < total; i++) {
  276. let curDirection = undefined;
  277. let curLineColor = undefined;
  278. let curFillColor = undefined;
  279. let curAngleDegree = undefined;
  280. let curIsCounterclockwise = undefined;
  281. let curFractionItems = undefined;
  282. let curCircle = undefined;
  283. const curCircleInfo = {
  284. direction: undefined,
  285. direc: undefined,
  286. distance: undefined,
  287. angle: undefined,
  288. fraction: {
  289. labels: [],
  290. nominator: undefined,
  291. denominator: undefined,
  292. },
  293. };
  294. let curDivisor = game.math.randomInRange(1, gameDifficulty); // Set fraction 'divisor' (depends on difficulty)
  295. if (curDivisor === gameDifficulty) hasBaseDifficulty = true; // True if after for ends has at least 1 '1/difficulty' fraction
  296. curDivisor = curDivisor === 3 ? 4 : curDivisor; // Turns 1/3 into 1/4 fractions
  297. self.control.divisorsList += curDivisor + ','; // Add this divisor to the list of divisors (for postScore())
  298. // Set each circle direction
  299. switch (gameOperation) {
  300. case 'plus':
  301. curDirection = 'right';
  302. break;
  303. case 'minus':
  304. curDirection = 'left';
  305. break;
  306. case 'mixed':
  307. curDirection =
  308. i < self.control.numberOfPlusFractions ? 'right' : 'left';
  309. break;
  310. }
  311. curCircleInfo.direction = curDirection;
  312. // Set each circle visual info
  313. if (curDirection === 'right') {
  314. curIsCounterclockwise = true;
  315. curLineColor = colors.green;
  316. curFillColor = colors.greenLight;
  317. curCircleInfo.direc = 1;
  318. } else {
  319. curIsCounterclockwise = false;
  320. curLineColor = colors.red;
  321. curFillColor = colors.redLight;
  322. curCircleInfo.direc = -1;
  323. }
  324. font.fill = curLineColor;
  325. const curCircleY =
  326. validPath.y0 -
  327. 5 -
  328. self.circles.diameter / 2 -
  329. i * self.circles.diameter;
  330. // Draw circles
  331. if (curDivisor === 1) {
  332. curAngleDegree = 360;
  333. curCircle = game.add.geom.circle(
  334. validPath.x0,
  335. curCircleY,
  336. self.circles.diameter,
  337. curLineColor,
  338. 3,
  339. curFillColor,
  340. 1
  341. );
  342. curCircle.counterclockwise = curIsCounterclockwise;
  343. curCircleInfo.angleDegree = curAngleDegree;
  344. curFractionItems = [
  345. {
  346. x: fractionX,
  347. y: curCircleY + 10,
  348. text: '1',
  349. },
  350. {
  351. x: fractionX - 25,
  352. y: curCircleY + 10,
  353. text: curDirection === 'left' ? '-' : '',
  354. },
  355. null,
  356. ];
  357. } else {
  358. curAngleDegree = 360 / curDivisor;
  359. if (curDirection === 'right') curAngleDegree = 360 - curAngleDegree; // counterclockwise equivalent
  360. curCircle = game.add.geom.arc(
  361. validPath.x0,
  362. curCircleY,
  363. self.circles.diameter,
  364. 0,
  365. game.math.degreeToRad(curAngleDegree),
  366. curIsCounterclockwise,
  367. curLineColor,
  368. 3,
  369. curFillColor,
  370. 1
  371. );
  372. curCircleInfo.angleDegree = curAngleDegree;
  373. curFractionItems = [
  374. {
  375. x: fractionX,
  376. y: curCircleY - 2,
  377. text: `1\n${curDivisor}`,
  378. },
  379. {
  380. x: fractionX - 35,
  381. y: curCircleY + 15,
  382. text: curDirection === 'left' ? '-' : '',
  383. },
  384. {
  385. x0: fractionX,
  386. y0: curCircleY + 2,
  387. x1: fractionX + 25,
  388. y1: curCircleY + 2,
  389. lineWidth: 2,
  390. color: curLineColor,
  391. },
  392. ];
  393. }
  394. for (let i = 0; i < 2; i++) {
  395. const item = game.add.text(
  396. curFractionItems[i].x,
  397. curFractionItems[i].y,
  398. curFractionItems[i].text,
  399. font
  400. );
  401. item.lineHeight = 37;
  402. curCircleInfo.fraction.labels.push(item);
  403. }
  404. if (curFractionItems[2]) {
  405. const line = game.add.geom.line(
  406. curFractionItems[2].x0,
  407. curFractionItems[2].y0,
  408. curFractionItems[2].x1,
  409. curFractionItems[2].y1,
  410. curFractionItems[2].lineWidth,
  411. curFractionItems[2].color
  412. );
  413. line.anchor(0.5, 0);
  414. curCircleInfo.fraction.labels.push(line);
  415. } else {
  416. curCircleInfo.fraction.labels.push(null);
  417. }
  418. curCircleInfo.fraction.nominator = curCircleInfo.direc;
  419. curCircleInfo.fraction.denominator = curDivisor;
  420. if (!showFractions) {
  421. curCircleInfo.fraction.labels.forEach((label) => {
  422. if (label) label.alpha = 0;
  423. });
  424. }
  425. curCircle.rotate = 90;
  426. // If game is type (b) (select fractions)
  427. if (gameMode === 'b') {
  428. curCircle.index = i;
  429. }
  430. curCircleInfo.distance = Math.floor(
  431. validPath.distanceBetweenPoints / curDivisor
  432. );
  433. // Add to the list
  434. curCircle.info = curCircleInfo;
  435. self.circles.list.push(curCircle);
  436. self.control.correctX +=
  437. Math.floor(validPath.distanceBetweenPoints / curDivisor) *
  438. curCircle.info.direc;
  439. }
  440. // Restart if
  441. // Does not have at least one fraction of type 1/difficulty
  442. if (!hasBaseDifficulty) {
  443. restart = true;
  444. }
  445. // Calculate next circle
  446. self.control.nextX =
  447. validPath.x0 +
  448. self.circles.list[0].info.distance * self.circles.list[0].info.direc;
  449. let isBeforeMin = (isAfterMax = false);
  450. let finalPosition = self.control.correctX;
  451. // Restart if
  452. // In Game mode 'a' and 'b' : Balloon position is out of bounds
  453. if (gameOperation === 'minus') {
  454. isBeforeMin = finalPosition > validPath.x0;
  455. isAfterMax =
  456. finalPosition < validPath.x0 - 5 * validPath.distanceBetweenPoints;
  457. } else {
  458. isBeforeMin = finalPosition < validPath.x0;
  459. isAfterMax =
  460. finalPosition > validPath.x0 + 5 * validPath.distanceBetweenPoints;
  461. }
  462. if (isBeforeMin || isAfterMax) restart = true;
  463. if (gameMode === 'b') {
  464. // If game is type (b), select a random balloon place
  465. balloonX = validPath.x0;
  466. self.control.correctIndex = game.math.randomInRange(
  467. self.control.numberOfPlusFractions,
  468. self.circles.list.length
  469. );
  470. for (let i = 0; i < self.control.correctIndex; i++) {
  471. balloonX +=
  472. self.circles.list[i].info.distance *
  473. self.circles.list[i].info.direc;
  474. }
  475. finalPosition = balloonX;
  476. self.blocks.list.forEach((cur) => {
  477. self.utils.fillCurrentBlock(balloonX, cur.x, cur);
  478. if (self.utils.isOverBlock(balloonX, cur.x, cur.width, cur))
  479. self.blocks.cur = cur;
  480. });
  481. // Restart if
  482. // In Game mode 'b' : Top circle position is out of bounds (when on the ground)
  483. if (gameOperation === 'minus') {
  484. isBeforeMin = finalPosition > validPath.x0;
  485. isAfterMax =
  486. finalPosition < validPath.x0 - 5 * validPath.distanceBetweenPoints;
  487. } else {
  488. isBeforeMin = finalPosition < validPath.x0;
  489. isAfterMax =
  490. finalPosition > validPath.x0 + 5 * validPath.distanceBetweenPoints;
  491. }
  492. if (isBeforeMin || isAfterMax) restart = true;
  493. }
  494. return [restart, balloonX];
  495. },
  496. renderCharacters: function (validPath, balloonX) {
  497. // KID
  498. self.kid = game.add.sprite(
  499. validPath.x0,
  500. validPath.y0 - 32 - self.circles.list.length * self.circles.diameter,
  501. 'kid_walking',
  502. 0,
  503. 1.2
  504. );
  505. self.kid.anchor(0.5, 0.8);
  506. self.animation.list.right = [
  507. 'right',
  508. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
  509. 4,
  510. ];
  511. self.animation.list.left = [
  512. 'left',
  513. [23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12],
  514. 4,
  515. ];
  516. if (gameOperation === 'minus') {
  517. self.kid.animation = self.animation.list.left;
  518. self.kid.curFrame = 23;
  519. } else {
  520. self.kid.animation = self.animation.list.right;
  521. }
  522. // BALLOON
  523. self.balloon = game.add.image(
  524. balloonX,
  525. validPath.y0 - 295,
  526. 'balloon',
  527. 1.5,
  528. 0.5
  529. );
  530. self.balloon.alpha = 0.5;
  531. self.balloon.anchor(0.5, 0.5);
  532. self.basket = game.add.image(
  533. balloonX,
  534. validPath.y0 - 95,
  535. 'balloon_basket',
  536. 1.5
  537. );
  538. self.basket.alpha = 0.8;
  539. self.basket.anchor(0.5, 0.5);
  540. },
  541. renderMainUI: function () {
  542. // Help pointer
  543. self.ui.help = game.add.image(0, 0, 'pointer', 2, 0);
  544. // Intro text
  545. const correctMessage =
  546. gameMode === 'a'
  547. ? game.lang.circleOne_intro_a
  548. : game.lang.circleOne_intro_b;
  549. const treatedMessage = correctMessage.split('\\n');
  550. self.ui.message = [];
  551. self.ui.message.push(
  552. game.add.text(
  553. context.canvas.width / 2,
  554. 170,
  555. treatedMessage[0] + '\n' + treatedMessage[1],
  556. textStyles.h1_
  557. )
  558. );
  559. },
  560. renderOperationUI: function () {
  561. /**
  562. * if game mode A:
  563. * - left: selected balloon position (user selection)
  564. * - right: line created from the stack of arcs (pre-set)
  565. *
  566. * if game mode B:
  567. * - left: line created from the stack of arcs (user selection)
  568. * - right: baloon position (pre-set)
  569. */
  570. let validCircles = self.circles.list;
  571. if (gameMode === 'b') {
  572. validCircles = [];
  573. for (let i = 0; i <= self.control.selectedIndex; i++) {
  574. validCircles.push(self.circles.list[i]);
  575. }
  576. }
  577. const font = textStyles.fraction;
  578. font.fill = colors.green;
  579. const nominators = [];
  580. const denominators = [];
  581. const renderList = [];
  582. const padding = 100;
  583. const offsetX = 100;
  584. const widthOfChar = 35;
  585. const x0 = padding;
  586. const y0 = context.canvas.height / 3;
  587. let nextX = x0;
  588. const cardHeight = 400;
  589. const cardX = x0 - padding;
  590. const cardY = y0;
  591. // Card
  592. const card = game.add.geom.rect(
  593. cardX,
  594. cardY,
  595. 0,
  596. cardHeight,
  597. colors.blueLight,
  598. 0.5,
  599. colors.blueDark,
  600. 8
  601. );
  602. card.id = 'card';
  603. card.anchor(0, 0.5);
  604. renderList.push(card);
  605. // Fraction list
  606. for (let i in validCircles) {
  607. const curFraction = validCircles[i].info.fraction;
  608. const curFractionString = curFraction.labels[0].name;
  609. let curFractionSign = i !== '0' ? '+' : '';
  610. if (curFraction.labels[1].name === '-') {
  611. curFractionSign = '-';
  612. font.fill = colors.red;
  613. }
  614. const fractionText = game.add.text(
  615. x0 + i * offsetX + offsetX / 2,
  616. curFractionString === '1' ? y0 + 40 : y0,
  617. curFractionString,
  618. font
  619. );
  620. fractionText.lineHeight = 70;
  621. renderList.push(
  622. game.add.text(x0 + i * offsetX, y0 + 35, curFractionSign, font)
  623. );
  624. renderList.push(fractionText);
  625. renderList.push(
  626. game.add.text(
  627. x0 + offsetX / 2 + i * offsetX,
  628. y0,
  629. curFractionString === '1' ? '' : '_',
  630. font
  631. )
  632. );
  633. nominators.push(curFraction.nominator);
  634. denominators.push(curFraction.denominator);
  635. }
  636. // Setup for fraction operation with least common multiple
  637. font.fill = colors.black;
  638. const updatedNominators = [];
  639. const mmc = game.math.mmcArray(denominators);
  640. let resultNominator = 0;
  641. for (let i in nominators) {
  642. const temp = nominators[i] * (mmc / denominators[i]);
  643. updatedNominators.push(temp);
  644. resultNominator += temp;
  645. }
  646. const resultNominatorUnsigned =
  647. resultNominator < 0 ? -resultNominator : resultNominator;
  648. const resultAux = resultNominator / mmc;
  649. const result =
  650. ('' + resultAux).length > 4 ? resultAux.toFixed(2) : resultAux;
  651. // Fraction operation with least common multiple
  652. nextX = x0 + validCircles.length * offsetX + 20;
  653. // Fraction result
  654. renderList.push(game.add.text(nextX, y0 + 35, '=', font));
  655. font.align = 'center';
  656. nextX += offsetX;
  657. if (result < 0) {
  658. nextX -= 30;
  659. renderList.push(game.add.text(nextX, y0 + 35, '-', font));
  660. nextX += 60;
  661. }
  662. const fractionResult = game.add.text(
  663. nextX,
  664. mmc === 1 || resultNominatorUnsigned === 0 ? y0 + 40 : y0,
  665. mmc === 1 || resultNominatorUnsigned === 0
  666. ? resultNominatorUnsigned
  667. : resultNominatorUnsigned + '\n' + mmc,
  668. font
  669. );
  670. fractionResult.lineHeight = 70;
  671. renderList.push(fractionResult);
  672. const fractionLine = game.add.geom.line(
  673. nextX,
  674. y0 + 15,
  675. nextX + 60,
  676. y0 + 15,
  677. 4,
  678. colors.black,
  679. mmc === 1 || resultNominatorUnsigned === 0 ? 0 : 1
  680. );
  681. fractionLine.anchor(0.5, 0);
  682. renderList.push(fractionLine);
  683. // Fraction result simplified setup
  684. const mdcAux = game.math.mdc(resultNominator, mmc);
  685. const mdc = mdcAux < 0 ? -mdcAux : mdcAux;
  686. if (mdc !== 1 && resultNominatorUnsigned !== 0) {
  687. // alert(mdc + ' ' + resultNominatorUnsigned);
  688. nextX += offsetX;
  689. renderList.push(game.add.text(nextX, y0 + 35, '=', font));
  690. nextX += offsetX;
  691. renderList.push(
  692. game.add.text(nextX - 50, y0 + 35, result > 0 ? '' : '-', font)
  693. );
  694. renderList.push(
  695. game.add.text(nextX, y0, resultNominatorUnsigned / mdc, font)
  696. );
  697. renderList.push(game.add.text(nextX, y0 + 70, mmc / mdc, font));
  698. const fractionLine = game.add.geom.line(
  699. nextX,
  700. y0 + 15,
  701. nextX + 60,
  702. y0 + 15,
  703. 4,
  704. colors.black
  705. );
  706. fractionLine.anchor(0.5, 0);
  707. renderList.push(fractionLine);
  708. }
  709. // Decimal result
  710. let resultWidth = '_'.length * widthOfChar;
  711. const cardWidth = nextX - x0 + resultWidth + padding * 2;
  712. card.width = cardWidth;
  713. const endSignX = (context.canvas.width - cardWidth) / 2 + cardWidth;
  714. // Center Card
  715. moveList(renderList, (context.canvas.width - cardWidth) / 2, 0);
  716. self.fractionOperationUI = renderList;
  717. return endSignX;
  718. },
  719. renderOperationUI_new: () => {
  720. /**
  721. * if game mode A:
  722. * - left: selected balloon position (user selection)
  723. * - right: correct sum of stack of arcs (pre-set)
  724. *
  725. * if game mode B:
  726. * - left: line created from the stack of arcs (user selection)
  727. * - right: baloon position (pre-set)
  728. */
  729. const divisor = gameDifficulty == 3 ? 4 : gameDifficulty;
  730. const renderFloorFractions = (lastIndex) => {
  731. const divisor = gameDifficulty == 3 ? 4 : gameDifficulty;
  732. const operator = gameOperation === 'minus' ? '-' : '+';
  733. const index = lastIndex;
  734. const blocks = index + 1;
  735. const valueReal = blocks / divisor;
  736. const valueFloor = Math.floor(valueReal);
  737. const valueRest = valueReal - valueFloor;
  738. let fracNomin = (fracDenomin = fracLine = '');
  739. // adds sign on the left of the equation
  740. if (gameOperation === 'minus') {
  741. fracNomin += ' ';
  742. fracDenomin += ' ';
  743. fracLine += operator;
  744. }
  745. // 1 _ _
  746. if (valueFloor) {
  747. fracNomin += ' ';
  748. fracDenomin += ' ';
  749. fracLine += valueFloor;
  750. }
  751. // _ + _
  752. if (valueFloor && valueRest) {
  753. fracNomin += ' ';
  754. fracDenomin += ' ';
  755. fracLine += operator;
  756. }
  757. // _ _ 1/5
  758. if (valueRest) {
  759. fracNomin += `${valueRest * divisor}`;
  760. fracDenomin += `${divisor}`;
  761. fracLine += '-';
  762. }
  763. return [fracNomin, fracDenomin, fracLine, valueReal];
  764. };
  765. const renderStackFractions = (lastIndex) => {
  766. const operator = gameOperation === 'minus' ? '-' : '+';
  767. const index = lastIndex;
  768. const blocks = index + 1;
  769. const nominators = [];
  770. const denominators = [];
  771. const values = [];
  772. let valueReal = 0;
  773. let fracNomin = (fracDenomin = fracLine = '');
  774. for (let i = 0; i < blocks; i++) {
  775. const m = self.circles.list[i].info.fraction.denominator || 1;
  776. const temp = self.circles.list[i].info.fraction.nominator || 0;
  777. const n = gameOperation === 'minus' ? -temp : +temp;
  778. const nm = n / m;
  779. nominators[i] = n + 0;
  780. denominators[i] = m + 0;
  781. values[i] = nm;
  782. valueReal += nm;
  783. }
  784. for (let i = 0; i < blocks; i++) {
  785. const valueReal = values[i];
  786. const valueFloor = Math.floor(valueReal);
  787. const valueRest = valueReal - valueFloor;
  788. if (i > 0 || gameOperation === 'minus') {
  789. fracNomin += ' ';
  790. fracDenomin += ' ';
  791. fracLine += operator;
  792. }
  793. if (valueFloor && !valueRest) {
  794. fracNomin += ' ';
  795. fracDenomin += ' ';
  796. fracLine += valueFloor;
  797. }
  798. if (valueRest) {
  799. fracNomin += `${nominators[i]}`;
  800. fracDenomin += `${denominators[i]}`;
  801. fracLine += '-';
  802. }
  803. }
  804. return [fracNomin, fracDenomin, fracLine, valueReal];
  805. };
  806. // Initial setup
  807. const font = textStyles.fraction;
  808. font.fill = colors.black;
  809. const padding = 100;
  810. const offsetX = 100;
  811. const widthOfChar = 35;
  812. const x0 = padding;
  813. const y0 = context.canvas.height / 3;
  814. let nextX = x0;
  815. const cardHeight = 400;
  816. const cardX = x0 - padding;
  817. const cardY = y0;
  818. const renderList = [];
  819. // Render Card
  820. const card = game.add.geom.rect(
  821. cardX,
  822. cardY,
  823. 0,
  824. cardHeight,
  825. colors.blueLight,
  826. 0.5,
  827. colors.blueDark,
  828. 8
  829. );
  830. card.id = 'card';
  831. card.anchor(0, 0.5);
  832. renderList.push(card);
  833. // Fraction setup
  834. const [floorNominators, floorDenominators, floorLines, floorValue] =
  835. renderFloorFractions(self.blocks.cur.info.index);
  836. const [stackNominators, stackDenominators, stackLines, stackValue] =
  837. renderStackFractions(self.circles.cur - 1);
  838. const renderFloorOperationLine = (x) => {
  839. font.fill = colors.black;
  840. const floorNom = game.add.text(
  841. x + offsetX / 2,
  842. y0,
  843. floorNominators,
  844. font,
  845. 60
  846. );
  847. const floorDenom = game.add.text(
  848. x + offsetX / 2,
  849. y0 + 70,
  850. floorDenominators,
  851. font,
  852. 60
  853. );
  854. const floorLin = game.add.text(
  855. x + offsetX / 2,
  856. y0 + 35,
  857. floorLines,
  858. font,
  859. 60
  860. );
  861. renderList.push(floorNom);
  862. renderList.push(floorDenom);
  863. renderList.push(floorLin);
  864. };
  865. const renderStackOperationLine = (x) => {
  866. font.fill = colors.black;
  867. const stackNom = game.add.text(
  868. x + offsetX / 2,
  869. y0,
  870. stackNominators,
  871. font,
  872. 60
  873. );
  874. const stackDenom = game.add.text(
  875. x + offsetX / 2,
  876. y0 + 70,
  877. stackDenominators,
  878. font,
  879. 60
  880. );
  881. const stackLin = game.add.text(
  882. x + offsetX / 2,
  883. y0 + 35,
  884. stackLines,
  885. font,
  886. 60
  887. );
  888. renderList.push(stackNom);
  889. renderList.push(stackDenom);
  890. renderList.push(stackLin);
  891. };
  892. // Render LEFT part of the operation
  893. if (gameMode === 'a') renderFloorOperationLine(x0);
  894. else renderStackOperationLine(x0);
  895. let curNominators = gameMode === 'a' ? floorNominators : stackNominators;
  896. nextX = x0 + (curNominators.length + 2) * widthOfChar;
  897. // Render middle sign - equal by default
  898. font.fill = colors.green;
  899. let comparisonSign = '=';
  900. // Render middle sign - if not equal
  901. if (floorValue != stackValue) {
  902. font.fill = colors.red;
  903. let leftSideIsLarger = floorValue > stackValue;
  904. if (gameMode === 'b') leftSideIsLarger = !leftSideIsLarger;
  905. if (gameOperation === 'minus') leftSideIsLarger = !leftSideIsLarger;
  906. comparisonSign = leftSideIsLarger ? '>' : '<';
  907. }
  908. renderList.push(game.add.text(nextX, y0 + 35, comparisonSign, font));
  909. // Render RIGHT part of the operationf
  910. if (gameMode === 'a') renderStackOperationLine(nextX);
  911. else renderFloorOperationLine(nextX);
  912. curNominators = gameMode === 'a' ? stackNominators : floorNominators;
  913. const resultWidth = (curNominators.length + 2) * widthOfChar;
  914. const cardWidth = nextX - x0 + resultWidth + padding * 2;
  915. card.width = cardWidth;
  916. const endSignX = (context.canvas.width - cardWidth) / 2 + cardWidth;
  917. // Center Card
  918. moveList(renderList, (context.canvas.width - cardWidth) / 2, 0);
  919. self.fractionOperationUI = renderList;
  920. return endSignX;
  921. },
  922. renderEndUI: () => {
  923. let btnColor = colors.green;
  924. let btnText = game.lang.continue;
  925. if (!self.control.isCorrect) {
  926. btnColor = colors.red;
  927. btnText = game.lang.retry;
  928. }
  929. // continue button
  930. self.ui.continue.button = game.add.geom.rect(
  931. context.canvas.width / 2,
  932. context.canvas.height / 2 + 100,
  933. 450,
  934. 100,
  935. btnColor
  936. );
  937. self.ui.continue.button.anchor(0.5, 0.5);
  938. self.ui.continue.text = game.add.text(
  939. context.canvas.width / 2,
  940. context.canvas.height / 2 + 16 + 100,
  941. btnText,
  942. textStyles.btn
  943. );
  944. },
  945. // UPDATE
  946. animateKidHandler: function () {
  947. let canLowerCircles = undefined;
  948. let curCircle = self.circles.list[self.circles.cur];
  949. let curDirec = curCircle.info.direc;
  950. // Move
  951. self.circles.list.forEach((circle) => {
  952. circle.x += self.animation.walkOffsetX * curDirec;
  953. });
  954. self.kid.x += self.animation.walkOffsetX * curDirec;
  955. self.walkedPath[self.control.curWalkedPath].width +=
  956. self.animation.walkOffsetX * curDirec;
  957. // Update arc
  958. curCircle.info.angleDegree += self.animation.angleOffset * curDirec;
  959. curCircle.angleEnd = game.math.degreeToRad(curCircle.info.angleDegree);
  960. // When finish current circle
  961. if (curCircle.info.direction === 'right') {
  962. canLowerCircles = curCircle.x >= self.control.nextX;
  963. } else if (curCircle.info.direction === 'left') {
  964. canLowerCircles = curCircle.x <= self.control.nextX;
  965. // If just changed from 'right' to 'left' inform to change direction of kid animation
  966. if (
  967. self.animation.invertDirection === undefined &&
  968. self.circles.cur > 0 &&
  969. self.circles.list[self.circles.cur - 1].info.direction === 'right'
  970. ) {
  971. self.animation.invertDirection = true;
  972. }
  973. }
  974. // Change direction of kid animation
  975. if (self.animation.invertDirection) {
  976. self.animation.invertDirection = false;
  977. game.animation.stop(self.kid.animation[0]);
  978. self.kid.animation = self.animation.list.left;
  979. self.kid.curFrame = 23;
  980. game.animation.play('left');
  981. self.control.curWalkedPath = 1;
  982. self.utils.renderWalkedPath(
  983. curCircle.x,
  984. self.walkedPath[0].y + 8,
  985. colors.red
  986. );
  987. }
  988. if (canLowerCircles) {
  989. // Hide current circle
  990. curCircle.alpha = 0;
  991. // Lowers kid and other circles
  992. self.circles.list.forEach((circle) => {
  993. circle.y += self.circles.diameter;
  994. });
  995. self.kid.y += self.circles.diameter;
  996. self.circles.cur++; // Update index of current circle
  997. if (self.circles.list[self.circles.cur]) {
  998. curCircle = self.circles.list[self.circles.cur];
  999. curDirec = curCircle.info.direc;
  1000. self.control.nextX += curCircle.info.distance * curDirec; // Update next position
  1001. }
  1002. }
  1003. // When finish all circles (final position)
  1004. if (
  1005. self.circles.cur === self.circles.list.length ||
  1006. curCircle.alpha === 0
  1007. ) {
  1008. self.animation.animateKid = false;
  1009. self.control.checkAnswer = true;
  1010. }
  1011. },
  1012. checkAnswerHandler: function () {
  1013. game.timer.stop();
  1014. game.animation.stop(self.kid.animation[0]);
  1015. self.control.isCorrect = game.math.isOverlap(self.basket, self.kid);
  1016. const x = self.utils.renderOperationUI_new();
  1017. if (self.control.isCorrect) {
  1018. completedLevels++;
  1019. self.kid.curFrame = self.kid.curFrame < 12 ? 24 : 25;
  1020. if (audioStatus) game.audio.okSound.play();
  1021. game.add
  1022. .image(x + 50, context.canvas.height / 3, 'answer_correct')
  1023. .anchor(0.5, 0.5);
  1024. if (isDebugMode) console.log('Completed Levels: ' + completedLevels);
  1025. } else {
  1026. if (audioStatus) game.audio.errorSound.play();
  1027. game.add
  1028. .image(x, context.canvas.height / 3, 'answer_wrong')
  1029. .anchor(0.5, 0.5);
  1030. }
  1031. self.fetch.postScore();
  1032. self.control.checkAnswer = false;
  1033. self.animation.counter = 0;
  1034. self.animation.animateBalloon = true;
  1035. },
  1036. animateBalloonHandler: function () {
  1037. self.animation.counter++;
  1038. self.balloon.y -= 2;
  1039. self.basket.y -= 2;
  1040. if (self.control.isCorrect) self.kid.y -= 2;
  1041. if (self.animation.counter === 100) {
  1042. self.utils.renderEndUI();
  1043. self.control.showEndInfo = true;
  1044. if (self.control.isCorrect) canGoToNextMapPosition = true;
  1045. else canGoToNextMapPosition = false;
  1046. }
  1047. },
  1048. endLevel: function () {
  1049. game.state.start('map');
  1050. },
  1051. // INFORMATION
  1052. /**
  1053. * Show correct answer
  1054. */
  1055. showAnswer: function () {
  1056. if (!self.control.hasClicked) {
  1057. // On gameMode (a)
  1058. if (gameMode === 'a') {
  1059. self.ui.help.x = self.control.correctX - 20;
  1060. self.ui.help.y = self.road.defaultY;
  1061. // On gameMode (b)
  1062. } else {
  1063. self.ui.help.x = self.circles.list[self.control.correctIndex - 1].x;
  1064. self.ui.help.y = self.circles.list[self.control.correctIndex - 1].y; // - self.circles.diameter / 2;
  1065. }
  1066. self.ui.help.alpha = 1;
  1067. }
  1068. },
  1069. // HANDLERS
  1070. /**
  1071. * (in gameMode 'b') Function called when player clicked over a valid circle
  1072. *
  1073. * @param {number|object} cur clicked circle
  1074. */
  1075. clickCircleHandler: function (cur) {
  1076. if (!self.control.hasClicked) {
  1077. // On gameMode (a)
  1078. if (gameMode === 'a') {
  1079. self.balloon.x = cur;
  1080. self.basket.x = cur;
  1081. }
  1082. // On gameMode (b)
  1083. if (gameMode === 'b') {
  1084. document.body.style.cursor = 'auto';
  1085. for (let i in self.circles.list) {
  1086. if (i <= cur.index) {
  1087. self.circles.list[i].alpha = 1; // Keep selected circle
  1088. self.control.selectedIndex = cur.index;
  1089. } else {
  1090. self.circles.list[i].alpha = 0; // Hide unselected circle
  1091. self.kid.y += self.circles.diameter; // Lower kid to selected circle
  1092. }
  1093. }
  1094. }
  1095. if (audioStatus) game.audio.popSound.play();
  1096. // Hide fractions
  1097. if (showFractions) {
  1098. self.circles.list.forEach((circle) => {
  1099. circle.info.fraction.labels.forEach((labelPart) => {
  1100. if (labelPart) labelPart.alpha = 0;
  1101. });
  1102. });
  1103. }
  1104. // Hide solution pointer
  1105. if (self.ui.help != undefined) self.ui.help.alpha = 0;
  1106. self.ui.message[0].alpha = 0;
  1107. navigation.disableIcon(navigation.showAnswerIcon);
  1108. self.balloon.alpha = 1;
  1109. self.basket.alpha = 1;
  1110. self.walkedPath[self.control.curWalkedPath].alpha = 1;
  1111. self.control.hasClicked = true;
  1112. self.animation.animateKid = true;
  1113. game.animation.play(self.kid.animation[0]);
  1114. }
  1115. },
  1116. /**
  1117. * (in gameMode 'b') Function called when cursor is over a valid circle
  1118. *
  1119. * @param {object} cur circle the cursor is over
  1120. */
  1121. overCircleHandler: function (cur) {
  1122. if (!self.control.hasClicked) {
  1123. document.body.style.cursor = 'pointer';
  1124. for (let i in self.circles.list) {
  1125. const alpha = i <= cur.index ? 1 : 0.4;
  1126. self.circles.list[i].alpha = alpha;
  1127. if (showFractions) {
  1128. self.circles.list[i].info.fraction.labels.forEach((lbl) => {
  1129. if (lbl) lbl.alpha = alpha;
  1130. });
  1131. }
  1132. }
  1133. }
  1134. },
  1135. /**
  1136. * (in gameMode 'b') Function called when cursor leaves a valid circle
  1137. */
  1138. outCircleHandler: function () {
  1139. if (!self.control.hasClicked) {
  1140. document.body.style.cursor = 'auto';
  1141. const alpha = 1;
  1142. self.circles.list.forEach((circle) => {
  1143. circle.alpha = alpha;
  1144. if (showFractions) {
  1145. circle.info.fraction.labels.forEach((lbl) => {
  1146. if (lbl) lbl.alpha = alpha;
  1147. });
  1148. }
  1149. });
  1150. }
  1151. },
  1152. /** TODO */
  1153. isOverBlock: function (x, blockX, blockWidth) {
  1154. if (
  1155. ((gameOperation === 'plus' || gameOperation === 'mixed') &&
  1156. x >= blockX &&
  1157. x < blockX + blockWidth) ||
  1158. (gameOperation === 'minus' && x <= blockX && x > blockX + blockWidth)
  1159. )
  1160. return true;
  1161. return false;
  1162. },
  1163. /** TODO */
  1164. isOverRoad: function (x, y, roadX, roadWidth) {
  1165. if (y > 150) {
  1166. if (
  1167. ((gameOperation === 'plus' || gameOperation === 'mixed') &&
  1168. x >= roadX &&
  1169. x < roadX + roadWidth) ||
  1170. (gameOperation === 'minus' &&
  1171. x <= roadX &&
  1172. x > roadX + roadWidth * self.control.directionModifier)
  1173. )
  1174. return true;
  1175. }
  1176. return false;
  1177. },
  1178. /** TODO */
  1179. fillCurrentBlock: function (x, blockX, block) {
  1180. block.fillColor =
  1181. ((gameOperation === 'plus' || gameOperation === 'mixed') &&
  1182. x > blockX) ||
  1183. (gameOperation === 'minus' && x < blockX)
  1184. ? colors.red
  1185. : 'transparent';
  1186. },
  1187. },
  1188. events: {
  1189. /**
  1190. * Called by mouse click event
  1191. *
  1192. * @param {object} mouseEvent contains the mouse click coordinates
  1193. */
  1194. onInputDown: function (mouseEvent) {
  1195. const x = game.math.getMouse(mouseEvent).x;
  1196. const y = game.math.getMouse(mouseEvent).y;
  1197. // GAME MODE A : click road
  1198. if (gameMode === 'a') {
  1199. const isValidX = self.utils.isOverRoad(
  1200. x,
  1201. y,
  1202. self.road.x,
  1203. self.road.width
  1204. );
  1205. if (isValidX) {
  1206. self.utils.clickCircleHandler(
  1207. self.blocks.cur.x + self.blocks.cur.width
  1208. );
  1209. }
  1210. }
  1211. // GAME MODE B : click circle
  1212. if (gameMode === 'b') {
  1213. self.circles.list.forEach((circle) => {
  1214. const isValid =
  1215. game.math.distanceToPointer(
  1216. x,
  1217. circle.xWithAnchor,
  1218. y,
  1219. circle.yWithAnchor
  1220. ) <=
  1221. (circle.diameter / 2) * circle.scale;
  1222. if (isValid) self.utils.clickCircleHandler(circle);
  1223. });
  1224. }
  1225. // Continue button
  1226. if (self.control.showEndInfo) {
  1227. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  1228. if (audioStatus) game.audio.popSound.play();
  1229. self.utils.endLevel();
  1230. }
  1231. }
  1232. navigation.onInputDown(x, y);
  1233. game.render.all();
  1234. },
  1235. /**
  1236. * Called by mouse move event
  1237. *
  1238. * @param {object} mouseEvent contains the mouse move coordinates
  1239. */
  1240. onInputOver: function (mouseEvent) {
  1241. const x = game.math.getMouse(mouseEvent).x;
  1242. const y = game.math.getMouse(mouseEvent).y;
  1243. let isOverCircle = false;
  1244. if (gameMode === 'a' && !self.control.hasClicked) {
  1245. const isValidX = self.utils.isOverRoad(
  1246. x,
  1247. y,
  1248. self.road.x,
  1249. self.road.width
  1250. );
  1251. if (isValidX) {
  1252. // GAME MODE A : balloon follow mouse
  1253. self.blocks.cur = self.blocks.list[0];
  1254. self.blocks.list.forEach((cur) => {
  1255. self.utils.fillCurrentBlock(x, cur.x, cur);
  1256. if (self.utils.isOverBlock(x, cur.x, cur.width, cur))
  1257. self.blocks.cur = cur;
  1258. });
  1259. const newX = self.blocks.cur.x + self.blocks.cur.width;
  1260. self.balloon.x = newX;
  1261. self.basket.x = newX;
  1262. document.body.style.cursor = 'auto';
  1263. }
  1264. }
  1265. // GAME MODE B : hover circle
  1266. if (gameMode === 'b' && !self.control.hasClicked) {
  1267. self.circles.list.forEach((circle) => {
  1268. const isValid =
  1269. game.math.distanceToPointer(
  1270. x,
  1271. circle.xWithAnchor,
  1272. y,
  1273. circle.yWithAnchor
  1274. ) <=
  1275. (circle.diameter / 2) * circle.scale;
  1276. if (isValid) {
  1277. self.utils.overCircleHandler(circle);
  1278. isOverCircle = true;
  1279. }
  1280. });
  1281. if (!isOverCircle) self.utils.outCircleHandler();
  1282. }
  1283. // Continue button
  1284. if (self.control.showEndInfo) {
  1285. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  1286. // If pointer is over icon
  1287. document.body.style.cursor = 'pointer';
  1288. self.ui.continue.button.scale =
  1289. self.ui.continue.button.initialScale * 1.1;
  1290. self.ui.continue.text.style = textStyles.btnLg;
  1291. } else {
  1292. // If pointer is not over icon
  1293. document.body.style.cursor = 'auto';
  1294. self.ui.continue.button.scale =
  1295. self.ui.continue.button.initialScale * 1;
  1296. self.ui.continue.text.style = textStyles.btn;
  1297. }
  1298. }
  1299. navigation.onInputOver(x, y);
  1300. game.render.all();
  1301. },
  1302. },
  1303. fetch: {
  1304. /**
  1305. * Saves players data after level ends - to be sent to database <br>
  1306. *
  1307. * Attention: the 'line_' prefix data table must be compatible to data table fields (MySQL server)
  1308. *
  1309. * @see /php/squareOne.js
  1310. */
  1311. postScore: function () {
  1312. // Creates string that is going to be sent to db
  1313. const data =
  1314. '&line_game=' +
  1315. gameShape +
  1316. '&line_mode=' +
  1317. gameMode +
  1318. '&line_oper=' +
  1319. gameOperation +
  1320. '&line_leve=' +
  1321. gameDifficulty +
  1322. '&line_posi=' +
  1323. curMapPosition +
  1324. '&line_resu=' +
  1325. self.control.isCorrect +
  1326. '&line_time=' +
  1327. game.timer.elapsed +
  1328. '&line_deta=' +
  1329. 'numCircles:' +
  1330. self.circles.list.length +
  1331. ', valCircles: ' +
  1332. self.control.divisorsList +
  1333. ' balloonX: ' +
  1334. self.basket.x +
  1335. ', selIndex: ' +
  1336. self.control.selectedIndex;
  1337. // FOR MOODLE
  1338. sendToDatabase(data);
  1339. },
  1340. },
  1341. };