circleOne.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /**
  2. *
  3. * LInE - Free Education, Private Data
  4. *
  5. * iFractions GAME STATE
  6. *
  7. * LEVELS - CIRCLE I & II: balloon level
  8. *
  9. * Name of game state : 'circleOne'
  10. * Shape : circle
  11. * Character : kid/balloon
  12. * Theme : flying a balloon
  13. * Concept : 'How much the kid has to walk to get to the balloon?'
  14. * Represent fractions as : circles
  15. *
  16. * # of different difficulties for each level : 5
  17. *
  18. * Levels can be : 'A' or 'B' (in variable 'levelType')
  19. *
  20. * A : Player can place balloon position
  21. * Place balloon in position (so the kid can get to it)
  22. * B : Player can select # of circles
  23. * Selects number of circles (that represent distance kid needs to walk to get to the balloon)
  24. *
  25. * Sublevels can be : 'Plus', 'Minus' or 'Mixed' (in variable 'sublevelType')
  26. *
  27. * Plus : addition of fractions
  28. * Represented by : kid going to the right (floor positions 0..5)
  29. * Minus : subtraction of fractions
  30. * Represented by: kid going to the left (floor positions 5..0)
  31. * Mixed : Mix addition and subtraction of fractions in same
  32. * Represented by: kid going to the left (floor positions 0..5)
  33. *
  34. * @namespace
  35. */
  36. const circleOne = {
  37. /**
  38. * Main code
  39. */
  40. create: function () {
  41. // CONTROL VARIABLES
  42. this.availableAnimations = [];
  43. this.changeAnimationFrames = undefined;
  44. this.checkAnswer = false; // Check kid inside ballon's basket
  45. this.animate = false; // Start move animation
  46. this.animateEnding = false; // Start ballon fly animation
  47. this.hasClicked = false; // Air ballon positioned
  48. this.result = false; // Game is correct
  49. this.count = 0;
  50. this.divisorsList = ''; // Used in postScore()
  51. let hasBaseDifficulty = false; // Will validate that level isnt too easy (has at least one '1/difficulty' fraction)
  52. const startX = (sublevelType == 'Minus') ? 66 + 5 * 156 : 66; // Initial 'x' coordinate for the kid and the baloon
  53. this.correctX = startX; // Ending position, accumulative
  54. // BACKGROUND
  55. // Add background image
  56. game.add.image(0, 0, 'bgimage');
  57. // Add clouds
  58. game.add.image(300, 100, 'cloud');
  59. game.add.image(660, 80, 'cloud');
  60. game.add.image(110, 85, 'cloud', 0.8);
  61. // Add floor of grass
  62. for (let i = 0; i < 9; i++) { game.add.image(i * 100, 501, 'floor'); }
  63. // Road
  64. this.road = game.add.image(47, 515, 'road', 1.01, 0.94);
  65. // Road points
  66. const distanceBetweenPoints = 156; // Distance between road points
  67. for (let i = 0; i <= 5; i++) {
  68. game.add.image(66 + i * distanceBetweenPoints, 526, 'place_off', 0.3).anchor(0.5, 0.5);
  69. game.add.text(66 + i * distanceBetweenPoints, 560, i, textStyles.h2_blue);
  70. }
  71. this.trace = game.add.graphic.rect(startX - 1, 526, 1, 1, undefined, 1);
  72. this.trace.alpha = 0;
  73. // Calls function that loads navigation icons
  74. navigationIcons.func_addIcons(
  75. true, true, true, // Left buttons
  76. true, false, // Right buttons
  77. 'customMenu', this.func_viewHelp
  78. );
  79. // CIRCLES AND FRACTIONS
  80. this.circles = {
  81. all: [], // Circles objects of current level
  82. label: [], // Fractions labels
  83. diameter: 60, // (Fixed) diameter for circles
  84. cur: 0, // Current circle index
  85. direction: [], // Circle direction : 'Right' (plus), 'Left' (minus)
  86. distance: [], // Fraction of distance between circles (used in walking animation)
  87. angle: [], // Angle in degrees : 90 / 180 / 270 / 360
  88. lineColor: [], // Circle line colors (also used for tracing on floor)
  89. direc: [], // Can be : 1 or -1 : will be multiplied to values to easily change object direction when needed
  90. };
  91. this.balloonPlace = defaultWidth / 2; // Balloon place
  92. // Number of circles
  93. const max = (sublevelType == 'Mixed' || levelType == 'B') ? 6 : mapPosition + 1;
  94. const min = (sublevelType == 'Mixed' && mapPosition < 2) ? 2 : mapPosition; // Mixed level has at least 2 fractions
  95. const total = game.math.randomInRange(min, max); // Total number of circles
  96. // LevelType 'B' exclusive variables
  97. this.fractionIndex = -1; // Index of clicked circle (game B)
  98. this.numberOfPlusFractions = game.math.randomInRange(1, total - 1);
  99. // CIRCLES
  100. const levelDirection = (sublevelType == 'Minus') ? -1 : 1;
  101. const x = startX + 65 * levelDirection;
  102. for (let i = 0; i < total; i++) {
  103. const divisor = game.math.randomInRange(1, gameDifficulty); // Set fraction 'divisor' (depends on difficulty)
  104. if (divisor == gameDifficulty) hasBaseDifficulty = true; // True if after for ends has at least 1 '1/difficulty' fraction
  105. this.divisorsList += divisor + ','; // Add this divisor to the list of divisors (for postScore())
  106. // Set each circle direction
  107. let direction;
  108. switch (sublevelType) {
  109. case 'Plus': direction = 'Right'; break;
  110. case 'Minus': direction = 'Left'; break;
  111. case 'Mixed':
  112. if (i < this.numberOfPlusFractions) direction = 'Right';
  113. else direction = 'Left';
  114. break;
  115. }
  116. this.circles.direction[i] = direction;
  117. // Set each circle color
  118. let lineColor, anticlockwise;
  119. if (direction == 'Right') {
  120. lineColor = colors.darkBlue;
  121. this.circles.direc[i] = 1;
  122. anticlockwise = true;
  123. } else {
  124. lineColor = colors.red;
  125. this.circles.direc[i] = -1;
  126. anticlockwise = false;
  127. }
  128. this.circles.lineColor[i] = lineColor;
  129. // Draw circles
  130. let circle, label = [];
  131. if (divisor == 1) {
  132. circle = game.add.graphic.circle(startX, 490 - i * this.circles.diameter, this.circles.diameter,
  133. lineColor, 2, colors.white, 1);
  134. circle.anticlockwise = anticlockwise;
  135. this.circles.angle.push(360);
  136. if (fractionLabel) {
  137. label[0] = game.add.text(x, 490 - i * this.circles.diameter, divisor, textStyles.h2_blue);
  138. this.circles.label.push(label);
  139. }
  140. }
  141. else {
  142. let degree = 360 / divisor;
  143. if (direction == 'Right') degree = 360 - degree; // Anticlockwise equivalent
  144. circle = game.add.graphic.arc(startX, 490 - i * this.circles.diameter, this.circles.diameter,
  145. 0, game.math.degreeToRad(degree), anticlockwise,
  146. lineColor, 2, colors.white, 1);
  147. this.circles.angle.push(degree);
  148. if (fractionLabel) {
  149. label[0] = game.add.text(x, 480 - i * this.circles.diameter + 32, divisor, textStyles.h4_blue);
  150. label[1] = game.add.text(x, 488 - i * this.circles.diameter, '1', textStyles.h4_blue);
  151. label[2] = game.add.text(x, 488 - i * this.circles.diameter, '___', textStyles.h4_blue);
  152. this.circles.label.push(label);
  153. }
  154. }
  155. circle.rotate = 90;
  156. // If game is type B (select fractions)
  157. if (levelType == 'B') {
  158. circle.alpha = 0.5;
  159. circle.index = i;
  160. }
  161. this.circles.distance.push(Math.floor(distanceBetweenPoints / divisor));
  162. this.circles.all.push(circle);
  163. this.correctX += Math.floor(distanceBetweenPoints / divisor) * this.circles.direc[i];
  164. }
  165. // Calculate next circle
  166. this.nextX = startX + this.circles.distance[0] * this.circles.direc[0];
  167. // Check if need to restart
  168. this.restart = false;
  169. // If top circle position is out of bounds (when on the ground) or game doesnt have base difficulty, restart
  170. if (this.correctX < 66 || this.correctX > 66 + 3 * 260 || !hasBaseDifficulty) {
  171. this.restart = true;
  172. }
  173. // If game is type B, selectiong a random balloon place
  174. if (levelType == 'B') {
  175. this.balloonPlace = startX;
  176. this.endIndex = game.math.randomInRange(this.numberOfPlusFractions, this.circles.all.length);
  177. for (let i = 0; i < this.endIndex; i++) {
  178. this.balloonPlace += this.circles.distance[i] * this.circles.direc[i];
  179. }
  180. // If balloon position is out of bounds, restart
  181. if (this.balloonPlace < 66 || this.balloonPlace > 66 + 5 * distanceBetweenPoints) {
  182. this.restart = true;
  183. }
  184. }
  185. // KID
  186. this.availableAnimations['Right'] = ['Right', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 4];
  187. this.availableAnimations['Left'] = ['Left', [23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12], 4];
  188. this.kid = game.add.sprite(startX, 495 - this.circles.all.length * this.circles.diameter, 'kid_walk', 0, 0.8);
  189. this.kid.anchor(0.5, 0.8);
  190. if (sublevelType == 'Minus') {
  191. this.kid.animation = this.availableAnimations['Left'];
  192. this.kid.curFrame = 23;
  193. } else {
  194. this.kid.animation = this.availableAnimations['Right'];
  195. }
  196. // BALLOON
  197. this.balloon = game.add.image(this.balloonPlace, 350, 'balloon', 1, 0.5);
  198. this.balloon.alpha = 0.5;
  199. this.balloon.anchor(0.5, 0.5);
  200. this.basket = game.add.image(this.balloonPlace, 472, 'balloon_basket');
  201. this.basket.anchor(0.5, 0.5);
  202. // Help pointer
  203. this.help = game.add.image(0, 0, 'help_pointer', 0.5);
  204. this.help.anchor(0.5, 0);
  205. this.help.alpha = 0;
  206. if (!this.restart) {
  207. game.timer.start(); // Set a timer for the current level (used in postScore())
  208. game.event.add('click', this.func_onInputDown);
  209. game.event.add('mousemove', this.func_onInputOver);
  210. }
  211. },
  212. /**
  213. * Game loop
  214. */
  215. update: function () {
  216. self.count++;
  217. // Start animation
  218. if (self.animate) {
  219. let cur = self.circles.cur;
  220. let DIREC = self.circles.direc[cur];
  221. if (self.count % 2 == 0) { // Lowers animation
  222. // Move kid
  223. self.kid.x += 2 * DIREC;
  224. // Move circles
  225. for (let i in self.circles.all) {
  226. self.circles.all[i].x += 2 * DIREC;
  227. }
  228. // Manage line on the floor
  229. self.trace.width += 2 * DIREC;
  230. self.trace.lineColor = self.circles.all[cur].lineColor;
  231. // Change angle of current arc
  232. self.circles.angle[cur] += 4.6 * DIREC;
  233. self.circles.all[cur].angleEnd = game.math.degreeToRad(self.circles.angle[cur]);
  234. // When finish current circle
  235. let lowerCircles;
  236. if (self.circles.direction[cur] == 'Right') {
  237. lowerCircles = self.circles.all[cur].x >= self.nextX;
  238. }
  239. else if (self.circles.direction[cur] == 'Left') {
  240. lowerCircles = self.circles.all[cur].x <= self.nextX;
  241. // If just changed from 'right' to 'left' inform to change direction of kid animation
  242. if (self.changeAnimationFrames == undefined && cur > 0 && self.circles.direction[cur - 1] == 'Right') {
  243. self.changeAnimationFrames = true;
  244. }
  245. }
  246. // Change direction of kid animation
  247. if (self.changeAnimationFrames) {
  248. self.changeAnimationFrames = false;
  249. game.animation.stop(self.kid.animation[0]);
  250. self.kid.animation = self.availableAnimations['Left'];
  251. self.kid.curFrame = 23;
  252. game.animation.play(self.kid.animation[0]);
  253. }
  254. if (lowerCircles) {
  255. self.circles.all[cur].alpha = 0; // Cicle disappear
  256. self.circles.all.forEach(cur => {
  257. cur.y += self.circles.diameter; // Lower circles
  258. });
  259. self.kid.y += self.circles.diameter; // Lower kid
  260. self.circles.cur++; // Update current circle
  261. cur = self.circles.cur;
  262. DIREC = self.circles.direc[cur];
  263. self.nextX += self.circles.distance[cur] * DIREC; // Update next position
  264. }
  265. // When finish all circles (final position)
  266. if (cur == self.circles.all.length || self.circles.all[cur].alpha == 0) {
  267. self.animate = false;
  268. self.checkAnswer = true;
  269. }
  270. }
  271. }
  272. // Check if kid is inside the basket
  273. if (self.checkAnswer) {
  274. game.timer.stop();
  275. game.animation.stop(self.kid.animation[0]);
  276. if (self.func_checkOverlap(self.basket, self.kid)) {
  277. self.result = true; // Answer is correct
  278. self.kid.curFrame = (self.kid.curFrame < 12) ? 24 : 25;
  279. if (audioStatus) game.audio.okSound.play();
  280. game.add.image(defaultWidth / 2, defaultHeight / 2, 'ok').anchor(0.5, 0.5);
  281. completedLevels++;
  282. if (debugMode) console.log('completedLevels = ' + completedLevels);
  283. } else {
  284. self.result = false; // Answer is incorrect
  285. if (audioStatus) game.audio.errorSound.play();
  286. game.add.image(defaultWidth / 2, defaultHeight / 2, 'error').anchor(0.5, 0.5);
  287. }
  288. self.postScore();
  289. self.animateEnding = true;
  290. self.checkAnswer = false;
  291. self.count = 0;
  292. }
  293. // Balloon flying animation
  294. if (self.animateEnding) {
  295. self.balloon.y -= 2;
  296. self.basket.y -= 2;
  297. if (self.result) self.kid.y -= 2;
  298. if (self.count >= 140) {
  299. if (self.result) mapMove = true;
  300. else mapMove = false;
  301. game.state.start('map');
  302. }
  303. }
  304. game.render.all();
  305. },
  306. /* EVENT HANDLER */
  307. /**
  308. * Called by mouse click event
  309. *
  310. * @param {object} mouseEvent contains the mouse click coordinates
  311. */
  312. func_onInputDown: function (mouseEvent) {
  313. const x = mouseEvent.offsetX;
  314. const y = mouseEvent.offsetY;
  315. // LEVEL A : click road
  316. if (levelType == 'A') {
  317. const cur = self.road;
  318. const valid = y > 60 && (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  319. if (valid) self.func_clicked(x);
  320. }
  321. // LEVEL B : click circle
  322. if (levelType == 'B') {
  323. self.circles.all.forEach(cur => {
  324. const valid = game.math.distanceToPointer(x, cur.xWithAnchor, y, cur.yWithAnchor) <= (cur.diameter / 2) * cur.scale;
  325. if (valid) self.func_clicked(cur);
  326. });
  327. }
  328. navigationIcons.func_onInputDown(x, y);
  329. game.render.all();
  330. },
  331. /**
  332. * Called by mouse move event
  333. *
  334. * @param {object} mouseEvent contains the mouse move coordinates
  335. */
  336. func_onInputOver: function (mouseEvent) {
  337. const x = mouseEvent.offsetX;
  338. const y = mouseEvent.offsetY;
  339. let flag = false;
  340. // LEVEL A : balloon follow mouse
  341. if (levelType == 'A' && !self.hasClicked) {
  342. if (game.math.distanceToPointer(x, self.balloon.x, y, self.balloon.y) > 8) {
  343. self.balloon.x = x;
  344. self.basket.x = x;
  345. }
  346. document.body.style.cursor = 'auto';
  347. }
  348. // LEVEL B : hover circle
  349. if (levelType == 'B' && !self.hasClicked) {
  350. self.circles.all.forEach(cur => {
  351. const valid = game.math.distanceToPointer(x, cur.xWithAnchor, y, cur.yWithAnchor) <= (cur.diameter / 2) * cur.scale;
  352. if (valid) {
  353. self.func_overCircle(cur);
  354. flag = true;
  355. }
  356. });
  357. if (!flag) self.func_outCircle();
  358. }
  359. navigationIcons.func_onInputOver(x, y);
  360. game.render.all();
  361. },
  362. /* CALLED BY EVENT HANDLER */
  363. /**
  364. * (in levelType 'B')
  365. *
  366. * Function called when cursor is over a valid circle
  367. *
  368. * @param {object} cur circle the cursor is over
  369. */
  370. func_overCircle: function (cur) {
  371. if (!self.hasClicked) {
  372. document.body.style.cursor = 'pointer';
  373. for (let i in self.circles.all) {
  374. self.circles.all[i].alpha = (i <= cur.index) ? 1 : 0.5;
  375. }
  376. }
  377. },
  378. /**
  379. * (in levelType 'B')
  380. *
  381. * Function called when cursor is out of a valid circle
  382. */
  383. func_outCircle: function () {
  384. if (!self.hasClicked) {
  385. document.body.style.cursor = 'auto';
  386. self.circles.all.forEach(cur => {
  387. cur.alpha = 0.5;
  388. });
  389. }
  390. },
  391. /**
  392. * (in levelType 'B')
  393. *
  394. * Function called when player clicked over a valid circle
  395. *
  396. * @param {number|object} cur clicked circle
  397. */
  398. func_clicked: function (cur) {
  399. if (!self.hasClicked) {
  400. // On levelType A
  401. if (levelType == 'A') {
  402. self.balloon.x = cur;
  403. self.basket.x = cur;
  404. // On levelType B
  405. }
  406. else if (levelType == 'B') {
  407. document.body.style.cursor = 'auto';
  408. for (let i in self.circles.all) {
  409. if (i <= cur.index) {
  410. self.circles.all[i].alpha = 1; // Keep selected circle
  411. self.fractionIndex = cur.index;
  412. } else {
  413. self.circles.all[i].alpha = 0; // Hide unselected circle
  414. self.kid.y += self.circles.diameter; // Lower kid to selected circle
  415. }
  416. }
  417. }
  418. if (audioStatus) game.audio.beepSound.play();
  419. // Hide fractions
  420. if (fractionLabel) {
  421. self.circles.label.forEach(cur => {
  422. cur.forEach(cur => { cur.alpha = 0; });
  423. });
  424. }
  425. // Hide solution pointer
  426. if (self.help != undefined) self.help.alpha = 0;
  427. self.balloon.alpha = 1;
  428. self.trace.alpha = 1;
  429. self.hasClicked = true;
  430. self.animate = true;
  431. game.animation.play(this.kid.animation[0]);
  432. }
  433. },
  434. /* GAME FUNCTIONS */
  435. /**
  436. * Checks if 2 images overlap
  437. *
  438. * @param {object} spriteA image 1
  439. * @param {object} spriteB image 2
  440. * @returns {boolean}
  441. */
  442. func_checkOverlap: function (spriteA, spriteB) {
  443. const xA = spriteA.x;
  444. const xB = spriteB.x;
  445. // Consider it comming from both sides
  446. if (Math.abs(xA - xB) > 14) return false;
  447. else return true;
  448. },
  449. /**
  450. * Display correct answer
  451. */
  452. func_viewHelp: function () {
  453. if (!self.hasClicked) {
  454. // On levelType A
  455. if (levelType == 'A') {
  456. self.help.x = self.correctX;
  457. self.help.y = 490;
  458. // On levelType B
  459. }
  460. else {
  461. self.help.x = self.circles.all[self.endIndex - 1].x;
  462. self.help.y = self.circles.all[self.endIndex - 1].y - self.circles.diameter / 2;
  463. }
  464. self.help.alpha = 0.7;
  465. }
  466. },
  467. /* METADATA FOR GAME */
  468. /**
  469. * Saves players data after level
  470. */
  471. postScore: function () {
  472. // Saves player data to send to the database
  473. const data = '&line_game=' + gameShape
  474. + '&line_mode=' + levelType
  475. + '&line_oper=' + sublevelType
  476. + '&line_leve=' + gameDifficulty
  477. + '&line_posi=' + mapPosition
  478. + '&line_resu=' + self.result
  479. + '&line_time=' + game.timer.elapsed
  480. + '&line_deta='
  481. + 'numCircles:' + self.circles.all.length
  482. + ', valCircles: ' + self.divisorsList
  483. + ' balloonX: ' + self.basket.x
  484. + ', selIndex: ' + self.fractionIndex;
  485. postScore(data);
  486. }
  487. };