circleOne.js 18 KB

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