squareTwo.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. *
  3. * LInE - Free Education, Private Data
  4. *
  5. * iFractions GAME STATE
  6. *
  7. * LEVELS - SQUARE III: fraction comparisson level
  8. *
  9. * Name of game state : 'squareTwo'
  10. * Shape : square
  11. * Character : kid
  12. * Theme : (not themed)
  13. * Concept : player select equivalent dividends for fractions with different divisors
  14. * Represent fractions as : subdivided blocks
  15. *
  16. * # of different difficulties for each level : 5
  17. *
  18. * Levels can be : 'A' or 'B' (in variable 'levelType')
  19. *
  20. * A : equivalence of fractions
  21. * top has more subdivisions
  22. * B : equivalence of fractions
  23. * bottom has more subdivisions
  24. *
  25. * Sublevel : 'Equals' (in variable 'sublevelType')
  26. *
  27. * Equals : Player selects equivalent fractions of both blocks
  28. *
  29. * @namespace
  30. */
  31. const squareTwo = {
  32. /**
  33. * Main code
  34. */
  35. create: function () {
  36. // CONTROL VARIABLES
  37. this.result = false; // Check if selected blocks are correct
  38. this.delay = 0; // Counter for game dalays
  39. this.endLevel = false;
  40. this.A = {
  41. blocks: [], // List of selection blocks
  42. auxBlocks: [], // List of shadow under selection blocks
  43. fractions: [], // Fraction numbers
  44. selected: 0, // Number of selected blocks for A
  45. hasClicked: false, // Check if player clicked blocks from A
  46. animate: false, // Animate blocks from A
  47. warningText: undefined,
  48. label: undefined,
  49. };
  50. this.B = {
  51. blocks: [],
  52. auxBlocks: [],
  53. fractions: [],
  54. selected: 0,
  55. hasClicked: false,
  56. animate: false,
  57. warningText: undefined,
  58. label: undefined,
  59. };
  60. // BACKGROUND AND KID
  61. // Add background image
  62. game.add.image(0, 0, 'bgimage');
  63. // Add clouds
  64. game.add.image(300, 100, 'cloud');
  65. game.add.image(660, 80, 'cloud');
  66. game.add.image(110, 85, 'cloud', 0.8);
  67. // Add floor of grass
  68. for (let i = 0; i < 9; i++) { game.add.image(i * 100, 501, 'floor'); }
  69. // Calls function that loads navigation icons
  70. navigationIcons.func_addIcons(
  71. true, true, false,
  72. true, false,
  73. 'customMenu', false);
  74. // Add kid
  75. this.kidAnimation = game.add.sprite(100, 470, 'kid_standing', 5, 0.8);
  76. this.kidAnimation.anchor(0.5, 0.7);
  77. // Width and Height of A and B
  78. this.figureWidth = 400;
  79. const figureHeight = 50;
  80. // Coordinates for A and B
  81. let xA, xB, yA, yB;
  82. if (levelType != 'B') { // More subdivisions on B
  83. xA = 230;
  84. yA = 90;
  85. xB = xA;
  86. yB = yA + 3 * figureHeight + 30;
  87. } else { // More subdivisions on A
  88. xB = 230;
  89. yB = 90;
  90. xA = xB;
  91. yA = yB + 3 * figureHeight + 30;
  92. }
  93. // Possible points for A
  94. const points = [2, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20];
  95. // Random index for 'points'
  96. const randomIndex = game.math.randomInRange((gameDifficulty - 1) * 2 + 1, (gameDifficulty - 1) * 2 + 3);
  97. // Number of subdivisions of A and B (blocks)
  98. const totalBlocksA = points[randomIndex];
  99. const totalBlocksB = game.math.randomDivisor(totalBlocksA);
  100. if (debugMode) {
  101. console.log('----------');
  102. console.log('Difficulty ' + gameDifficulty + ', ini ' + ((gameDifficulty - 1) * 2 + 1) + ', end ' + ((gameDifficulty - 1) * 2 + 3));
  103. console.log('Rpoint ' + randomIndex + ', val ' + totalBlocksA);
  104. console.log('total blocks A ' + totalBlocksA + ', total blocks B ');
  105. }
  106. // CREATING TOP FIGURE (A)
  107. let blockWidth = this.figureWidth / totalBlocksA; // Width of each block in A
  108. let lineColor = colors.darkRed;
  109. let fillColor = colors.lightRed;
  110. // Create blocks
  111. for (let i = 0; i < totalBlocksA; i++) {
  112. const x = xA + i * blockWidth;
  113. // Blocks
  114. const block = game.add.graphic.rect(x, yA, blockWidth, figureHeight, lineColor, 2, fillColor, 0.5);
  115. block.figure = 'A';
  116. block.index = i;
  117. block.finalX = xA;
  118. this.A.blocks.push(block);
  119. // Auxiliar blocks
  120. const alpha = (fractionLabel) ? 0.1 : 0;
  121. const yAux = yA + figureHeight + 10; // On the bottom of A
  122. const auxBlock = game.add.graphic.rect(x, yAux, blockWidth, figureHeight, lineColor, 1, fillColor, alpha);
  123. this.A.auxBlocks.push(auxBlock);
  124. }
  125. // 'total blocks' label for A : on the side of A
  126. let xLabel = xA + this.figureWidth + 30;
  127. let yLabel = yA + figureHeight / 2;
  128. this.A.label = game.add.text(xLabel, yLabel, this.A.blocks.length, textStyles.h4_blue);
  129. // 'selected blocks/fraction' label for A : at the bottom of A
  130. yLabel = yA + figureHeight + 34;
  131. this.A.fractions[0] = game.add.text(xLabel, yLabel, '', textStyles.h4_blue);
  132. this.A.fractions[1] = game.add.text(xLabel, yLabel + 21, '', textStyles.h4_blue);
  133. this.A.fractions[2] = game.add.text(xLabel, yLabel, '___', textStyles.h4_blue);
  134. this.A.fractions[0].alpha = 0;
  135. this.A.fractions[1].alpha = 0;
  136. this.A.fractions[2].alpha = 0;
  137. // CREATING BOTTOM FIGURE (B)
  138. blockWidth = this.figureWidth / totalBlocksB; // Width of each block in B
  139. lineColor = colors.darkGreen;
  140. fillColor = colors.lightGreen;
  141. // Blocks and auxiliar blocks
  142. for (let i = 0; i < totalBlocksB; i++) {
  143. const x = xB + i * blockWidth;
  144. // Blocks
  145. const block = game.add.graphic.rect(x, yB, blockWidth, figureHeight, lineColor, 2, fillColor, 0.5);
  146. block.figure = 'B';
  147. block.index = i;
  148. block.finalX = xB;
  149. this.B.blocks.push(block);
  150. // Auxiliar blocks
  151. const alpha = (fractionLabel) ? 0.1 : 0;
  152. const yAux = yB + figureHeight + 10; // On the bottom of B
  153. const auxBlock = game.add.graphic.rect(x, yAux, blockWidth, figureHeight, lineColor, 1, fillColor, alpha);
  154. this.B.auxBlocks.push(auxBlock);
  155. }
  156. // Label block B
  157. xLabel = xB + this.figureWidth + 30;
  158. yLabel = yB + figureHeight / 2;
  159. this.B.label = game.add.text(xLabel, yLabel, this.B.blocks.length, textStyles.h4_blue);
  160. // Label fraction
  161. yLabel = yB + figureHeight + 34;
  162. this.B.fractions[0] = game.add.text(xLabel, yLabel, '', textStyles.h4_blue);
  163. this.B.fractions[1] = game.add.text(xLabel, yLabel + 21, '', textStyles.h4_blue);
  164. this.B.fractions[2] = game.add.text(xLabel, yLabel, '___', textStyles.h4_blue);
  165. this.B.fractions[0].alpha = 0;
  166. this.B.fractions[1].alpha = 0;
  167. this.B.fractions[2].alpha = 0;
  168. // Invalid selection text
  169. this.A.warningText = game.add.text(defaultWidth / 2, defaultHeight / 2 - 225, '', textStyles.h4_brown);
  170. this.B.warningText = game.add.text(defaultWidth / 2, defaultHeight / 2 - 45, '', textStyles.h4_brown);
  171. game.timer.start(); // Set a timer for the current level (used in postScore)
  172. game.event.add('click', this.func_onInputDown);
  173. game.event.add('mousemove', this.func_onInputOver);
  174. },
  175. /**
  176. * Game loop
  177. */
  178. update: function () {
  179. // Animate blocks
  180. if (self.A.animate || self.B.animate) {
  181. ['A', 'B'].forEach(cur => {
  182. if (self[cur].animate) {
  183. // Lower selected blocks
  184. for (let i = 0; i < self[cur].selected; i++) {
  185. self[cur].blocks[i].y += 2;
  186. }
  187. // After fully lowering blocks, set fraction value
  188. if (self[cur].blocks[0].y >= self[cur].auxBlocks[0].y) {
  189. self[cur].fractions[0].name = self[cur].selected;
  190. self[cur].animate = false;
  191. }
  192. }
  193. });
  194. }
  195. // If A and B are already clicked
  196. if (self.A.hasClicked && self.B.hasClicked && !self.endLevel) {
  197. game.timer.stop();
  198. self.delay++;
  199. // After delay is over, check result
  200. if (self.delay > 50) {
  201. self.result = (self.A.selected / self.A.blocks.length) == (self.B.selected / self.B.blocks.length);
  202. // Fractions are equivalent : CORRECT
  203. if (self.result) {
  204. if (audioStatus) game.audio.okSound.play();
  205. game.add.image(defaultWidth / 2, defaultHeight / 2, 'ok').anchor(0.5, 0.5);
  206. mapMove = true; // Allow character to move to next level in map state
  207. completedLevels++;
  208. if (debugMode) console.log('completedLevels = ' + completedLevels);
  209. // Fractions are not equivalent : INCORRECT
  210. } else {
  211. if (audioStatus) game.audio.errorSound.play();
  212. game.add.image(defaultWidth / 2, defaultHeight / 2, 'error').anchor(0.5, 0.5);
  213. mapMove = false; // Doesnt allow character to move to next level in map state
  214. }
  215. self.postScore();
  216. self.endLevel = true;
  217. // Reset delay values for next delay
  218. self.delay = 0;
  219. }
  220. }
  221. // Wait a bit and go to map state
  222. if (self.endLevel) {
  223. self.delay++;
  224. if (self.delay >= 80) {
  225. game.state.start('map');
  226. }
  227. }
  228. game.render.all();
  229. },
  230. /* EVENT HANDLER */
  231. /**
  232. * Called by mouse click event
  233. *
  234. * @param {object} mouseEvent contains the mouse click coordinates
  235. */
  236. func_onInputDown: function (mouseEvent) {
  237. const x = mouseEvent.offsetX;
  238. const y = mouseEvent.offsetY;
  239. // Click block in A
  240. self.A.blocks.forEach(cur => {
  241. if (game.math.isOverIcon(x, y, cur)) self.func_clickSquare(cur);
  242. });
  243. // Click block in B
  244. self.B.blocks.forEach(cur => {
  245. if (game.math.isOverIcon(x, y, cur)) self.func_clickSquare(cur);
  246. });
  247. // Click navigation icons
  248. navigationIcons.func_onInputDown(x, y);
  249. game.render.all();
  250. },
  251. /**
  252. * Called by mouse move event
  253. *
  254. * @param {object} mouseEvent contains the mouse move coordinates
  255. */
  256. func_onInputOver: function (mouseEvent) {
  257. const x = mouseEvent.offsetX;
  258. const y = mouseEvent.offsetY;
  259. let flagA = false;
  260. let flagB = false;
  261. // Mouse over A : show fraction
  262. self.A.blocks.forEach(cur => {
  263. if (game.math.isOverIcon(x, y, cur)) {
  264. flagA = true;
  265. self.func_overSquare(cur);
  266. }
  267. });
  268. if (!flagA) self.func_outSquare('A');
  269. // Mouse over B : show fraction
  270. self.B.blocks.forEach(cur => {
  271. if (game.math.isOverIcon(x, y, cur)) {
  272. flagB = true;
  273. self.func_overSquare(cur);
  274. }
  275. });
  276. if (!flagB) self.func_outSquare('B');
  277. if (!flagA && !flagB) document.body.style.cursor = 'auto';
  278. // Mouse over navigation icons : show name
  279. navigationIcons.func_onInputOver(x, y);
  280. game.render.all();
  281. },
  282. /* CALLED BY EVENT HANDLER */
  283. /**
  284. * Function called when cursor is over a valid rectangle
  285. *
  286. * @param {object} curBlock rectangle the cursor is over
  287. */
  288. func_overSquare: function (curBlock) { // curBlock : self.A.blocks[i] || self.B.blocks[i]
  289. const curSet = curBlock.figure; // 'A' || 'B'
  290. if (!self[curSet].hasClicked) { // self.A.hasClicked || self.B.hasClicked
  291. // If over fraction 'n/n' shows warning message not allowing it
  292. if (curBlock.index == self[curSet].blocks.length - 1) {
  293. const otherSet = (curSet == 'A') ? 'B' : 'A';
  294. self[curSet].warningText.name = game.lang.error_msg;
  295. self[otherSet].warningText.name = '';
  296. self.func_outSquare(curSet);
  297. } else {
  298. document.body.style.cursor = 'pointer';
  299. self.A.warningText.name = '';
  300. self.B.warningText.name = '';
  301. // Selected blocks become fully visible
  302. for (let i in self[curSet].blocks) {
  303. self[curSet].blocks[i].alpha = (i <= curBlock.index) ? 1 : 0.5;
  304. }
  305. self[curSet].fractions[0].name = curBlock.index + 1; // Nominator : selected blocks
  306. self[curSet].fractions[1].name = self[curSet].blocks.length; // Denominator : total blocks
  307. const newX = curBlock.finalX + ((curBlock.index + 1) * (self.figureWidth / self[curSet].blocks.length)) + 25;
  308. self[curSet].fractions[0].x = newX;
  309. self[curSet].fractions[1].x = newX;
  310. self[curSet].fractions[2].x = newX;
  311. self[curSet].fractions[0].alpha = 1;
  312. }
  313. }
  314. },
  315. /**
  316. * Function called when cursor is out of a valid rectangle
  317. *
  318. * @param {object} curSet set of rectangles (top or bottom)
  319. */
  320. func_outSquare: function (curSet) { // curSet : self.A || self.B
  321. if (!self[curSet].hasClicked) {
  322. self[curSet].fractions[0].alpha = 0;
  323. self[curSet].fractions[1].alpha = 0;
  324. self[curSet].fractions[2].alpha = 0;
  325. self[curSet].blocks.forEach(cur => {
  326. cur.alpha = 0.5;
  327. });
  328. }
  329. },
  330. /**
  331. * Function called when player clicked a valid rectangle
  332. *
  333. * @param {object} curBlock clicked rectangle
  334. */
  335. func_clickSquare: function (curBlock) { // curBlock : self.A.blocks[i] || self.B.blocks[i]
  336. const curSet = curBlock.figure; // 'A' || 'B'
  337. if (!self[curSet].hasClicked && curBlock.index != self[curSet].blocks.length - 1) {
  338. document.body.style.cursor = 'auto';
  339. // Turn auxiliar blocks invisible
  340. for (let i in self[curSet].blocks) {
  341. if (i > curBlock.index) self[curSet].auxBlocks[i].alpha = 0;
  342. }
  343. // Turn value label invisible
  344. self[curSet].label.alpha = 0;
  345. if (audioStatus) game.audio.beepSound.play();
  346. // Save number of selected blocks
  347. self[curSet].selected = curBlock.index + 1;
  348. // Set fraction x position
  349. const newX = curBlock.finalX + (self[curSet].selected * (self.figureWidth / self[curSet].blocks.length)) + 25;
  350. self[curSet].fractions[0].x = newX;
  351. self[curSet].fractions[1].x = newX;
  352. self[curSet].fractions[2].x = newX;
  353. self[curSet].fractions[1].alpha = 1;
  354. self[curSet].fractions[2].alpha = 1;
  355. self[curSet].hasClicked = true; // Inform player have clicked in current block set
  356. self[curSet].animate = true; // Let it initiate animation
  357. }
  358. game.render.all();
  359. },
  360. /* METADATA FOR GAME */
  361. /**
  362. * Saves players data after level ends - to be sent to database
  363. *
  364. * Attention: the "line_" prefix data table must be compatible to data table fields (MySQL server)
  365. * @see /php/save.php
  366. */
  367. postScore: function () {
  368. // Creates string that is going to be sent to db
  369. const data = '&line_game=' + gameShape
  370. + '&line_mode=' + levelType
  371. + '&line_oper=Equal'
  372. + '&line_leve=' + gameDifficulty
  373. + '&line_posi=' + mapPosition
  374. + '&line_resu=' + self.result
  375. + '&line_time=' + game.timer.elapsed
  376. + '&line_deta='
  377. + 'numBlocksA: ' + self.A.blocks.length
  378. + ', valueA: ' + self.A.selected
  379. + ', numBlocksB: ' + self.B.blocks.length
  380. + ', valueB: ' + self.B.selected;
  381. sendToDB(data);
  382. }
  383. };