squareTwo.js 14 KB

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