squareTwo.js 14 KB

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