circleOne.js 44 KB

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