circleOne.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. let gameCircleOne = {
  3. create: function(){},
  4. update: function(){},
  5. ---------------------------- end of phaser functions
  6. func_overCircle: function(){},
  7. func_outCircle: function(){},
  8. func_clickCircle: function(){},
  9. func_setPlace: function(){},
  10. func_checkOverlap: function(_,_){}
  11. //func_getRndDivisor: function(){}
  12. func_viewHelp: function(){},
  13. func_postScore: function(){},
  14. func_updateCounter: function(){},
  15. };
  16. GAME LEVELS - CIRCLE I & II: balloon level
  17. Name of game state : 'circleOne'
  18. Shape : circle
  19. Character : kid/balloon
  20. Theme : flying a balloon
  21. Concept : 'How much the kid has to walk to get to the balloon?'
  22. Represent fractions as : circles
  23. # of different difficulties for each level : 5
  24. Levels can be : 'A' or 'B' (in variable 'levelType')
  25. A : Player can place balloon position
  26. Place balloon in position (so the kid can get to it)
  27. B : Player can select # of circles
  28. Selects number of circles (that represent distance kid needs to walk to get to the balloon)
  29. Sublevels can be : 'Plus', 'Minus' or 'Mixed' (in variable 'sublevelType')
  30. Plus : addition of fractions
  31. Represented by : kid going to the right (floor positions 0..5)
  32. Minus : subtraction of fractions
  33. Represented by: kid going to the left (floor positions 5..0)
  34. Mixed : Mix addition and subtraction of fractions in same
  35. Represented by: kid going to the left (floor positions 0..5)
  36. */
  37. let gameCircleOne = {
  38. create: function () {
  39. // CONTROL VARIABLES
  40. self = this; // saves a reference to gameSquareOne : to be used in functions called by phaser events
  41. this.checkAnswer = false; //Check kid inside ballon's basket
  42. this.animate = false; //Start move animation
  43. this.animateEnding = false; //Start ballon fly animation
  44. this.animateCounter = 0; //Fly counter
  45. this.hasClicked = false; //Air ballon positioned
  46. this.result = false; //Game is correct
  47. let hasBaseDifficulty = false; //If has level figure
  48. this.divisorsList = "";
  49. this.DIREC_LEVEL = (sublevelType == 'Minus') ? -1 : 1; // Will be multiplied to values to easily change kid direction when needed
  50. // GAME VARIABLES
  51. this.circleSize = 60; // Base circle width and height
  52. const startX = (sublevelType == 'Minus') ? 66 + 5 * 156 : 66; // Initial 'x' coordinate for the kid and the baloon
  53. // BACKGROUND AND KID
  54. // Add background image
  55. game.add.image(0, 0, 'bgimage');
  56. // Add clouds
  57. game.add.image(300, 100, 'cloud');
  58. game.add.image(660, 80, 'cloud');
  59. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  60. // Add floor of grass
  61. for (let i = 0; i < 9; i++) { game.add.image(i * 100, 501, 'floor'); }
  62. // Road
  63. const road = game.add.image(47, 515, 'road');
  64. road.scale.setTo(1.01, 0.94);
  65. if (levelType == 'A') {
  66. road.inputEnabled = true;
  67. road.events.onInputDown.add(this.func_setPlace); //enabling input for tablets
  68. }
  69. // Road points
  70. const placeDistance = 156; //Distance between places
  71. for (let p = 0; p <= 5; p++) {// Places
  72. const place = game.add.image(66 + p * placeDistance, 526, 'place_a');
  73. place.anchor.setTo(0.5);
  74. place.scale.setTo(0.3);
  75. game.add.text(66 + p * placeDistance, 560, p, textStyles.valueLabelBlue1).anchor.setTo(0.5);
  76. }
  77. // Calls function that loads navigation icons
  78. iconSettings.func_addIcons(true, true,
  79. true, true, true,
  80. true, false,
  81. 'difficulty', this.func_viewHelp);
  82. //trace
  83. this.trace = this.add.bitmapData(this.game.width, this.game.height);
  84. this.trace.addToWorld();
  85. this.trace.clear();
  86. // CIRCLES AND FRACTIONS
  87. this.circles = game.add.group(); //Fraction arrays
  88. this.curCircle = 0; //Actual index circle
  89. this.circleDirection = []; //Directions right(plus), left (minus)
  90. this.circleDistance = []; //Displacement distance of the circles
  91. this.circleAngle = []; //Angles of circles
  92. this.circleTraceColor = []; //Trace colors
  93. this.endPosition = startX; //Ending position, accumulative
  94. this.circleLabel = game.add.group(); //Labels of the circles
  95. this.fractionLines = game.add.group(); // line in the center of fraction
  96. //Maximum circles according to difficulty
  97. const maxCircles = (levelType == 'B' || sublevelType == 'Mixed') ? 6 : mapPosition + 1;
  98. this.numCircles = game.rnd.integerInRange(mapPosition, maxCircles); //Number of circles
  99. //Game B exclusive variables
  100. this.balloonPlace = this.game.world.centerX; //Fixed place for ballon (game B)
  101. this.fractionIndex = -1; //Index of clicked fraction (game B)
  102. this.numPlus = game.rnd.integerInRange(1, this.numCircles - 1);
  103. // CREATING CIRCLES
  104. for (let i = 0; i < this.numCircles; i++) {
  105. // Set divisor of fraction for the current circle (depends on difficulty)
  106. const divisor = game.rnd.integerInRange(1, levelDifficulty);
  107. // will validate that level isnt too easy (has at least one '1/difficulty' fraction)
  108. if (divisor == levelDifficulty) hasBaseDifficulty = true;
  109. // Add this divisor to the list of divisors (for func_postScore)
  110. this.divisorsList += divisor + ",";
  111. let direction = '';
  112. let lineColor = '';
  113. if (sublevelType == 'Mixed') {
  114. if (i <= this.numPlus) {
  115. direction = 'Right';
  116. lineColor = colors.darkBlue;
  117. } else {
  118. direction = 'Left';
  119. lineColor = colors.red;
  120. }
  121. } else if (sublevelType == 'Plus') {
  122. direction = 'Right';
  123. lineColor = colors.darkBlue;
  124. } else if (sublevelType == 'Minus') {
  125. direction = 'Left';
  126. lineColor = colors.red;
  127. }
  128. this.circleTraceColor[i] = lineColor;
  129. const circle = game.add.graphics(startX, 490 - i * this.circleSize);
  130. circle.anchor.setTo(0.5, 0.5);
  131. circle.lineStyle(2, lineColor);
  132. circle.beginFill(colors.lightBlue);
  133. if (direction == 'Right') circle.scale.y *= -1;
  134. this.circleDirection[i] = direction;
  135. if (divisor == 1) {
  136. circle.drawCircle(0, 0, this.circleSize);
  137. this.circleDistance.push(placeDistance);
  138. this.circleAngle.push(360);
  139. if (levelLabel) {
  140. const x = startX + 65 * this.DIREC_LEVEL;
  141. const label = game.add.text(x, 490 - i * this.circleSize, divisor, textStyles.valueLabelBlue1);
  142. label.anchor.setTo(0.5, 0.5);
  143. this.circleLabel.add(label);
  144. }
  145. } else {
  146. const distance = 360 / divisor + 5;
  147. circle.arc(0, 0, this.circleSize / 2, game.math.degToRad(distance), 0, true);
  148. this.circleDistance.push(Math.floor(placeDistance / divisor));
  149. this.circleAngle.push(distance);
  150. if (levelLabel) {
  151. const x = startX + 65 * this.DIREC_LEVEL;
  152. const label = game.add.text(x, 488 - i * this.circleSize, '1\n' + divisor, textStyles.valueLabelBlue1);
  153. label.anchor.setTo(0.5, 0.5);
  154. this.circleLabel.add(label);
  155. // Sprite that serves as line in the middle of a fraction
  156. const fractionLine = game.add.sprite(x, 485 - i * this.circleSize, 'fractionLine');
  157. fractionLine.anchor.setTo(0.5, 0.5);
  158. this.fractionLines.add(fractionLine);
  159. }
  160. }
  161. if (direction == 'Right') this.endPosition += Math.floor(placeDistance / divisor);
  162. else if (direction == 'Left') this.endPosition -= Math.floor(placeDistance / divisor);
  163. circle.endFill();
  164. circle.angle += 90;
  165. //If game is type B, (select fractions, adding event)
  166. if (levelType == 'B') {
  167. circle.alpha = 0.5;
  168. circle.inputEnabled = true;
  169. circle.input.useHandCursor = true;
  170. circle.events.onInputDown.add(this.func_clickCircle, { index: i });
  171. circle.events.onInputOver.add(this.func_overCircle, { index: i });
  172. circle.events.onInputOut.add(this.func_outCircle, { index: i });
  173. }
  174. this.circles.add(circle);
  175. }
  176. //Calculate next circle
  177. if (this.circleDirection[this.curCircle] == 'Right') this.nextEnd = startX + this.circleDistance[this.curCircle];
  178. else this.nextEnd = startX - this.circleDistance[this.curCircle];
  179. //If game is type B, selectiong a random balloon place
  180. if (levelType == 'B') {
  181. this.balloonPlace = startX;
  182. this.endIndex = game.rnd.integerInRange(this.numPlus, this.numCircles);
  183. for (let j = 0; j < this.endIndex; j++) {
  184. if (this.circleDirection[j] == 'Right') this.balloonPlace += this.circleDistance[j];
  185. else if (this.circleDirection[j] == 'Left') this.balloonPlace -= this.circleDistance[j];
  186. }
  187. if (this.balloonPlace < 66 || this.balloonPlace > 66 + 5 * placeDistance || !hasBaseDifficulty) {
  188. game.state.start('gameCircleOne');
  189. }
  190. }
  191. //If end position is out of bounds, restart
  192. if (this.endPosition < 66 || this.endPosition > 66 + 3 * 260 || !hasBaseDifficulty) {
  193. game.state.start('gameCircleOne');
  194. }
  195. //kid
  196. this.kid_walk = game.add.sprite(startX, 495 - this.numCircles * this.circleSize, 'kid_walk');
  197. this.kid_walk.anchor.setTo(0.5, 0.8);
  198. this.kid_walk.scale.setTo(0.8);
  199. this.kid_walk.animations.add('right', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
  200. this.kid_walk.animations.add('left', [23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12]);
  201. if (sublevelType == 'Minus') {
  202. this.kid_walk.animations.play('left', 6, true);
  203. this.kid_walk.animations.stop();
  204. }
  205. // BALLOON
  206. this.balloon = game.add.sprite(this.balloonPlace, 350, 'balloon');
  207. this.balloon.anchor.setTo(0.5, 0.5);
  208. this.balloon.alpha = 0.5;
  209. this.basket = game.add.sprite(this.balloonPlace, 472, 'balloon_basket');
  210. this.basket.anchor.setTo(0.5, 0.5);
  211. // OUTPUT ICONS
  212. // Ok image
  213. this.okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  214. this.okImg.anchor.setTo(0.5);
  215. this.okImg.visible = false;
  216. // Error image
  217. this.errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  218. this.errorImg.anchor.setTo(0.5);
  219. this.errorImg.visible = false;
  220. // TIMER
  221. // Set a timer for the current level
  222. this.totalTime = 0;
  223. this.timer = game.time.create(false);
  224. this.timer.loop(1000, this.func_updateCounter, this);
  225. this.timer.start();
  226. },
  227. update: function () {
  228. // if player clicked
  229. if (game.input.activePointer.isDown && !this.animateEnding && !this.hasClicked) {
  230. // Game wont consider click if clicking on navigation icons
  231. if (levelType == 'A' && game.input.mousePointer.y > 60) {
  232. this.balloon.x = game.input.mousePointer.x;
  233. this.basket.x = game.input.mousePointer.x;
  234. // Balloon is completely visible
  235. this.balloon.alpha = 1;
  236. if (audioStatus) beepSound.play();
  237. // Turn on kid animation
  238. if (this.circleDirection[this.curCircle] == 'Right') {
  239. this.kid_walk.animations.play('right', 6, true);
  240. } else {
  241. this.kid_walk.animations.play('left', 6, true);
  242. }
  243. //Hiding labels
  244. if (levelLabel) {
  245. this.circleLabel.visible = false;
  246. this.fractionLines.visible = false;
  247. }
  248. this.hasClicked = true;
  249. this.animate = true;
  250. }
  251. }
  252. // while player not clicked : track mouse to move baloon according to its position
  253. if (levelType == "A" && !this.hasClicked && !this.animateEnding) {
  254. if (game.physics.arcade.distanceToPointer(this.balloon, game.input.activePointer) > 8) {
  255. this.balloon.x = game.input.mousePointer.x;
  256. this.basket.x = game.input.mousePointer.x;
  257. }
  258. }
  259. //Start animation
  260. if (this.animate) {
  261. // create line on the ground and move kid
  262. let color;
  263. if (this.circleDirection[this.curCircle] == 'Right') {
  264. this.kid_walk.x += 2;
  265. color = 'rgba(0, 51, 153, 1)';
  266. } else if (this.circleDirection[this.curCircle] == 'Left') {
  267. this.kid_walk.x -= 2;
  268. color = 'rgba(179, 0, 0, 1)';
  269. }
  270. this.trace.rect(this.kid_walk.x, 526, 2, 2, color);
  271. //Moving every circle
  272. for (let i = 0; i < this.numCircles; i++) {
  273. if (this.circleDirection[this.curCircle] == 'Right') this.circles.children[i].x += 2;
  274. else this.circles.children[i].x -= 2;
  275. }
  276. this.circleAngle[this.curCircle] -= 4.6;
  277. this.circles.children[this.curCircle].clear();
  278. this.circles.children[this.curCircle].lineStyle(2, this.circleTraceColor[this.curCircle]);
  279. this.circles.children[this.curCircle].beginFill(colors.lightBlue);
  280. this.circles.children[this.curCircle].arc(0, 0, this.circleSize / 2, game.math.degToRad(this.circleAngle[this.curCircle]), 0, true);
  281. this.circles.children[this.curCircle].endFill();
  282. if ( (this.circleDirection[this.curCircle] == 'Right' && this.circles.children[this.curCircle].x >= this.nextEnd) ||
  283. (this.circleDirection[this.curCircle] == 'Left' && this.circles.children[this.curCircle].x <= this.nextEnd)
  284. ) {
  285. this.circles.children[this.curCircle].visible = false;
  286. this.circles.y += this.circleSize;
  287. this.kid_walk.y += this.circleSize;
  288. this.curCircle += 1;
  289. if (this.circleDirection[this.curCircle] == 'Right') {
  290. this.nextEnd += this.circleDistance[this.curCircle];
  291. this.kid_walk.animations.play('right', 6, true);
  292. } else if (this.circleDirection[this.curCircle] == 'Left') {
  293. this.nextEnd -= this.circleDistance[this.curCircle];
  294. this.kid_walk.animations.play('left', 6, true);
  295. }
  296. }
  297. if (this.curCircle == this.numCircles) { //Final position
  298. this.animate = false;
  299. this.checkAnswer = true;
  300. }
  301. }
  302. //Check if kid is inside the basket
  303. if (this.checkAnswer) {
  304. this.kid_walk.animations.stop();
  305. this.timer.stop();
  306. if (this.func_checkOverlap(this.basket, this.kid_walk)) {
  307. this.kid_walk.frame = (this.kid_walk.frame < 12) ? 24 : 25;
  308. this.result = true;
  309. if (audioStatus) okSound.play();
  310. this.okImg.visible = true;
  311. completedLevels++;
  312. if (debugMode) console.log("completedLevels = " + completedLevels);
  313. } else {
  314. this.result = false;
  315. if (audioStatus) errorSound.play();
  316. this.errorImg.visible = true;
  317. }
  318. this.func_postScore();
  319. this.animateEnding = true;
  320. this.checkAnswer = false;
  321. }
  322. // balloon flying animation
  323. if (this.animateEnding) {
  324. this.animateCounter++;
  325. this.balloon.y -= 2;
  326. this.basket.y -= 2;
  327. if (this.result) this.kid_walk.y -= 2;
  328. if (this.animateCounter >= 140) {
  329. if (this.result) mapCanMove = true;
  330. else mapCanMove = false;
  331. game.state.start('map');
  332. }
  333. }
  334. },
  335. // in levelType 'B'
  336. func_overCircle: function () {
  337. if (!self.hasClicked) {
  338. for (let i = 0; i < self.numCircles; i++) {
  339. self.circles.children[i].alpha = (i <= this.index) ? 1 : 0.5;
  340. }
  341. }
  342. },
  343. // in levelType 'B'
  344. func_outCircle: function () {
  345. if (!self.hasClicked) {
  346. for (let i = 0; i <= this.index; i++) {
  347. self.circles.children[i].alpha = 0.5;
  348. }
  349. }
  350. },
  351. // in levelType 'B'
  352. func_clickCircle: function () {
  353. if (!self.hasClicked) {
  354. let minusCircles = 0;
  355. for (let i = 0; i < self.numCircles; i++) {
  356. if (i <= this.index) {
  357. self.fractionIndex = this.index;
  358. self.circles.children[i].alpha = 1;
  359. } else {
  360. self.circles.children[i].visible = false; //Delete unselected circle
  361. minusCircles += 1; //number of circles to reduce
  362. self.kid_walk.y += self.circleSize; // Lower kid to selected circle
  363. }
  364. }
  365. self.numCircles -= minusCircles; //Final reduced circles
  366. self.balloon.alpha = 1;
  367. if (audioStatus) beepSound.play();
  368. if (self.circleDirection[self.curCircle] == 'Right') {
  369. self.kid_walk.animations.play('right', 6, true);
  370. } else {
  371. self.kid_walk.animations.play('left', 6, true);
  372. }
  373. if (levelLabel) { //Hiding labels
  374. self.circleLabel.visible = false;
  375. self.fractionLines.visible = false;
  376. }
  377. self.hasClicked = true;
  378. self.animate = true;
  379. }
  380. },
  381. func_setPlace: function () {
  382. if (!self.hasClicked) {
  383. self.balloon.x = game.input.x;
  384. self.basket.x = game.input.x;
  385. self.balloon.alpha = 1;
  386. if (audioStatus) beepSound.play();
  387. if (self.circleDirection[self.curCircle] == 'Right') {
  388. self.kid_walk.animations.play('right', 6, true);
  389. } else {
  390. self.kid_walk.animations.play('left', 6, true);
  391. }
  392. if (levelLabel) { //Hiding labels
  393. self.circleLabel.visible = false;
  394. self.fractionLines.visible = false;
  395. }
  396. self.hasClicked = true;
  397. self.animate = true;
  398. }
  399. },
  400. func_checkOverlap: function (spriteA, spriteB) {
  401. const xA = spriteA.x;
  402. const xB = spriteB.x;
  403. if (Math.abs(xA - xB) > 14) return false;
  404. else return true;
  405. },
  406. func_viewHelp: function () {
  407. if (!self.hasClicked) {
  408. let pointer;
  409. if (levelType == 'A') {
  410. pointer = game.add.image(self.endPosition, 490, 'pointer');
  411. } else {
  412. pointer = game.add.image(self.circles.children[self.endIndex - 1].x, self.circles.children[self.endIndex - 1].y - self.circleSize / 2, 'pointer');
  413. }
  414. pointer.anchor.setTo(0.5, 0);
  415. pointer.alpha = 0.7;
  416. }
  417. },
  418. // Game information
  419. func_updateCounter: function () {
  420. this.totalTime++;
  421. },
  422. func_postScore: function () {
  423. const abst = "numCircles:" + this.numCircles
  424. + ", valCircles: " + this.divisorsList
  425. + " balloonX: " + this.basket.x
  426. + ", selIndex: " + this.fractionIndex;
  427. let hr = new XMLHttpRequest();
  428. // Create some variables we need to send to our PHP file
  429. const url = "php/save.php";
  430. let vars = "s_ip=" + hip
  431. + "&s_name=" + playerName
  432. + "&s_lang=" + langString
  433. + "&s_game=" + levelShape
  434. + "&s_mode=" + levelType;
  435. vars += "&s_oper=" + sublevelType
  436. + "&s_leve=" + levelDifficulty
  437. + "&s_posi=" + mapPosition
  438. + "&s_resu=" + this.result
  439. + "&s_time=" + this.totalTime
  440. + "&s_deta=" + abst;
  441. hr.open("POST", url, true);
  442. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  443. hr.onreadystatechange = function () {
  444. if (debugMode) console.log(hr);
  445. if (hr.readyState == 4 && hr.status == 200) {
  446. let return_data = hr.responseText;
  447. if (debugMode) console.log(return_data);
  448. }
  449. }
  450. // Send the data to PHP now... and wait for response to update the status div
  451. hr.send(vars); // Actually execute the request
  452. if (debugMode) console.log("processing...");
  453. if (debugMode) console.log(vars);
  454. }
  455. };