circleOne.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [GAME STATE]
  5. *
  6. * .....circleOne.... = gameType
  7. * ....../....\......
  8. * .....A......B..... = gameMode
  9. * .......\./........
  10. * ........|.........
  11. * ....../.|.\.......
  12. * .Plus.Minus.Mixed. = gameOperation
  13. * ......\.|./.......
  14. * ........|.........
  15. * ....1,2,3,4,5..... = 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. /**
  42. * Main code
  43. */
  44. create: function () {
  45. // CONTROL VARIABLES
  46. this.availableAnimations = [];
  47. this.changeAnimationFrames = undefined;
  48. this.checkAnswer = false; // Check kid inside ballon's basket
  49. this.animate = false; // Start move animation
  50. this.animateEnding = false; // Start ballon fly animation
  51. this.hasClicked = false; // Air ballon positioned
  52. this.result = false; // Game is correct
  53. this.count = 0;
  54. this.divisorsList = ''; // Used in postScore()
  55. let hasBaseDifficulty = false; // Will validate that level isnt too easy (has at least one '1/difficulty' fraction)
  56. const startY = context.canvas.height - 75;
  57. const startX = gameOperation == 'Minus' ? 66 + 5 * 156 : 66; // Initial 'x' coordinate for the kid and the baloon
  58. this.correctX = startX; // Ending position, accumulative
  59. // BACKGROUND
  60. // Add background image
  61. game.add.image(0, 0, 'bgimage', 2.2);
  62. // Add clouds
  63. game.add.image(640, 100, 'cloud');
  64. game.add.image(1280, 80, 'cloud');
  65. game.add.image(300, 85, 'cloud', 0.8);
  66. // Add floor of grass
  67. for (let i = 0; i < context.canvas.width / 100; i++) {
  68. game.add.image(i * 100, context.canvas.height - 100, 'floor');
  69. }
  70. // Road
  71. this.road = game.add.image(47, startY - 11, 'road', 1.01, 0.94);
  72. // Road points
  73. const distanceBetweenPoints = 156; // Distance between road points
  74. for (let i = 0; i <= 5; i++) {
  75. game.add
  76. .image(66 + i * distanceBetweenPoints, startY, 'place_off', 0.3)
  77. .anchor(0.5, 0.5);
  78. game.add.text(
  79. 66 + i * distanceBetweenPoints,
  80. startY + 34,
  81. i,
  82. textStyles.h2_blueDark
  83. );
  84. }
  85. this.trace = game.add.geom.rect(startX - 1, startY, 1, 1, undefined, 1);
  86. this.trace.alpha = 0;
  87. // Calls function that loads navigation icons
  88. // FOR MOODLE
  89. if (moodle) {
  90. navigationIcons.add(
  91. false,
  92. false,
  93. false, // Left buttons
  94. true,
  95. false, // Right buttons
  96. false,
  97. false
  98. );
  99. } else {
  100. navigationIcons.add(
  101. true,
  102. true,
  103. true, // Left buttons
  104. true,
  105. false, // Right buttons
  106. 'customMenu',
  107. this.viewHelp
  108. );
  109. }
  110. // CIRCLES AND FRACTIONS
  111. this.circles = {
  112. all: [], // Circles objects of current level
  113. label: [], // Fractions labels
  114. diameter: 60, // (Fixed) diameter for circles
  115. cur: 0, // Current circle index
  116. direction: [], // Circle direction : 'Right' (plus), 'Left' (minus)
  117. distance: [], // Fraction of distance between circles (used in walking animation)
  118. angle: [], // Angle in degrees : 90 / 180 / 270 / 360
  119. lineColor: [], // Circle line colors (also used for tracing on floor)
  120. direc: [], // Can be : 1 or -1 : will be multiplied to values to easily change object direction when needed
  121. };
  122. this.balloonPlace = context.canvas.width / 2; // Balloon place
  123. // Number of circles
  124. const max =
  125. gameOperation == 'Mixed' || gameMode == 'B' ? 6 : mapPosition + 1;
  126. const min = gameOperation == 'Mixed' && mapPosition < 2 ? 2 : mapPosition; // Mixed level has at least 2 fractions
  127. const total = game.math.randomInRange(min, max); // Total number of circles
  128. // gameMode 'B' exclusive variables
  129. this.fractionIndex = -1; // Index of clicked circle (game B)
  130. this.numberOfPlusFractions = game.math.randomInRange(1, total - 1);
  131. // CIRCLES
  132. const levelDirection = gameOperation == 'Minus' ? -1 : 1;
  133. const x = startX + 65 * levelDirection;
  134. for (let i = 0; i < total; i++) {
  135. const divisor = game.math.randomInRange(1, gameDifficulty); // Set fraction 'divisor' (depends on difficulty)
  136. if (divisor == gameDifficulty) hasBaseDifficulty = true; // True if after for ends has at least 1 '1/difficulty' fraction
  137. this.divisorsList += divisor + ','; // Add this divisor to the list of divisors (for postScore())
  138. // Set each circle direction
  139. let direction;
  140. switch (gameOperation) {
  141. case 'Plus':
  142. direction = 'Right';
  143. break;
  144. case 'Minus':
  145. direction = 'Left';
  146. break;
  147. case 'Mixed':
  148. if (i < this.numberOfPlusFractions) direction = 'Right';
  149. else direction = 'Left';
  150. break;
  151. }
  152. this.circles.direction[i] = direction;
  153. // Set each circle color
  154. let lineColor, anticlockwise;
  155. if (direction == 'Right') {
  156. lineColor = colors.blueDark;
  157. this.circles.direc[i] = 1;
  158. anticlockwise = true;
  159. } else {
  160. lineColor = colors.red;
  161. this.circles.direc[i] = -1;
  162. anticlockwise = false;
  163. }
  164. this.circles.lineColor[i] = lineColor;
  165. // Draw circles
  166. let circle,
  167. label = [];
  168. if (divisor == 1) {
  169. circle = game.add.geom.circle(
  170. startX,
  171. startY - 36 - i * this.circles.diameter,
  172. this.circles.diameter,
  173. lineColor,
  174. 2,
  175. colors.white,
  176. 1
  177. );
  178. circle.anticlockwise = anticlockwise;
  179. this.circles.angle.push(360);
  180. if (fractionLabel) {
  181. label[0] = game.add.text(
  182. x,
  183. startY - 36 - i * this.circles.diameter,
  184. divisor,
  185. textStyles.h2_blueDark
  186. );
  187. this.circles.label.push(label);
  188. }
  189. } else {
  190. let degree = 360 / divisor;
  191. if (direction == 'Right') degree = 360 - degree; // Anticlockwise equivalent
  192. circle = game.add.geom.arc(
  193. startX,
  194. startY - 36 - i * this.circles.diameter,
  195. this.circles.diameter,
  196. 0,
  197. game.math.degreeToRad(degree),
  198. anticlockwise,
  199. lineColor,
  200. 2,
  201. colors.white,
  202. 1
  203. );
  204. this.circles.angle.push(degree);
  205. if (fractionLabel) {
  206. label[0] = game.add.text(
  207. x,
  208. startY - 46 - i * this.circles.diameter + 32,
  209. divisor,
  210. textStyles.h4_blueDark
  211. );
  212. label[1] = game.add.text(
  213. x,
  214. startY - 38 - i * this.circles.diameter,
  215. '1',
  216. textStyles.h4_blueDark
  217. );
  218. label[2] = game.add.text(
  219. x,
  220. startY - 38 - i * this.circles.diameter,
  221. '___',
  222. textStyles.h4_blueDark
  223. );
  224. this.circles.label.push(label);
  225. }
  226. }
  227. circle.rotate = 90;
  228. // If game is type B (select fractions)
  229. if (gameMode == 'B') {
  230. circle.alpha = 0.5;
  231. circle.index = i;
  232. }
  233. this.circles.distance.push(Math.floor(distanceBetweenPoints / divisor));
  234. this.circles.all.push(circle);
  235. this.correctX +=
  236. Math.floor(distanceBetweenPoints / divisor) * this.circles.direc[i];
  237. }
  238. // Calculate next circle
  239. this.nextX = startX + this.circles.distance[0] * this.circles.direc[0];
  240. // Check if need to restart
  241. this.restart = false;
  242. // If top circle position is out of bounds (when on the ground) or game doesnt have base difficulty, restart
  243. if (
  244. this.correctX < 66 ||
  245. this.correctX > 66 + 3 * 260 ||
  246. !hasBaseDifficulty
  247. ) {
  248. this.restart = true;
  249. }
  250. // If game is type B, selectiong a random balloon place
  251. if (gameMode == 'B') {
  252. this.balloonPlace = startX;
  253. this.endIndex = game.math.randomInRange(
  254. this.numberOfPlusFractions,
  255. this.circles.all.length
  256. );
  257. for (let i = 0; i < this.endIndex; i++) {
  258. this.balloonPlace += this.circles.distance[i] * this.circles.direc[i];
  259. }
  260. // If balloon position is out of bounds, restart
  261. if (
  262. this.balloonPlace < 66 ||
  263. this.balloonPlace > 66 + 5 * distanceBetweenPoints
  264. ) {
  265. this.restart = true;
  266. }
  267. }
  268. // KID
  269. this.availableAnimations['Right'] = [
  270. 'Right',
  271. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
  272. 4,
  273. ];
  274. this.availableAnimations['Left'] = [
  275. 'Left',
  276. [23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12],
  277. 4,
  278. ];
  279. this.kid = game.add.sprite(
  280. startX,
  281. startY - 31 - this.circles.all.length * this.circles.diameter,
  282. 'kid_walk',
  283. 0,
  284. 0.8
  285. );
  286. this.kid.anchor(0.5, 0.8);
  287. if (gameOperation == 'Minus') {
  288. this.kid.animation = this.availableAnimations['Left'];
  289. this.kid.curFrame = 23;
  290. } else {
  291. this.kid.animation = this.availableAnimations['Right'];
  292. }
  293. // BALLOON
  294. this.balloon = game.add.image(
  295. this.balloonPlace,
  296. startY - 176,
  297. 'balloon',
  298. 1,
  299. 0.5
  300. );
  301. this.balloon.alpha = 0.5;
  302. this.balloon.anchor(0.5, 0.5);
  303. this.basket = game.add.image(
  304. this.balloonPlace,
  305. startY - 54,
  306. 'balloon_basket'
  307. );
  308. this.basket.anchor(0.5, 0.5);
  309. // Help pointer
  310. this.help = game.add.image(0, 0, 'help_pointer', 0.5);
  311. this.help.anchor(0.5, 0);
  312. this.help.alpha = 0;
  313. if (!this.restart) {
  314. game.timer.start(); // Set a timer for the current level (used in postScore())
  315. game.event.add('click', this.onInputDown);
  316. game.event.add('mousemove', this.onInputOver);
  317. }
  318. },
  319. /**
  320. * Game loop
  321. */
  322. update: function () {
  323. self.count++;
  324. // Start animation
  325. if (self.animate) {
  326. let cur = self.circles.cur;
  327. let direc = self.circles.direc[cur];
  328. if (self.count % 2 == 0) {
  329. // Lowers animation
  330. // Move kid
  331. self.kid.x += 2 * direc;
  332. // Move circles
  333. for (let i in self.circles.all) {
  334. self.circles.all[i].x += 2 * direc;
  335. }
  336. // Manage line on the floor
  337. self.trace.width += 2 * direc;
  338. self.trace.lineColor = self.circles.all[cur].lineColor;
  339. // Change angle of current arc
  340. self.circles.angle[cur] += 4.6 * direc;
  341. self.circles.all[cur].angleEnd = game.math.degreeToRad(
  342. self.circles.angle[cur]
  343. );
  344. // When finish current circle
  345. let lowerCircles;
  346. if (self.circles.direction[cur] == 'Right') {
  347. lowerCircles = self.circles.all[cur].x >= self.nextX;
  348. } else if (self.circles.direction[cur] == 'Left') {
  349. lowerCircles = self.circles.all[cur].x <= self.nextX;
  350. // If just changed from 'right' to 'left' inform to change direction of kid animation
  351. if (
  352. self.changeAnimationFrames == undefined &&
  353. cur > 0 &&
  354. self.circles.direction[cur - 1] == 'Right'
  355. ) {
  356. self.changeAnimationFrames = true;
  357. }
  358. }
  359. // Change direction of kid animation
  360. if (self.changeAnimationFrames) {
  361. self.changeAnimationFrames = false;
  362. game.animation.stop(self.kid.animation[0]);
  363. self.kid.animation = self.availableAnimations['Left'];
  364. self.kid.curFrame = 23;
  365. game.animation.play(self.kid.animation[0]);
  366. }
  367. if (lowerCircles) {
  368. self.circles.all[cur].alpha = 0; // Cicle disappear
  369. self.circles.all.forEach((cur) => {
  370. cur.y += self.circles.diameter; // Lower circles
  371. });
  372. self.kid.y += self.circles.diameter; // Lower kid
  373. self.circles.cur++; // Update current circle
  374. cur = self.circles.cur;
  375. direc = self.circles.direc[cur];
  376. self.nextX += self.circles.distance[cur] * direc; // Update next position
  377. }
  378. // When finish all circles (final position)
  379. if (
  380. cur == self.circles.all.length ||
  381. self.circles.all[cur].alpha == 0
  382. ) {
  383. self.animate = false;
  384. self.checkAnswer = true;
  385. }
  386. }
  387. }
  388. // Check if kid is inside the basket
  389. if (self.checkAnswer) {
  390. game.timer.stop();
  391. game.animation.stop(self.kid.animation[0]);
  392. if (self.checkOverlap(self.basket, self.kid)) {
  393. self.result = true; // Answer is correct
  394. self.kid.curFrame = self.kid.curFrame < 12 ? 24 : 25;
  395. if (audioStatus) game.audio.okSound.play();
  396. game.add
  397. .image(context.canvas.width / 2, context.canvas.height / 2, 'ok')
  398. .anchor(0.5, 0.5);
  399. completedLevels++;
  400. if (debugMode) console.log('Completed Levels: ' + completedLevels);
  401. } else {
  402. self.result = false; // Answer is incorrect
  403. if (audioStatus) game.audio.errorSound.play();
  404. game.add
  405. .image(context.canvas.width / 2, context.canvas.height / 2, 'error')
  406. .anchor(0.5, 0.5);
  407. }
  408. self.postScore();
  409. self.animateEnding = true;
  410. self.checkAnswer = false;
  411. self.count = 0;
  412. }
  413. // Balloon flying animation
  414. if (self.animateEnding) {
  415. self.balloon.y -= 2;
  416. self.basket.y -= 2;
  417. if (self.result) self.kid.y -= 2;
  418. if (self.count >= 140) {
  419. if (self.result) mapMove = true;
  420. else mapMove = false;
  421. game.state.start('map');
  422. }
  423. }
  424. game.render.all();
  425. },
  426. /**
  427. * (in gameMode 'B') Function called when cursor is over a valid circle
  428. *
  429. * @param {object} cur circle the cursor is over
  430. */
  431. overCircle: function (cur) {
  432. if (!self.hasClicked) {
  433. document.body.style.cursor = 'pointer';
  434. for (let i in self.circles.all) {
  435. self.circles.all[i].alpha = i <= cur.index ? 1 : 0.5;
  436. }
  437. }
  438. },
  439. /**
  440. * (in gameMode 'B') Function called when cursor is out of a valid circle
  441. */
  442. outCircle: function () {
  443. if (!self.hasClicked) {
  444. document.body.style.cursor = 'auto';
  445. self.circles.all.forEach((cur) => {
  446. cur.alpha = 0.5;
  447. });
  448. }
  449. },
  450. /**
  451. * (in gameMode 'B') Function called when player clicked over a valid circle
  452. *
  453. * @param {number|object} cur clicked circle
  454. */
  455. clicked: function (cur) {
  456. if (!self.hasClicked) {
  457. // On gameMode A
  458. if (gameMode == 'A') {
  459. self.balloon.x = cur;
  460. self.basket.x = cur;
  461. // On gameMode B
  462. } else if (gameMode == 'B') {
  463. document.body.style.cursor = 'auto';
  464. for (let i in self.circles.all) {
  465. if (i <= cur.index) {
  466. self.circles.all[i].alpha = 1; // Keep selected circle
  467. self.fractionIndex = cur.index;
  468. } else {
  469. self.circles.all[i].alpha = 0; // Hide unselected circle
  470. self.kid.y += self.circles.diameter; // Lower kid to selected circle
  471. }
  472. }
  473. }
  474. if (audioStatus) game.audio.popSound.play();
  475. // Hide fractions
  476. if (fractionLabel) {
  477. self.circles.label.forEach((cur) => {
  478. cur.forEach((cur) => {
  479. cur.alpha = 0;
  480. });
  481. });
  482. }
  483. // Hide solution pointer
  484. if (self.help != undefined) self.help.alpha = 0;
  485. self.balloon.alpha = 1;
  486. self.trace.alpha = 1;
  487. self.hasClicked = true;
  488. self.animate = true;
  489. game.animation.play(this.kid.animation[0]);
  490. }
  491. },
  492. /**
  493. * Checks if 2 images overlap
  494. *
  495. * @param {object} spriteA image 1
  496. * @param {object} spriteB image 2
  497. *
  498. * @returns {boolean} true if there is overlap
  499. */
  500. checkOverlap: function (spriteA, spriteB) {
  501. const xA = spriteA.x;
  502. const xB = spriteB.x;
  503. // Consider it comming from both sides
  504. if (Math.abs(xA - xB) > 14) return false;
  505. else return true;
  506. },
  507. /**
  508. * Display correct answer
  509. */
  510. viewHelp: function () {
  511. if (!self.hasClicked) {
  512. // On gameMode A
  513. if (gameMode == 'A') {
  514. self.help.x = self.correctX;
  515. self.help.y = 490;
  516. // On gameMode B
  517. } else {
  518. self.help.x = self.circles.all[self.endIndex - 1].x;
  519. self.help.y =
  520. self.circles.all[self.endIndex - 1].y - self.circles.diameter / 2;
  521. }
  522. self.help.alpha = 0.7;
  523. }
  524. },
  525. /**
  526. * Called by mouse click event
  527. *
  528. * @param {object} mouseEvent contains the mouse click coordinates
  529. */
  530. onInputDown: function (mouseEvent) {
  531. const x = game.math.getMouse(mouseEvent).x;
  532. const y = game.math.getMouse(mouseEvent).y;
  533. // GAME MODE A : click road
  534. if (gameMode == 'A') {
  535. const cur = self.road;
  536. const valid =
  537. y > 60 &&
  538. x >= cur.xWithAnchor &&
  539. x <= cur.xWithAnchor + cur.width * cur.scale;
  540. if (valid) self.clicked(x);
  541. }
  542. // GAME MODE B : click circle
  543. if (gameMode == 'B') {
  544. self.circles.all.forEach((cur) => {
  545. const valid =
  546. game.math.distanceToPointer(x, cur.xWithAnchor, y, cur.yWithAnchor) <=
  547. (cur.diameter / 2) * cur.scale;
  548. if (valid) self.clicked(cur);
  549. });
  550. }
  551. navigationIcons.onInputDown(x, y);
  552. game.render.all();
  553. },
  554. /**
  555. * Called by mouse move event
  556. *
  557. * @param {object} mouseEvent contains the mouse move coordinates
  558. */
  559. onInputOver: function (mouseEvent) {
  560. const x = game.math.getMouse(mouseEvent).x;
  561. const y = game.math.getMouse(mouseEvent).y;
  562. let flag = false;
  563. // GAME MODE A : balloon follow mouse
  564. if (gameMode == 'A' && !self.hasClicked) {
  565. if (
  566. game.math.distanceToPointer(x, self.balloon.x, y, self.balloon.y) > 8
  567. ) {
  568. self.balloon.x = x;
  569. self.basket.x = x;
  570. }
  571. document.body.style.cursor = 'auto';
  572. }
  573. // GAME MODE B : hover circle
  574. if (gameMode == 'B' && !self.hasClicked) {
  575. self.circles.all.forEach((cur) => {
  576. const valid =
  577. game.math.distanceToPointer(x, cur.xWithAnchor, y, cur.yWithAnchor) <=
  578. (cur.diameter / 2) * cur.scale;
  579. if (valid) {
  580. self.overCircle(cur);
  581. flag = true;
  582. }
  583. });
  584. if (!flag) self.outCircle();
  585. }
  586. navigationIcons.onInputOver(x, y);
  587. game.render.all();
  588. },
  589. /**
  590. * Saves players data after level ends - to be sent to database <br>
  591. *
  592. * Attention: the 'line_' prefix data table must be compatible to data table fields (MySQL server)
  593. *
  594. * @see /php/squareOne.js
  595. */
  596. postScore: function () {
  597. // Creates string that is going to be sent to db
  598. const data =
  599. '&line_game=' +
  600. gameShape +
  601. '&line_mode=' +
  602. gameMode +
  603. '&line_oper=' +
  604. gameOperation +
  605. '&line_leve=' +
  606. gameDifficulty +
  607. '&line_posi=' +
  608. mapPosition +
  609. '&line_resu=' +
  610. self.result +
  611. '&line_time=' +
  612. game.timer.elapsed +
  613. '&line_deta=' +
  614. 'numCircles:' +
  615. self.circles.all.length +
  616. ', valCircles: ' +
  617. self.divisorsList +
  618. ' balloonX: ' +
  619. self.basket.x +
  620. ', selIndex: ' +
  621. self.fractionIndex;
  622. // FOR MOODLE
  623. sendToDB(data);
  624. },
  625. };