squareTwo.js 14 KB

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