squareTwo.js 14 KB

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