circleOne.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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. // Restart if
  477. // In Game mode 'b' : Top circle position is out of bounds (when on the ground)
  478. if (gameOperation === 'minus') {
  479. isBeforeMin = finalPosition > validPath.x0;
  480. isAfterMax =
  481. finalPosition < validPath.x0 - 5 * validPath.distanceBetweenPoints;
  482. } else {
  483. isBeforeMin = finalPosition < validPath.x0;
  484. isAfterMax =
  485. finalPosition > validPath.x0 + 5 * validPath.distanceBetweenPoints;
  486. }
  487. if (isBeforeMin || isAfterMax) restart = true;
  488. }
  489. return [restart, balloonX];
  490. },
  491. renderCharacters: function (validPath, balloonX) {
  492. // KID
  493. self.kid = game.add.sprite(
  494. validPath.x0,
  495. validPath.y0 - 32 - self.circles.list.length * self.circles.diameter,
  496. 'kid_walking',
  497. 0,
  498. 1.2
  499. );
  500. self.kid.anchor(0.5, 0.8);
  501. self.animation.list.right = [
  502. 'right',
  503. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
  504. 4,
  505. ];
  506. self.animation.list.left = [
  507. 'left',
  508. [23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12],
  509. 4,
  510. ];
  511. if (gameOperation === 'minus') {
  512. self.kid.animation = self.animation.list.left;
  513. self.kid.curFrame = 23;
  514. } else {
  515. self.kid.animation = self.animation.list.right;
  516. }
  517. // BALLOON
  518. self.balloon = game.add.image(
  519. balloonX,
  520. validPath.y0 - 295,
  521. 'balloon',
  522. 1.5,
  523. 0.5
  524. );
  525. self.balloon.alpha = 0.5;
  526. self.balloon.anchor(0.5, 0.5);
  527. self.basket = game.add.image(
  528. balloonX,
  529. validPath.y0 - 95,
  530. 'balloon_basket',
  531. 1.5
  532. );
  533. self.basket.alpha = 0.8;
  534. self.basket.anchor(0.5, 0.5);
  535. },
  536. renderMainUI: function () {
  537. // Help pointer
  538. self.ui.help = game.add.image(0, 0, 'pointer', 2, 0);
  539. // Intro text
  540. const correctMessage =
  541. gameMode === 'a'
  542. ? game.lang.circleOne_intro_a
  543. : game.lang.circleOne_intro_b;
  544. const treatedMessage = correctMessage.split('\\n');
  545. self.ui.message = [];
  546. self.ui.message.push(
  547. game.add.text(
  548. context.canvas.width / 2,
  549. 170,
  550. treatedMessage[0] + '\n' + treatedMessage[1],
  551. textStyles.h1_
  552. )
  553. );
  554. },
  555. renderOperationUI: function () {
  556. /**
  557. * if game mode A:
  558. * - left: selected balloon position (user selection)
  559. * - right: line created from the stack of arcs (pre-set)
  560. *
  561. * if game mode B:
  562. * - left: line created from the stack of arcs (user selection)
  563. * - right: baloon position (pre-set)
  564. */
  565. let validCircles = self.circles.list;
  566. if (gameMode === 'b') {
  567. validCircles = [];
  568. for (let i = 0; i <= self.control.selectedIndex; i++) {
  569. validCircles.push(self.circles.list[i]);
  570. }
  571. }
  572. const font = textStyles.fraction;
  573. font.fill = colors.green;
  574. const nominators = [];
  575. const denominators = [];
  576. const renderList = [];
  577. const padding = 100;
  578. const offsetX = 100;
  579. const widthOfChar = 35;
  580. const x0 = padding;
  581. const y0 = context.canvas.height / 3;
  582. let nextX = x0;
  583. const cardHeight = 400;
  584. const cardX = x0 - padding;
  585. const cardY = y0;
  586. // Card
  587. const card = game.add.geom.rect(
  588. cardX,
  589. cardY,
  590. 0,
  591. cardHeight,
  592. colors.blueLight,
  593. 0.5,
  594. colors.blueDark,
  595. 8
  596. );
  597. card.id = 'card';
  598. card.anchor(0, 0.5);
  599. renderList.push(card);
  600. // Fraction list
  601. for (let i in validCircles) {
  602. const curFraction = validCircles[i].info.fraction;
  603. const curFractionString = curFraction.labels[0].name;
  604. let curFractionSign = i !== '0' ? '+' : '';
  605. if (curFraction.labels[1].name === '-') {
  606. curFractionSign = '-';
  607. font.fill = colors.red;
  608. }
  609. const fractionText = game.add.text(
  610. x0 + i * offsetX + offsetX / 2,
  611. curFractionString === '1' ? y0 + 40 : y0,
  612. curFractionString,
  613. font
  614. );
  615. fractionText.lineHeight = 70;
  616. renderList.push(
  617. game.add.text(x0 + i * offsetX, y0 + 35, curFractionSign, font)
  618. );
  619. renderList.push(fractionText);
  620. renderList.push(
  621. game.add.text(
  622. x0 + offsetX / 2 + i * offsetX,
  623. y0,
  624. curFractionString === '1' ? '' : '_',
  625. font
  626. )
  627. );
  628. nominators.push(curFraction.nominator);
  629. denominators.push(curFraction.denominator);
  630. }
  631. // Setup for fraction operation with least common multiple
  632. font.fill = colors.black;
  633. const updatedNominators = [];
  634. const mmc = game.math.mmcArray(denominators);
  635. let resultNominator = 0;
  636. for (let i in nominators) {
  637. const temp = nominators[i] * (mmc / denominators[i]);
  638. updatedNominators.push(temp);
  639. resultNominator += temp;
  640. }
  641. const resultNominatorUnsigned =
  642. resultNominator < 0 ? -resultNominator : resultNominator;
  643. const resultAux = resultNominator / mmc;
  644. const result =
  645. ('' + resultAux).length > 4 ? resultAux.toFixed(2) : resultAux;
  646. // Fraction operation with least common multiple
  647. nextX = x0 + validCircles.length * offsetX + 20;
  648. // Fraction result
  649. renderList.push(game.add.text(nextX, y0 + 35, '=', font));
  650. font.align = 'center';
  651. nextX += offsetX;
  652. if (result < 0) {
  653. nextX -= 30;
  654. renderList.push(game.add.text(nextX, y0 + 35, '-', font));
  655. nextX += 60;
  656. }
  657. const fractionResult = game.add.text(
  658. nextX,
  659. mmc === 1 || resultNominatorUnsigned === 0 ? y0 + 40 : y0,
  660. mmc === 1 || resultNominatorUnsigned === 0
  661. ? resultNominatorUnsigned
  662. : resultNominatorUnsigned + '\n' + mmc,
  663. font
  664. );
  665. fractionResult.lineHeight = 70;
  666. renderList.push(fractionResult);
  667. const fractionLine = game.add.geom.line(
  668. nextX,
  669. y0 + 15,
  670. nextX + 60,
  671. y0 + 15,
  672. 4,
  673. colors.black,
  674. mmc === 1 || resultNominatorUnsigned === 0 ? 0 : 1
  675. );
  676. fractionLine.anchor(0.5, 0);
  677. renderList.push(fractionLine);
  678. // Fraction result simplified setup
  679. const mdcAux = game.math.mdc(resultNominator, mmc);
  680. const mdc = mdcAux < 0 ? -mdcAux : mdcAux;
  681. if (mdc !== 1 && resultNominatorUnsigned !== 0) {
  682. // alert(mdc + ' ' + resultNominatorUnsigned);
  683. nextX += offsetX;
  684. renderList.push(game.add.text(nextX, y0 + 35, '=', font));
  685. nextX += offsetX;
  686. renderList.push(
  687. game.add.text(nextX - 50, y0 + 35, result > 0 ? '' : '-', font)
  688. );
  689. renderList.push(
  690. game.add.text(nextX, y0, resultNominatorUnsigned / mdc, font)
  691. );
  692. renderList.push(game.add.text(nextX, y0 + 70, mmc / mdc, font));
  693. const fractionLine = game.add.geom.line(
  694. nextX,
  695. y0 + 15,
  696. nextX + 60,
  697. y0 + 15,
  698. 4,
  699. colors.black
  700. );
  701. fractionLine.anchor(0.5, 0);
  702. renderList.push(fractionLine);
  703. }
  704. // Decimal result
  705. let resultWidth = '_'.length * widthOfChar;
  706. const cardWidth = nextX - x0 + resultWidth + padding * 2;
  707. card.width = cardWidth;
  708. const endSignX = (context.canvas.width - cardWidth) / 2 + cardWidth;
  709. // Center Card
  710. moveList(renderList, (context.canvas.width - cardWidth) / 2, 0);
  711. self.fractionOperationUI = renderList;
  712. return endSignX;
  713. },
  714. renderOperationUI_new: () => {
  715. /**
  716. * if game mode A:
  717. * - left: selected balloon position (user selection)
  718. * - right: correct sum of stack of arcs (pre-set)
  719. *
  720. * if game mode B:
  721. * - left: line created from the stack of arcs (user selection)
  722. * - right: baloon position (pre-set)
  723. */
  724. const divisor = gameDifficulty == 3 ? 4 : gameDifficulty;
  725. // const renderFloorFractions = (lastIndex, divisor) => {
  726. // const operator = gameOperation === 'minus' ? '-' : '+';
  727. // const index = lastIndex;
  728. // const blocks = index + 1;
  729. // const valueReal = blocks / divisor;
  730. // const valueFloor = Math.floor(valueReal);
  731. // const valueRest = valueReal - valueFloor;
  732. // let fracNomin = (fracDenomin = fracLine = '');
  733. // // adds sign on the left of the equation
  734. // if (gameOperation === 'minus') {
  735. // fracNomin += ' ';
  736. // fracDenomin += ' ';
  737. // fracLine += operator;
  738. // }
  739. // // 1 _ _
  740. // if (valueFloor) {
  741. // fracNomin += ' ';
  742. // fracDenomin += ' ';
  743. // fracLine += valueFloor;
  744. // }
  745. // // _ + _
  746. // if (valueFloor && valueRest) {
  747. // fracNomin += ' ';
  748. // fracDenomin += ' ';
  749. // fracLine += operator;
  750. // }
  751. // // _ _ 1/5
  752. // if (valueRest) {
  753. // fracNomin += `${valueRest * divisor}`;
  754. // fracDenomin += `${divisor}`;
  755. // fracLine += '-';
  756. // }
  757. // return [fracNomin, fracDenomin, fracLine, valueReal];
  758. // };
  759. const renderStackFractions = (lastIndex) => {
  760. const operator = gameOperation === 'minus' ? '-' : '+';
  761. const index = lastIndex;
  762. const blocks = index + 1;
  763. const nominators = [];
  764. const denominators = [];
  765. const values = [];
  766. let valueReal = 0;
  767. let fracNomin = (fracDenomin = fracLine = '');
  768. for (let i = 0; i < blocks; i++) {
  769. const m = self.circles.list[i].info.fraction.denominator || 1;
  770. const temp = self.circles.list[i].info.fraction.nominator || 0;
  771. const n = gameOperation === 'minus' ? -temp : +temp;
  772. const nm = n / m;
  773. nominators[i] = n + 0;
  774. denominators[i] = m + 0;
  775. values[i] = nm;
  776. valueReal += nm;
  777. }
  778. for (let i = 0; i < blocks; i++) {
  779. const valueReal = values[i];
  780. const valueFloor = Math.floor(valueReal);
  781. const valueRest = valueReal - valueFloor;
  782. if (i > 0 || gameOperation === 'minus') {
  783. fracNomin += ' ';
  784. fracDenomin += ' ';
  785. fracLine += operator;
  786. }
  787. if (valueFloor && !valueRest) {
  788. fracNomin += ' ';
  789. fracDenomin += ' ';
  790. fracLine += valueFloor;
  791. }
  792. if (valueRest) {
  793. fracNomin += `${nominators[i]}`;
  794. fracDenomin += `${denominators[i]}`;
  795. fracLine += '-';
  796. }
  797. }
  798. console.log(fracNomin, fracDenomin, fracLine, valueReal);
  799. return [fracNomin, fracDenomin, fracLine, valueReal];
  800. };
  801. const xyz = () => {
  802. const x0 = +self.road.x;
  803. // console.log(x0);
  804. const xEnd = +self.road.width;
  805. // console.log(xEnd);
  806. const blockWidth = +xEnd / 5;
  807. // console.log(blockWidth);
  808. const selectedX = +self.balloon.x;
  809. // console.log(selectedX);
  810. let count = (selectedX - x0) / blockWidth;
  811. count = Math.floor(count);
  812. // console.log(count);
  813. return count;
  814. };
  815. // Initial setup
  816. const font = textStyles.fraction;
  817. font.fill = colors.black;
  818. const padding = 100;
  819. const offsetX = 100;
  820. const widthOfChar = 35;
  821. const x0 = padding;
  822. const y0 = context.canvas.height / 3;
  823. let nextX = x0;
  824. const cardHeight = 400;
  825. const cardX = x0 - padding;
  826. const cardY = y0;
  827. const renderList = [];
  828. // Render Card
  829. const card = game.add.geom.rect(
  830. cardX,
  831. cardY,
  832. 0,
  833. cardHeight,
  834. colors.blueLight,
  835. 0.5,
  836. colors.blueDark,
  837. 8
  838. );
  839. card.id = 'card';
  840. card.anchor(0, 0.5);
  841. renderList.push(card);
  842. // Fraction setup
  843. // console.clear();
  844. // const [floorNominators, floorDenominators, floorLines, floorValue] =
  845. // renderFloorFractions(self.floor.selectedIndex, divisor);
  846. console.log(self);
  847. const [stackNominators, stackDenominators, stackLines, stackValue] =
  848. renderStackFractions(self.circles.cur - 1);
  849. const renderFloorOperationLine = (x) => {
  850. font.fill = colors.black;
  851. const floorNom = game.add.text(x + offsetX / 2, y0, '', font, 60);
  852. const floorDenom = game.add.text(
  853. x + offsetX / 2,
  854. y0 + 70,
  855. '',
  856. font,
  857. 60
  858. );
  859. const floorLin = game.add.text(
  860. x + offsetX / 2,
  861. y0 + 35,
  862. xyz(),
  863. font,
  864. 60
  865. );
  866. renderList.push(floorNom);
  867. renderList.push(floorDenom);
  868. renderList.push(floorLin);
  869. return;
  870. // font.fill = colors.black;
  871. // const floorNom = game.add.text(
  872. // x + offsetX / 2,
  873. // y0,
  874. // floorNominators,
  875. // font,
  876. // 60
  877. // );
  878. // const floorDenom = game.add.text(
  879. // x + offsetX / 2,
  880. // y0 + 70,
  881. // floorDenominators,
  882. // font,
  883. // 60
  884. // );
  885. // const floorLin = game.add.text(
  886. // x + offsetX / 2,
  887. // y0 + 35,
  888. // floorLines,
  889. // font,
  890. // 60
  891. // );
  892. // renderList.push(floorNom);
  893. // renderList.push(floorDenom);
  894. // renderList.push(floorLin);
  895. };
  896. const renderStackOperationLine = (x) => {
  897. font.fill = colors.black;
  898. const stackNom = game.add.text(
  899. x + offsetX / 2,
  900. y0,
  901. stackNominators,
  902. font,
  903. 60
  904. );
  905. const stackDenom = game.add.text(
  906. x + offsetX / 2,
  907. y0 + 70,
  908. stackDenominators,
  909. font,
  910. 60
  911. );
  912. const stackLin = game.add.text(
  913. x + offsetX / 2,
  914. y0 + 35,
  915. stackLines,
  916. font,
  917. 60
  918. );
  919. renderList.push(stackNom);
  920. renderList.push(stackDenom);
  921. renderList.push(stackLin);
  922. };
  923. // Render LEFT part of the operation
  924. // if (gameMode === 'a') renderFloorOperationLine(x0);
  925. // else renderStackOperationLine(x0);
  926. renderFloorOperationLine(x0);
  927. // let curNominators = gameMode === 'a' ? floorNominators : stackNominators;
  928. let curNominators = '1';
  929. nextX = x0 + (curNominators.length + 2) * widthOfChar;
  930. // Render middle sign - equal by default
  931. font.fill = colors.green;
  932. let comparisonSign = '=';
  933. // Render middle sign - if not equal
  934. // if (floorValue != stackValue) {
  935. // font.fill = colors.red;
  936. // let leftSideIsLarger = floorValue > stackValue;
  937. // if (gameMode === 'b') leftSideIsLarger = !leftSideIsLarger;
  938. // if (gameOperation === 'minus') leftSideIsLarger = !leftSideIsLarger;
  939. // comparisonSign = leftSideIsLarger ? '>' : '<';
  940. // }
  941. renderList.push(game.add.text(nextX, y0 + 35, comparisonSign, font));
  942. // Render RIGHT part of the operationf
  943. // if (gameMode === 'a')
  944. renderStackOperationLine(nextX);
  945. // else renderFloorOperationLine(nextX);
  946. curNominators = gameMode === 'a' ? stackNominators : floorNominators;
  947. const resultWidth = (curNominators.length + 2) * widthOfChar;
  948. const cardWidth = nextX - x0 + resultWidth + padding * 2;
  949. card.width = cardWidth;
  950. const endSignX = (context.canvas.width - cardWidth) / 2 + cardWidth;
  951. // Center Card
  952. moveList(renderList, (context.canvas.width - cardWidth) / 2, 0);
  953. self.fractionOperationUI = renderList;
  954. return endSignX;
  955. },
  956. renderEndUI: () => {
  957. let btnColor = colors.green;
  958. let btnText = game.lang.continue;
  959. if (!self.control.isCorrect) {
  960. btnColor = colors.red;
  961. btnText = game.lang.retry;
  962. }
  963. // continue button
  964. self.ui.continue.button = game.add.geom.rect(
  965. context.canvas.width / 2,
  966. context.canvas.height / 2 + 100,
  967. 450,
  968. 100,
  969. btnColor
  970. );
  971. self.ui.continue.button.anchor(0.5, 0.5);
  972. self.ui.continue.text = game.add.text(
  973. context.canvas.width / 2,
  974. context.canvas.height / 2 + 16 + 100,
  975. btnText,
  976. textStyles.btn
  977. );
  978. },
  979. // UPDATE
  980. animateKidHandler: function () {
  981. let canLowerCircles = undefined;
  982. let curCircle = self.circles.list[self.circles.cur];
  983. let curDirec = curCircle.info.direc;
  984. // Move
  985. self.circles.list.forEach((circle) => {
  986. circle.x += self.animation.walkOffsetX * curDirec;
  987. });
  988. self.kid.x += self.animation.walkOffsetX * curDirec;
  989. self.walkedPath[self.control.curWalkedPath].width +=
  990. self.animation.walkOffsetX * curDirec;
  991. // Update arc
  992. curCircle.info.angleDegree += self.animation.angleOffset * curDirec;
  993. curCircle.angleEnd = game.math.degreeToRad(curCircle.info.angleDegree);
  994. // When finish current circle
  995. if (curCircle.info.direction === 'right') {
  996. canLowerCircles = curCircle.x >= self.control.nextX;
  997. } else if (curCircle.info.direction === 'left') {
  998. canLowerCircles = curCircle.x <= self.control.nextX;
  999. // If just changed from 'right' to 'left' inform to change direction of kid animation
  1000. if (
  1001. self.animation.invertDirection === undefined &&
  1002. self.circles.cur > 0 &&
  1003. self.circles.list[self.circles.cur - 1].info.direction === 'right'
  1004. ) {
  1005. self.animation.invertDirection = true;
  1006. }
  1007. }
  1008. // Change direction of kid animation
  1009. if (self.animation.invertDirection) {
  1010. self.animation.invertDirection = false;
  1011. game.animation.stop(self.kid.animation[0]);
  1012. self.kid.animation = self.animation.list.left;
  1013. self.kid.curFrame = 23;
  1014. game.animation.play('left');
  1015. self.control.curWalkedPath = 1;
  1016. self.utils.renderWalkedPath(
  1017. curCircle.x,
  1018. self.walkedPath[0].y + 8,
  1019. colors.red
  1020. );
  1021. }
  1022. if (canLowerCircles) {
  1023. // Hide current circle
  1024. curCircle.alpha = 0;
  1025. // Lowers kid and other circles
  1026. self.circles.list.forEach((circle) => {
  1027. circle.y += self.circles.diameter;
  1028. });
  1029. self.kid.y += self.circles.diameter;
  1030. self.circles.cur++; // Update index of current circle
  1031. if (self.circles.list[self.circles.cur]) {
  1032. curCircle = self.circles.list[self.circles.cur];
  1033. curDirec = curCircle.info.direc;
  1034. self.control.nextX += curCircle.info.distance * curDirec; // Update next position
  1035. }
  1036. }
  1037. // When finish all circles (final position)
  1038. if (
  1039. self.circles.cur === self.circles.list.length ||
  1040. curCircle.alpha === 0
  1041. ) {
  1042. self.animation.animateKid = false;
  1043. self.control.checkAnswer = true;
  1044. }
  1045. },
  1046. checkAnswerHandler: function () {
  1047. game.timer.stop();
  1048. game.animation.stop(self.kid.animation[0]);
  1049. self.control.isCorrect = game.math.isOverlap(self.basket, self.kid);
  1050. const x = self.utils.renderOperationUI();
  1051. // const x = self.utils.renderOperationUI_new();
  1052. if (self.control.isCorrect) {
  1053. completedLevels++;
  1054. self.kid.curFrame = self.kid.curFrame < 12 ? 24 : 25;
  1055. if (audioStatus) game.audio.okSound.play();
  1056. game.add
  1057. .image(x + 50, context.canvas.height / 3, 'answer_correct')
  1058. .anchor(0.5, 0.5);
  1059. if (isDebugMode) console.log('Completed Levels: ' + completedLevels);
  1060. } else {
  1061. if (audioStatus) game.audio.errorSound.play();
  1062. game.add
  1063. .image(x, context.canvas.height / 3, 'answer_wrong')
  1064. .anchor(0.5, 0.5);
  1065. }
  1066. self.fetch.postScore();
  1067. self.control.checkAnswer = false;
  1068. self.animation.counter = 0;
  1069. self.animation.animateBalloon = true;
  1070. },
  1071. animateBalloonHandler: function () {
  1072. self.animation.counter++;
  1073. self.balloon.y -= 2;
  1074. self.basket.y -= 2;
  1075. if (self.control.isCorrect) self.kid.y -= 2;
  1076. if (self.animation.counter === 100) {
  1077. self.utils.renderEndUI();
  1078. self.control.showEndInfo = true;
  1079. if (self.control.isCorrect) canGoToNextMapPosition = true;
  1080. else canGoToNextMapPosition = false;
  1081. }
  1082. },
  1083. endLevel: function () {
  1084. game.state.start('map');
  1085. },
  1086. // INFORMATION
  1087. /**
  1088. * Show correct answer
  1089. */
  1090. showAnswer: function () {
  1091. if (!self.control.hasClicked) {
  1092. // On gameMode (a)
  1093. if (gameMode === 'a') {
  1094. self.ui.help.x = self.control.correctX - 20;
  1095. self.ui.help.y = self.road.defaultY;
  1096. // On gameMode (b)
  1097. } else {
  1098. self.ui.help.x = self.circles.list[self.control.correctIndex - 1].x;
  1099. self.ui.help.y = self.circles.list[self.control.correctIndex - 1].y; // - self.circles.diameter / 2;
  1100. }
  1101. self.ui.help.alpha = 1;
  1102. }
  1103. },
  1104. // HANDLERS
  1105. /**
  1106. * (in gameMode 'b') Function called when player clicked over a valid circle
  1107. *
  1108. * @param {number|object} cur clicked circle
  1109. */
  1110. clickCircleHandler: function (cur) {
  1111. if (!self.control.hasClicked) {
  1112. // On gameMode (a)
  1113. if (gameMode === 'a') {
  1114. self.balloon.x = cur;
  1115. self.basket.x = cur;
  1116. }
  1117. // On gameMode (b)
  1118. if (gameMode === 'b') {
  1119. document.body.style.cursor = 'auto';
  1120. for (let i in self.circles.list) {
  1121. if (i <= cur.index) {
  1122. self.circles.list[i].alpha = 1; // Keep selected circle
  1123. self.control.selectedIndex = cur.index;
  1124. } else {
  1125. self.circles.list[i].alpha = 0; // Hide unselected circle
  1126. self.kid.y += self.circles.diameter; // Lower kid to selected circle
  1127. }
  1128. }
  1129. }
  1130. if (audioStatus) game.audio.popSound.play();
  1131. // Hide fractions
  1132. if (showFractions) {
  1133. self.circles.list.forEach((circle) => {
  1134. circle.info.fraction.labels.forEach((labelPart) => {
  1135. if (labelPart) labelPart.alpha = 0;
  1136. });
  1137. });
  1138. }
  1139. // Hide solution pointer
  1140. if (self.ui.help != undefined) self.ui.help.alpha = 0;
  1141. self.ui.message[0].alpha = 0;
  1142. navigation.disableIcon(navigation.showAnswerIcon);
  1143. self.balloon.alpha = 1;
  1144. self.basket.alpha = 1;
  1145. self.walkedPath[self.control.curWalkedPath].alpha = 1;
  1146. self.control.hasClicked = true;
  1147. self.animation.animateKid = true;
  1148. game.animation.play(self.kid.animation[0]);
  1149. }
  1150. },
  1151. /**
  1152. * (in gameMode 'b') Function called when cursor is over a valid circle
  1153. *
  1154. * @param {object} cur circle the cursor is over
  1155. */
  1156. overCircleHandler: function (cur) {
  1157. if (!self.control.hasClicked) {
  1158. document.body.style.cursor = 'pointer';
  1159. for (let i in self.circles.list) {
  1160. const alpha = i <= cur.index ? 1 : 0.4;
  1161. self.circles.list[i].alpha = alpha;
  1162. if (showFractions) {
  1163. self.circles.list[i].info.fraction.labels.forEach((lbl) => {
  1164. if (lbl) lbl.alpha = alpha;
  1165. });
  1166. }
  1167. }
  1168. }
  1169. },
  1170. /**
  1171. * (in gameMode 'b') Function called when cursor leaves a valid circle
  1172. */
  1173. outCircleHandler: function () {
  1174. if (!self.control.hasClicked) {
  1175. document.body.style.cursor = 'auto';
  1176. const alpha = 1;
  1177. self.circles.list.forEach((circle) => {
  1178. circle.alpha = alpha;
  1179. if (showFractions) {
  1180. circle.info.fraction.labels.forEach((lbl) => {
  1181. if (lbl) lbl.alpha = alpha;
  1182. });
  1183. }
  1184. });
  1185. }
  1186. },
  1187. /** TODO */
  1188. isOverBlock: function (x, blockX, blockWidth) {
  1189. if (
  1190. (gameOperation === 'plus' && x >= blockX && x < blockX + blockWidth) ||
  1191. (gameOperation === 'minus' && x <= blockX && x > blockX + blockWidth)
  1192. )
  1193. return true;
  1194. return false;
  1195. },
  1196. /** TODO */
  1197. isOverRoad: function (x, y, roadX, roadWidth) {
  1198. if (y > 150) {
  1199. if (
  1200. (gameOperation === 'plus' && x >= roadX && x < roadX + roadWidth) ||
  1201. (gameOperation === 'minus' &&
  1202. x <= roadX &&
  1203. x > roadX + roadWidth * self.control.directionModifier)
  1204. )
  1205. return true;
  1206. }
  1207. return false;
  1208. },
  1209. /** TODO */
  1210. fillCurrentBlock: function (x, blockX, block) {
  1211. block.fillColor =
  1212. (gameOperation === 'plus' && x > blockX) ||
  1213. (gameOperation === 'minus' && x < blockX)
  1214. ? colors.red
  1215. : 'transparent';
  1216. },
  1217. },
  1218. events: {
  1219. /**
  1220. * Called by mouse click event
  1221. *
  1222. * @param {object} mouseEvent contains the mouse click coordinates
  1223. */
  1224. onInputDown: function (mouseEvent) {
  1225. const x = game.math.getMouse(mouseEvent).x;
  1226. const y = game.math.getMouse(mouseEvent).y;
  1227. // GAME MODE A : click road
  1228. if (gameMode === 'a') {
  1229. const isValidX = self.utils.isOverRoad(
  1230. x,
  1231. y,
  1232. self.road.x,
  1233. self.road.width
  1234. );
  1235. if (isValidX) {
  1236. self.utils.clickCircleHandler(
  1237. self.blocks.cur.x + self.blocks.cur.width
  1238. );
  1239. }
  1240. }
  1241. // GAME MODE B : click circle
  1242. if (gameMode === 'b') {
  1243. self.circles.list.forEach((circle) => {
  1244. const isValid =
  1245. game.math.distanceToPointer(
  1246. x,
  1247. circle.xWithAnchor,
  1248. y,
  1249. circle.yWithAnchor
  1250. ) <=
  1251. (circle.diameter / 2) * circle.scale;
  1252. if (isValid) self.utils.clickCircleHandler(circle);
  1253. });
  1254. }
  1255. // Continue button
  1256. if (self.control.showEndInfo) {
  1257. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  1258. if (audioStatus) game.audio.popSound.play();
  1259. self.utils.endLevel();
  1260. }
  1261. }
  1262. navigation.onInputDown(x, y);
  1263. game.render.all();
  1264. },
  1265. /**
  1266. * Called by mouse move event
  1267. *
  1268. * @param {object} mouseEvent contains the mouse move coordinates
  1269. */
  1270. onInputOver: function (mouseEvent) {
  1271. const x = game.math.getMouse(mouseEvent).x;
  1272. const y = game.math.getMouse(mouseEvent).y;
  1273. let isOverCircle = false;
  1274. if (gameMode === 'a' && !self.control.hasClicked) {
  1275. const isValidX = self.utils.isOverRoad(
  1276. x,
  1277. y,
  1278. self.road.x,
  1279. self.road.width
  1280. );
  1281. if (isValidX) {
  1282. // GAME MODE A : balloon follow mouse
  1283. self.blocks.cur = self.blocks.list[0];
  1284. self.blocks.list.forEach((cur) => {
  1285. self.utils.fillCurrentBlock(x, cur.x, cur);
  1286. if (self.utils.isOverBlock(x, cur.x, cur.width, cur))
  1287. self.blocks.cur = cur;
  1288. });
  1289. const newX = self.blocks.cur.x + self.blocks.cur.width;
  1290. self.balloon.x = newX;
  1291. self.basket.x = newX;
  1292. document.body.style.cursor = 'auto';
  1293. }
  1294. }
  1295. // GAME MODE B : hover circle
  1296. if (gameMode === 'b' && !self.control.hasClicked) {
  1297. self.circles.list.forEach((circle) => {
  1298. const isValid =
  1299. game.math.distanceToPointer(
  1300. x,
  1301. circle.xWithAnchor,
  1302. y,
  1303. circle.yWithAnchor
  1304. ) <=
  1305. (circle.diameter / 2) * circle.scale;
  1306. if (isValid) {
  1307. self.utils.overCircleHandler(circle);
  1308. isOverCircle = true;
  1309. }
  1310. });
  1311. if (!isOverCircle) self.utils.outCircleHandler();
  1312. }
  1313. // Continue button
  1314. if (self.control.showEndInfo) {
  1315. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  1316. // If pointer is over icon
  1317. document.body.style.cursor = 'pointer';
  1318. self.ui.continue.button.scale =
  1319. self.ui.continue.button.initialScale * 1.1;
  1320. self.ui.continue.text.style = textStyles.btnLg;
  1321. } else {
  1322. // If pointer is not over icon
  1323. document.body.style.cursor = 'auto';
  1324. self.ui.continue.button.scale =
  1325. self.ui.continue.button.initialScale * 1;
  1326. self.ui.continue.text.style = textStyles.btn;
  1327. }
  1328. }
  1329. navigation.onInputOver(x, y);
  1330. game.render.all();
  1331. },
  1332. },
  1333. fetch: {
  1334. /**
  1335. * Saves players data after level ends - to be sent to database <br>
  1336. *
  1337. * Attention: the 'line_' prefix data table must be compatible to data table fields (MySQL server)
  1338. *
  1339. * @see /php/squareOne.js
  1340. */
  1341. postScore: function () {
  1342. // Creates string that is going to be sent to db
  1343. const data =
  1344. '&line_game=' +
  1345. gameShape +
  1346. '&line_mode=' +
  1347. gameMode +
  1348. '&line_oper=' +
  1349. gameOperation +
  1350. '&line_leve=' +
  1351. gameDifficulty +
  1352. '&line_posi=' +
  1353. curMapPosition +
  1354. '&line_resu=' +
  1355. self.control.isCorrect +
  1356. '&line_time=' +
  1357. game.timer.elapsed +
  1358. '&line_deta=' +
  1359. 'numCircles:' +
  1360. self.circles.list.length +
  1361. ', valCircles: ' +
  1362. self.control.divisorsList +
  1363. ' balloonX: ' +
  1364. self.basket.x +
  1365. ', selIndex: ' +
  1366. self.control.selectedIndex;
  1367. // FOR MOODLE
  1368. sendToDatabase(data);
  1369. },
  1370. },
  1371. };