circleOne.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. } else {
  141. let degree = 360 / divisor;
  142. if (direction == 'Right') degree = 360 - degree; // Anticlockwise equivalent
  143. circle = game.add.graphic.arc(startX, 490 - i * this.circles.diameter, this.circles.diameter,
  144. 0, game.math.degreeToRad(degree), anticlockwise,
  145. lineColor, 2, colors.white, 1);
  146. this.circles.angle.push(degree);
  147. if (fractionLabel) {
  148. label[0] = game.add.text(x, 480 - i * this.circles.diameter + 32, divisor, textStyles.h4_blue);
  149. label[1] = game.add.text(x, 488 - i * this.circles.diameter, '1', textStyles.h4_blue);
  150. label[2] = game.add.text(x, 488 - i * this.circles.diameter, '___', textStyles.h4_blue);
  151. this.circles.label.push(label);
  152. }
  153. }
  154. circle.rotate = 90;
  155. // If game is type B (select fractions)
  156. if (levelType == 'B') {
  157. circle.alpha = 0.5;
  158. circle.index = i;
  159. }
  160. this.circles.distance.push(Math.floor(distanceBetweenPoints / divisor));
  161. this.circles.all.push(circle);
  162. this.correctX += Math.floor(distanceBetweenPoints / divisor) * this.circles.direc[i];
  163. }
  164. // Calculate next circle
  165. this.nextX = startX + this.circles.distance[0] * this.circles.direc[0];
  166. // Check if need to restart
  167. this.restart = false;
  168. // If top circle position is out of bounds (when on the ground) or game doesnt have base difficulty, restart
  169. if (this.correctX < 66 || this.correctX > 66 + 3 * 260 || !hasBaseDifficulty) {
  170. this.restart = true;
  171. }
  172. // If game is type B, selectiong a random balloon place
  173. if (levelType == 'B') {
  174. this.balloonPlace = startX;
  175. this.endIndex = game.math.randomInRange(this.numberOfPlusFractions, this.circles.all.length);
  176. for (let i = 0; i < this.endIndex; i++) {
  177. this.balloonPlace += this.circles.distance[i] * this.circles.direc[i];
  178. }
  179. // If balloon position is out of bounds, restart
  180. if (this.balloonPlace < 66 || this.balloonPlace > 66 + 5 * distanceBetweenPoints) {
  181. this.restart = true;
  182. }
  183. }
  184. // KID
  185. this.availableAnimations['Right'] = ['Right', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 4];
  186. this.availableAnimations['Left'] = ['Left', [23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12], 4];
  187. this.kid = game.add.sprite(startX, 495 - this.circles.all.length * this.circles.diameter, 'kid_walk', 0, 0.8);
  188. this.kid.anchor(0.5, 0.8);
  189. if (sublevelType == 'Minus') {
  190. this.kid.animation = this.availableAnimations['Left'];
  191. this.kid.curFrame = 23;
  192. } else {
  193. this.kid.animation = this.availableAnimations['Right'];
  194. }
  195. // BALLOON
  196. this.balloon = game.add.image(this.balloonPlace, 350, 'balloon', 1, 0.5);
  197. this.balloon.alpha = 0.5;
  198. this.balloon.anchor(0.5, 0.5);
  199. this.basket = game.add.image(this.balloonPlace, 472, 'balloon_basket');
  200. this.basket.anchor(0.5, 0.5);
  201. // Help pointer
  202. this.help = game.add.image(0, 0, 'help_pointer', 0.5);
  203. this.help.anchor(0.5, 0);
  204. this.help.alpha = 0;
  205. if (!this.restart) {
  206. game.timer.start(); // Set a timer for the current level (used in postScore())
  207. game.event.add('click', this.func_onInputDown);
  208. game.event.add('mousemove', this.func_onInputOver);
  209. }
  210. },
  211. /**
  212. * Game loop
  213. */
  214. update: function () {
  215. self.count++;
  216. // Start animation
  217. if (self.animate) {
  218. let cur = self.circles.cur;
  219. let DIREC = self.circles.direc[cur];
  220. if (self.count % 2 == 0) { // Lowers animation
  221. // Move kid
  222. self.kid.x += 2 * DIREC;
  223. // Move circles
  224. for (let i in self.circles.all) {
  225. self.circles.all[i].x += 2 * DIREC;
  226. }
  227. // Manage line on the floor
  228. self.trace.width += 2 * DIREC;
  229. self.trace.lineColor = self.circles.all[cur].lineColor;
  230. // Change angle of current arc
  231. self.circles.angle[cur] += 4.6 * DIREC;
  232. self.circles.all[cur].angleEnd = game.math.degreeToRad(self.circles.angle[cur]);
  233. // When finish current circle
  234. let lowerCircles;
  235. if (self.circles.direction[cur] == 'Right') {
  236. lowerCircles = self.circles.all[cur].x >= self.nextX;
  237. }
  238. else if (self.circles.direction[cur] == 'Left') {
  239. lowerCircles = self.circles.all[cur].x <= self.nextX;
  240. // If just changed from 'right' to 'left' inform to change direction of kid animation
  241. if (self.changeAnimationFrames == undefined && cur > 0 && self.circles.direction[cur - 1] == 'Right') {
  242. self.changeAnimationFrames = true;
  243. }
  244. }
  245. // Change direction of kid animation
  246. if (self.changeAnimationFrames) {
  247. self.changeAnimationFrames = false;
  248. game.animation.stop(self.kid.animation[0]);
  249. self.kid.animation = self.availableAnimations['Left'];
  250. self.kid.curFrame = 23;
  251. game.animation.play(self.kid.animation[0]);
  252. }
  253. if (lowerCircles) {
  254. self.circles.all[cur].alpha = 0; // Cicle disappear
  255. self.circles.all.forEach(cur => {
  256. cur.y += self.circles.diameter; // Lower circles
  257. });
  258. self.kid.y += self.circles.diameter; // Lower kid
  259. self.circles.cur++; // Update current circle
  260. cur = self.circles.cur;
  261. DIREC = self.circles.direc[cur];
  262. self.nextX += self.circles.distance[cur] * DIREC; // Update next position
  263. }
  264. // When finish all circles (final position)
  265. if (cur == self.circles.all.length || self.circles.all[cur].alpha == 0) {
  266. self.animate = false;
  267. self.checkAnswer = true;
  268. }
  269. }
  270. }
  271. // Check if kid is inside the basket
  272. if (self.checkAnswer) {
  273. game.timer.stop();
  274. game.animation.stop(self.kid.animation[0]);
  275. if (self.func_checkOverlap(self.basket, self.kid)) {
  276. self.result = true; // Answer is correct
  277. self.kid.curFrame = (self.kid.curFrame < 12) ? 24 : 25;
  278. if (audioStatus) game.audio.okSound.play();
  279. game.add.image(defaultWidth / 2, defaultHeight / 2, 'ok').anchor(0.5, 0.5);
  280. completedLevels++;
  281. if (debugMode) console.log('completedLevels = ' + completedLevels);
  282. } else {
  283. self.result = false; // Answer is incorrect
  284. if (audioStatus) game.audio.errorSound.play();
  285. game.add.image(defaultWidth / 2, defaultHeight / 2, 'error').anchor(0.5, 0.5);
  286. }
  287. self.postScore();
  288. self.animateEnding = true;
  289. self.checkAnswer = false;
  290. self.count = 0;
  291. }
  292. // Balloon flying animation
  293. if (self.animateEnding) {
  294. self.balloon.y -= 2;
  295. self.basket.y -= 2;
  296. if (self.result) self.kid.y -= 2;
  297. if (self.count >= 140) {
  298. if (self.result) mapMove = true;
  299. else mapMove = false;
  300. game.state.start('map');
  301. }
  302. }
  303. game.render.all();
  304. },
  305. /* EVENT HANDLER */
  306. /**
  307. * Called by mouse click event
  308. *
  309. * @param {object} mouseEvent contains the mouse click coordinates
  310. */
  311. func_onInputDown: function (mouseEvent) {
  312. const x = mouseEvent.offsetX;
  313. const y = mouseEvent.offsetY;
  314. // LEVEL A : click road
  315. if (levelType == 'A') {
  316. const cur = self.road;
  317. const valid = y > 60 && (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  318. if (valid) self.func_clicked(x);
  319. }
  320. // LEVEL B : click circle
  321. if (levelType == 'B') {
  322. self.circles.all.forEach(cur => {
  323. const valid = game.math.distanceToPointer(x, cur.xWithAnchor, y, cur.yWithAnchor) <= (cur.diameter / 2) * cur.scale;
  324. if (valid) self.func_clicked(cur);
  325. });
  326. }
  327. navigationIcons.func_onInputDown(x, y);
  328. game.render.all();
  329. },
  330. /**
  331. * Called by mouse move event
  332. *
  333. * @param {object} mouseEvent contains the mouse move coordinates
  334. */
  335. func_onInputOver: function (mouseEvent) {
  336. const x = mouseEvent.offsetX;
  337. const y = mouseEvent.offsetY;
  338. let flag = false;
  339. // LEVEL A : balloon follow mouse
  340. if (levelType == 'A' && !self.hasClicked) {
  341. if (game.math.distanceToPointer(x, self.balloon.x, y, self.balloon.y) > 8) {
  342. self.balloon.x = x;
  343. self.basket.x = x;
  344. }
  345. document.body.style.cursor = 'auto';
  346. }
  347. // LEVEL B : hover circle
  348. if (levelType == 'B' && !self.hasClicked) {
  349. self.circles.all.forEach(cur => {
  350. const valid = game.math.distanceToPointer(x, cur.xWithAnchor, y, cur.yWithAnchor) <= (cur.diameter / 2) * cur.scale;
  351. if (valid) {
  352. self.func_overCircle(cur);
  353. flag = true;
  354. }
  355. });
  356. if (!flag) self.func_outCircle();
  357. }
  358. navigationIcons.func_onInputOver(x, y);
  359. game.render.all();
  360. },
  361. /* CALLED BY EVENT HANDLER */
  362. /**
  363. * (in levelType 'B')
  364. *
  365. * Function called when cursor is over a valid circle
  366. *
  367. * @param {object} cur circle the cursor is over
  368. */
  369. func_overCircle: function (cur) {
  370. if (!self.hasClicked) {
  371. document.body.style.cursor = 'pointer';
  372. for (let i in self.circles.all) {
  373. self.circles.all[i].alpha = (i <= cur.index) ? 1 : 0.5;
  374. }
  375. }
  376. },
  377. /**
  378. * (in levelType 'B')
  379. *
  380. * Function called when cursor is out of a valid circle
  381. */
  382. func_outCircle: function () {
  383. if (!self.hasClicked) {
  384. document.body.style.cursor = 'auto';
  385. self.circles.all.forEach(cur => {
  386. cur.alpha = 0.5;
  387. });
  388. }
  389. },
  390. /**
  391. * (in levelType 'B')
  392. *
  393. * Function called when player clicked over a valid circle
  394. *
  395. * @param {number|object} cur clicked circle
  396. */
  397. func_clicked: function (cur) {
  398. if (!self.hasClicked) {
  399. // On levelType A
  400. if (levelType == 'A') {
  401. self.balloon.x = cur;
  402. self.basket.x = cur;
  403. // On levelType B
  404. }
  405. else if (levelType == 'B') {
  406. document.body.style.cursor = 'auto';
  407. for (let i in self.circles.all) {
  408. if (i <= cur.index) {
  409. self.circles.all[i].alpha = 1; // Keep selected circle
  410. self.fractionIndex = cur.index;
  411. } else {
  412. self.circles.all[i].alpha = 0; // Hide unselected circle
  413. self.kid.y += self.circles.diameter; // Lower kid to selected circle
  414. }
  415. }
  416. }
  417. if (audioStatus) game.audio.beepSound.play();
  418. // Hide fractions
  419. if (fractionLabel) {
  420. self.circles.label.forEach(cur => {
  421. cur.forEach(cur => { cur.alpha = 0; });
  422. });
  423. }
  424. // Hide solution pointer
  425. if (self.help != undefined) self.help.alpha = 0;
  426. self.balloon.alpha = 1;
  427. self.trace.alpha = 1;
  428. self.hasClicked = true;
  429. self.animate = true;
  430. game.animation.play(this.kid.animation[0]);
  431. }
  432. },
  433. /* GAME FUNCTIONS */
  434. /**
  435. * Checks if 2 images overlap
  436. *
  437. * @param {object} spriteA image 1
  438. * @param {object} spriteB image 2
  439. * @returns {boolean}
  440. */
  441. func_checkOverlap: function (spriteA, spriteB) {
  442. const xA = spriteA.x;
  443. const xB = spriteB.x;
  444. // Consider it comming from both sides
  445. if (Math.abs(xA - xB) > 14) return false;
  446. else return true;
  447. },
  448. /**
  449. * Display correct answer
  450. */
  451. func_viewHelp: function () {
  452. if (!self.hasClicked) {
  453. // On levelType A
  454. if (levelType == 'A') {
  455. self.help.x = self.correctX;
  456. self.help.y = 490;
  457. // On levelType B
  458. } else {
  459. self.help.x = self.circles.all[self.endIndex - 1].x;
  460. self.help.y = self.circles.all[self.endIndex - 1].y - self.circles.diameter / 2;
  461. }
  462. self.help.alpha = 0.7;
  463. }
  464. },
  465. /* METADATA FOR GAME */
  466. /**
  467. * Saves players data after level ends - to be sent to database
  468. *
  469. * Attention: the "line_" prefix data table must be compatible to data table fields (MySQL server)
  470. * @see /php/squareOne.js
  471. */
  472. postScore: function () {
  473. // Creates string that is going to be sent to db
  474. const data = '&line_game=' + gameShape
  475. + '&line_mode=' + levelType
  476. + '&line_oper=' + sublevelType
  477. + '&line_leve=' + gameDifficulty
  478. + '&line_posi=' + mapPosition
  479. + '&line_resu=' + self.result
  480. + '&line_time=' + game.timer.elapsed
  481. + '&line_deta='
  482. + 'numCircles:' + self.circles.all.length
  483. + ', valCircles: ' + self.divisorsList
  484. + ' balloonX: ' + self.basket.x
  485. + ', selIndex: ' + self.fractionIndex;
  486. sendToDB(data);
  487. }
  488. };