circleOne.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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) => {
  726. const divisor = gameDifficulty == 3 ? 4 : gameDifficulty;
  727. const operator = gameOperation === 'minus' ? '-' : '+';
  728. const index = lastIndex;
  729. const blocks = index + 1;
  730. const valueReal = blocks / divisor;
  731. const valueFloor = Math.floor(valueReal);
  732. const valueRest = valueReal - valueFloor;
  733. let fracNomin = (fracDenomin = fracLine = '');
  734. // adds sign on the left of the equation
  735. if (gameOperation === 'minus') {
  736. fracNomin += ' ';
  737. fracDenomin += ' ';
  738. fracLine += operator;
  739. }
  740. // 1 _ _
  741. if (valueFloor) {
  742. fracNomin += ' ';
  743. fracDenomin += ' ';
  744. fracLine += valueFloor;
  745. }
  746. // _ + _
  747. if (valueFloor && valueRest) {
  748. fracNomin += ' ';
  749. fracDenomin += ' ';
  750. fracLine += operator;
  751. }
  752. // _ _ 1/5
  753. if (valueRest) {
  754. fracNomin += `${valueRest * divisor}`;
  755. fracDenomin += `${divisor}`;
  756. fracLine += '-';
  757. }
  758. return [fracNomin, fracDenomin, fracLine, valueReal];
  759. };
  760. const renderStackFractions = (lastIndex) => {
  761. const operator = gameOperation === 'minus' ? '-' : '+';
  762. const index = lastIndex;
  763. const blocks = index + 1;
  764. const nominators = [];
  765. const denominators = [];
  766. const values = [];
  767. let valueReal = 0;
  768. let fracNomin = (fracDenomin = fracLine = '');
  769. for (let i = 0; i < blocks; i++) {
  770. const m = self.circles.list[i].info.fraction.denominator || 1;
  771. const temp = self.circles.list[i].info.fraction.nominator || 0;
  772. const n = gameOperation === 'minus' ? -temp : +temp;
  773. const nm = n / m;
  774. nominators[i] = n + 0;
  775. denominators[i] = m + 0;
  776. values[i] = nm;
  777. valueReal += nm;
  778. }
  779. for (let i = 0; i < blocks; i++) {
  780. const valueReal = values[i];
  781. const valueFloor = Math.floor(valueReal);
  782. const valueRest = valueReal - valueFloor;
  783. if (i > 0 || gameOperation === 'minus') {
  784. fracNomin += ' ';
  785. fracDenomin += ' ';
  786. fracLine += operator;
  787. }
  788. if (valueFloor && !valueRest) {
  789. fracNomin += ' ';
  790. fracDenomin += ' ';
  791. fracLine += valueFloor;
  792. }
  793. if (valueRest) {
  794. fracNomin += `${nominators[i]}`;
  795. fracDenomin += `${denominators[i]}`;
  796. fracLine += '-';
  797. }
  798. }
  799. return [fracNomin, fracDenomin, fracLine, valueReal];
  800. };
  801. // Initial setup
  802. const font = textStyles.fraction;
  803. font.fill = colors.black;
  804. const padding = 100;
  805. const offsetX = 100;
  806. const widthOfChar = 35;
  807. const x0 = padding;
  808. const y0 = context.canvas.height / 3;
  809. let nextX = x0;
  810. const cardHeight = 400;
  811. const cardX = x0 - padding;
  812. const cardY = y0;
  813. const renderList = [];
  814. // Render Card
  815. const card = game.add.geom.rect(
  816. cardX,
  817. cardY,
  818. 0,
  819. cardHeight,
  820. colors.blueLight,
  821. 0.5,
  822. colors.blueDark,
  823. 8
  824. );
  825. card.id = 'card';
  826. card.anchor(0, 0.5);
  827. renderList.push(card);
  828. // Fraction setup
  829. const [floorNominators, floorDenominators, floorLines, floorValue] =
  830. renderFloorFractions(self.blocks.cur.info.index);
  831. const [stackNominators, stackDenominators, stackLines, stackValue] =
  832. renderStackFractions(self.circles.cur - 1);
  833. const renderFloorOperationLine = (x) => {
  834. font.fill = colors.black;
  835. const floorNom = game.add.text(
  836. x + offsetX / 2,
  837. y0,
  838. floorNominators,
  839. font,
  840. 60
  841. );
  842. const floorDenom = game.add.text(
  843. x + offsetX / 2,
  844. y0 + 70,
  845. floorDenominators,
  846. font,
  847. 60
  848. );
  849. const floorLin = game.add.text(
  850. x + offsetX / 2,
  851. y0 + 35,
  852. floorLines,
  853. font,
  854. 60
  855. );
  856. renderList.push(floorNom);
  857. renderList.push(floorDenom);
  858. renderList.push(floorLin);
  859. };
  860. const renderStackOperationLine = (x) => {
  861. font.fill = colors.black;
  862. const stackNom = game.add.text(
  863. x + offsetX / 2,
  864. y0,
  865. stackNominators,
  866. font,
  867. 60
  868. );
  869. const stackDenom = game.add.text(
  870. x + offsetX / 2,
  871. y0 + 70,
  872. stackDenominators,
  873. font,
  874. 60
  875. );
  876. const stackLin = game.add.text(
  877. x + offsetX / 2,
  878. y0 + 35,
  879. stackLines,
  880. font,
  881. 60
  882. );
  883. renderList.push(stackNom);
  884. renderList.push(stackDenom);
  885. renderList.push(stackLin);
  886. };
  887. // Render LEFT part of the operation
  888. if (gameMode === 'a') renderFloorOperationLine(x0);
  889. else renderStackOperationLine(x0);
  890. let curNominators = gameMode === 'a' ? floorNominators : stackNominators;
  891. nextX = x0 + (curNominators.length + 2) * widthOfChar;
  892. // Render middle sign - equal by default
  893. font.fill = colors.green;
  894. let comparisonSign = '=';
  895. // Render middle sign - if not equal
  896. if (floorValue != stackValue) {
  897. font.fill = colors.red;
  898. let leftSideIsLarger = floorValue > stackValue;
  899. if (gameMode === 'b') leftSideIsLarger = !leftSideIsLarger;
  900. if (gameOperation === 'minus') leftSideIsLarger = !leftSideIsLarger;
  901. comparisonSign = leftSideIsLarger ? '>' : '<';
  902. }
  903. renderList.push(game.add.text(nextX, y0 + 35, comparisonSign, font));
  904. // Render RIGHT part of the operationf
  905. if (gameMode === 'a') renderStackOperationLine(nextX);
  906. else renderFloorOperationLine(nextX);
  907. curNominators = gameMode === 'a' ? stackNominators : floorNominators;
  908. const resultWidth = (curNominators.length + 2) * widthOfChar;
  909. const cardWidth = nextX - x0 + resultWidth + padding * 2;
  910. card.width = cardWidth;
  911. const endSignX = (context.canvas.width - cardWidth) / 2 + cardWidth;
  912. // Center Card
  913. moveList(renderList, (context.canvas.width - cardWidth) / 2, 0);
  914. self.fractionOperationUI = renderList;
  915. return endSignX;
  916. },
  917. renderEndUI: () => {
  918. let btnColor = colors.green;
  919. let btnText = game.lang.continue;
  920. if (!self.control.isCorrect) {
  921. btnColor = colors.red;
  922. btnText = game.lang.retry;
  923. }
  924. // continue button
  925. self.ui.continue.button = game.add.geom.rect(
  926. context.canvas.width / 2,
  927. context.canvas.height / 2 + 100,
  928. 450,
  929. 100,
  930. btnColor
  931. );
  932. self.ui.continue.button.anchor(0.5, 0.5);
  933. self.ui.continue.text = game.add.text(
  934. context.canvas.width / 2,
  935. context.canvas.height / 2 + 16 + 100,
  936. btnText,
  937. textStyles.btn
  938. );
  939. },
  940. // UPDATE
  941. animateKidHandler: function () {
  942. let canLowerCircles = undefined;
  943. let curCircle = self.circles.list[self.circles.cur];
  944. let curDirec = curCircle.info.direc;
  945. // Move
  946. self.circles.list.forEach((circle) => {
  947. circle.x += self.animation.walkOffsetX * curDirec;
  948. });
  949. self.kid.x += self.animation.walkOffsetX * curDirec;
  950. self.walkedPath[self.control.curWalkedPath].width +=
  951. self.animation.walkOffsetX * curDirec;
  952. // Update arc
  953. curCircle.info.angleDegree += self.animation.angleOffset * curDirec;
  954. curCircle.angleEnd = game.math.degreeToRad(curCircle.info.angleDegree);
  955. // When finish current circle
  956. if (curCircle.info.direction === 'right') {
  957. canLowerCircles = curCircle.x >= self.control.nextX;
  958. } else if (curCircle.info.direction === 'left') {
  959. canLowerCircles = curCircle.x <= self.control.nextX;
  960. // If just changed from 'right' to 'left' inform to change direction of kid animation
  961. if (
  962. self.animation.invertDirection === undefined &&
  963. self.circles.cur > 0 &&
  964. self.circles.list[self.circles.cur - 1].info.direction === 'right'
  965. ) {
  966. self.animation.invertDirection = true;
  967. }
  968. }
  969. // Change direction of kid animation
  970. if (self.animation.invertDirection) {
  971. self.animation.invertDirection = false;
  972. game.animation.stop(self.kid.animation[0]);
  973. self.kid.animation = self.animation.list.left;
  974. self.kid.curFrame = 23;
  975. game.animation.play('left');
  976. self.control.curWalkedPath = 1;
  977. self.utils.renderWalkedPath(
  978. curCircle.x,
  979. self.walkedPath[0].y + 8,
  980. colors.red
  981. );
  982. }
  983. if (canLowerCircles) {
  984. // Hide current circle
  985. curCircle.alpha = 0;
  986. // Lowers kid and other circles
  987. self.circles.list.forEach((circle) => {
  988. circle.y += self.circles.diameter;
  989. });
  990. self.kid.y += self.circles.diameter;
  991. self.circles.cur++; // Update index of current circle
  992. if (self.circles.list[self.circles.cur]) {
  993. curCircle = self.circles.list[self.circles.cur];
  994. curDirec = curCircle.info.direc;
  995. self.control.nextX += curCircle.info.distance * curDirec; // Update next position
  996. }
  997. }
  998. // When finish all circles (final position)
  999. if (
  1000. self.circles.cur === self.circles.list.length ||
  1001. curCircle.alpha === 0
  1002. ) {
  1003. self.animation.animateKid = false;
  1004. self.control.checkAnswer = true;
  1005. }
  1006. },
  1007. checkAnswerHandler: function () {
  1008. game.timer.stop();
  1009. game.animation.stop(self.kid.animation[0]);
  1010. self.control.isCorrect = game.math.isOverlap(self.basket, self.kid);
  1011. const x =
  1012. gameMode === 'a'
  1013. ? self.utils.renderOperationUI_new()
  1014. : self.utils.renderOperationUI();
  1015. if (self.control.isCorrect) {
  1016. completedLevels++;
  1017. self.kid.curFrame = self.kid.curFrame < 12 ? 24 : 25;
  1018. if (audioStatus) game.audio.okSound.play();
  1019. game.add
  1020. .image(x + 50, context.canvas.height / 3, 'answer_correct')
  1021. .anchor(0.5, 0.5);
  1022. if (isDebugMode) console.log('Completed Levels: ' + completedLevels);
  1023. } else {
  1024. if (audioStatus) game.audio.errorSound.play();
  1025. game.add
  1026. .image(x, context.canvas.height / 3, 'answer_wrong')
  1027. .anchor(0.5, 0.5);
  1028. }
  1029. self.fetch.postScore();
  1030. self.control.checkAnswer = false;
  1031. self.animation.counter = 0;
  1032. self.animation.animateBalloon = true;
  1033. },
  1034. animateBalloonHandler: function () {
  1035. self.animation.counter++;
  1036. self.balloon.y -= 2;
  1037. self.basket.y -= 2;
  1038. if (self.control.isCorrect) self.kid.y -= 2;
  1039. if (self.animation.counter === 100) {
  1040. self.utils.renderEndUI();
  1041. self.control.showEndInfo = true;
  1042. if (self.control.isCorrect) canGoToNextMapPosition = true;
  1043. else canGoToNextMapPosition = false;
  1044. }
  1045. },
  1046. endLevel: function () {
  1047. game.state.start('map');
  1048. },
  1049. // INFORMATION
  1050. /**
  1051. * Show correct answer
  1052. */
  1053. showAnswer: function () {
  1054. if (!self.control.hasClicked) {
  1055. // On gameMode (a)
  1056. if (gameMode === 'a') {
  1057. self.ui.help.x = self.control.correctX - 20;
  1058. self.ui.help.y = self.road.defaultY;
  1059. // On gameMode (b)
  1060. } else {
  1061. self.ui.help.x = self.circles.list[self.control.correctIndex - 1].x;
  1062. self.ui.help.y = self.circles.list[self.control.correctIndex - 1].y; // - self.circles.diameter / 2;
  1063. }
  1064. self.ui.help.alpha = 1;
  1065. }
  1066. },
  1067. // HANDLERS
  1068. /**
  1069. * (in gameMode 'b') Function called when player clicked over a valid circle
  1070. *
  1071. * @param {number|object} cur clicked circle
  1072. */
  1073. clickCircleHandler: function (cur) {
  1074. if (!self.control.hasClicked) {
  1075. // On gameMode (a)
  1076. if (gameMode === 'a') {
  1077. self.balloon.x = cur;
  1078. self.basket.x = cur;
  1079. }
  1080. // On gameMode (b)
  1081. if (gameMode === 'b') {
  1082. document.body.style.cursor = 'auto';
  1083. for (let i in self.circles.list) {
  1084. if (i <= cur.index) {
  1085. self.circles.list[i].alpha = 1; // Keep selected circle
  1086. self.control.selectedIndex = cur.index;
  1087. } else {
  1088. self.circles.list[i].alpha = 0; // Hide unselected circle
  1089. self.kid.y += self.circles.diameter; // Lower kid to selected circle
  1090. }
  1091. }
  1092. }
  1093. if (audioStatus) game.audio.popSound.play();
  1094. // Hide fractions
  1095. if (showFractions) {
  1096. self.circles.list.forEach((circle) => {
  1097. circle.info.fraction.labels.forEach((labelPart) => {
  1098. if (labelPart) labelPart.alpha = 0;
  1099. });
  1100. });
  1101. }
  1102. // Hide solution pointer
  1103. if (self.ui.help != undefined) self.ui.help.alpha = 0;
  1104. self.ui.message[0].alpha = 0;
  1105. navigation.disableIcon(navigation.showAnswerIcon);
  1106. self.balloon.alpha = 1;
  1107. self.basket.alpha = 1;
  1108. self.walkedPath[self.control.curWalkedPath].alpha = 1;
  1109. self.control.hasClicked = true;
  1110. self.animation.animateKid = true;
  1111. game.animation.play(self.kid.animation[0]);
  1112. }
  1113. },
  1114. /**
  1115. * (in gameMode 'b') Function called when cursor is over a valid circle
  1116. *
  1117. * @param {object} cur circle the cursor is over
  1118. */
  1119. overCircleHandler: function (cur) {
  1120. if (!self.control.hasClicked) {
  1121. document.body.style.cursor = 'pointer';
  1122. for (let i in self.circles.list) {
  1123. const alpha = i <= cur.index ? 1 : 0.4;
  1124. self.circles.list[i].alpha = alpha;
  1125. if (showFractions) {
  1126. self.circles.list[i].info.fraction.labels.forEach((lbl) => {
  1127. if (lbl) lbl.alpha = alpha;
  1128. });
  1129. }
  1130. }
  1131. }
  1132. },
  1133. /**
  1134. * (in gameMode 'b') Function called when cursor leaves a valid circle
  1135. */
  1136. outCircleHandler: function () {
  1137. if (!self.control.hasClicked) {
  1138. document.body.style.cursor = 'auto';
  1139. const alpha = 1;
  1140. self.circles.list.forEach((circle) => {
  1141. circle.alpha = alpha;
  1142. if (showFractions) {
  1143. circle.info.fraction.labels.forEach((lbl) => {
  1144. if (lbl) lbl.alpha = alpha;
  1145. });
  1146. }
  1147. });
  1148. }
  1149. },
  1150. /** TODO */
  1151. isOverBlock: function (x, blockX, blockWidth) {
  1152. if (
  1153. ((gameOperation === 'plus' || gameOperation === 'mixed') &&
  1154. x >= blockX &&
  1155. x < blockX + blockWidth) ||
  1156. (gameOperation === 'minus' && x <= blockX && x > blockX + blockWidth)
  1157. )
  1158. return true;
  1159. return false;
  1160. },
  1161. /** TODO */
  1162. isOverRoad: function (x, y, roadX, roadWidth) {
  1163. if (y > 150) {
  1164. if (
  1165. ((gameOperation === 'plus' || gameOperation === 'mixed') &&
  1166. x >= roadX &&
  1167. x < roadX + roadWidth) ||
  1168. (gameOperation === 'minus' &&
  1169. x <= roadX &&
  1170. x > roadX + roadWidth * self.control.directionModifier)
  1171. )
  1172. return true;
  1173. }
  1174. return false;
  1175. },
  1176. /** TODO */
  1177. fillCurrentBlock: function (x, blockX, block) {
  1178. block.fillColor =
  1179. ((gameOperation === 'plus' || gameOperation === 'mixed') &&
  1180. x > blockX) ||
  1181. (gameOperation === 'minus' && x < blockX)
  1182. ? colors.red
  1183. : 'transparent';
  1184. },
  1185. },
  1186. events: {
  1187. /**
  1188. * Called by mouse click event
  1189. *
  1190. * @param {object} mouseEvent contains the mouse click coordinates
  1191. */
  1192. onInputDown: function (mouseEvent) {
  1193. const x = game.math.getMouse(mouseEvent).x;
  1194. const y = game.math.getMouse(mouseEvent).y;
  1195. // GAME MODE A : click road
  1196. if (gameMode === 'a') {
  1197. const isValidX = self.utils.isOverRoad(
  1198. x,
  1199. y,
  1200. self.road.x,
  1201. self.road.width
  1202. );
  1203. if (isValidX) {
  1204. self.utils.clickCircleHandler(
  1205. self.blocks.cur.x + self.blocks.cur.width
  1206. );
  1207. }
  1208. }
  1209. // GAME MODE B : click circle
  1210. if (gameMode === 'b') {
  1211. self.circles.list.forEach((circle) => {
  1212. const isValid =
  1213. game.math.distanceToPointer(
  1214. x,
  1215. circle.xWithAnchor,
  1216. y,
  1217. circle.yWithAnchor
  1218. ) <=
  1219. (circle.diameter / 2) * circle.scale;
  1220. if (isValid) self.utils.clickCircleHandler(circle);
  1221. });
  1222. }
  1223. // Continue button
  1224. if (self.control.showEndInfo) {
  1225. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  1226. if (audioStatus) game.audio.popSound.play();
  1227. self.utils.endLevel();
  1228. }
  1229. }
  1230. navigation.onInputDown(x, y);
  1231. game.render.all();
  1232. },
  1233. /**
  1234. * Called by mouse move event
  1235. *
  1236. * @param {object} mouseEvent contains the mouse move coordinates
  1237. */
  1238. onInputOver: function (mouseEvent) {
  1239. const x = game.math.getMouse(mouseEvent).x;
  1240. const y = game.math.getMouse(mouseEvent).y;
  1241. let isOverCircle = false;
  1242. if (gameMode === 'a' && !self.control.hasClicked) {
  1243. const isValidX = self.utils.isOverRoad(
  1244. x,
  1245. y,
  1246. self.road.x,
  1247. self.road.width
  1248. );
  1249. console.log(isValidX);
  1250. if (isValidX) {
  1251. // GAME MODE A : balloon follow mouse
  1252. self.blocks.cur = self.blocks.list[0];
  1253. self.blocks.list.forEach((cur) => {
  1254. self.utils.fillCurrentBlock(x, cur.x, cur);
  1255. if (self.utils.isOverBlock(x, cur.x, cur.width, cur))
  1256. self.blocks.cur = cur;
  1257. });
  1258. const newX = self.blocks.cur.x + self.blocks.cur.width;
  1259. self.balloon.x = newX;
  1260. self.basket.x = newX;
  1261. document.body.style.cursor = 'auto';
  1262. }
  1263. }
  1264. // GAME MODE B : hover circle
  1265. if (gameMode === 'b' && !self.control.hasClicked) {
  1266. self.circles.list.forEach((circle) => {
  1267. const isValid =
  1268. game.math.distanceToPointer(
  1269. x,
  1270. circle.xWithAnchor,
  1271. y,
  1272. circle.yWithAnchor
  1273. ) <=
  1274. (circle.diameter / 2) * circle.scale;
  1275. if (isValid) {
  1276. self.utils.overCircleHandler(circle);
  1277. isOverCircle = true;
  1278. }
  1279. });
  1280. if (!isOverCircle) self.utils.outCircleHandler();
  1281. }
  1282. // Continue button
  1283. if (self.control.showEndInfo) {
  1284. if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
  1285. // If pointer is over icon
  1286. document.body.style.cursor = 'pointer';
  1287. self.ui.continue.button.scale =
  1288. self.ui.continue.button.initialScale * 1.1;
  1289. self.ui.continue.text.style = textStyles.btnLg;
  1290. } else {
  1291. // If pointer is not over icon
  1292. document.body.style.cursor = 'auto';
  1293. self.ui.continue.button.scale =
  1294. self.ui.continue.button.initialScale * 1;
  1295. self.ui.continue.text.style = textStyles.btn;
  1296. }
  1297. }
  1298. navigation.onInputOver(x, y);
  1299. game.render.all();
  1300. },
  1301. },
  1302. fetch: {
  1303. /**
  1304. * Saves players data after level ends - to be sent to database <br>
  1305. *
  1306. * Attention: the 'line_' prefix data table must be compatible to data table fields (MySQL server)
  1307. *
  1308. * @see /php/squareOne.js
  1309. */
  1310. postScore: function () {
  1311. // Creates string that is going to be sent to db
  1312. const data =
  1313. '&line_game=' +
  1314. gameShape +
  1315. '&line_mode=' +
  1316. gameMode +
  1317. '&line_oper=' +
  1318. gameOperation +
  1319. '&line_leve=' +
  1320. gameDifficulty +
  1321. '&line_posi=' +
  1322. curMapPosition +
  1323. '&line_resu=' +
  1324. self.control.isCorrect +
  1325. '&line_time=' +
  1326. game.timer.elapsed +
  1327. '&line_deta=' +
  1328. 'numCircles:' +
  1329. self.circles.list.length +
  1330. ', valCircles: ' +
  1331. self.control.divisorsList +
  1332. ' balloonX: ' +
  1333. self.basket.x +
  1334. ', selIndex: ' +
  1335. self.control.selectedIndex;
  1336. // FOR MOODLE
  1337. sendToDatabase(data);
  1338. },
  1339. },
  1340. };