squareTwo.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. GAME LEVELS - SQUARE III: fraction comparisson level
  3. Name of game state : 'SquareTwo'
  4. Shape : square
  5. Character : kid
  6. Theme : (not themed)
  7. Concept : player select equivalent dividends for fractions with different divisors
  8. Represent fractions as : subdivided blocks
  9. # of different difficulties for each level : 5
  10. Level : 'C' (in variable 'levelType')
  11. C : Player selects equivalent fractions of both blocks
  12. Sublevels can be : 'A', 'B' or 'C' (in variable 'sublevelType')
  13. A : equivalence of fractions (with low opacity blocks under the selectable blocks)
  14. top has more subdivisions
  15. B : equivalence of fractions
  16. top has more subdivisions
  17. C : equivalence of fractions
  18. bottom has more subdivisions
  19. */
  20. const squareTwo = {
  21. preload: function () {
  22. document.body.style.cursor = "auto";
  23. game.loop.stop();
  24. game.event.clear();
  25. game.animation.clear();
  26. self = this;
  27. // NOTHING TO LOAD HERE
  28. squareTwo.create();
  29. },
  30. create: function () {
  31. game.render.clear();
  32. // CONTROL VARIABLES
  33. this.result = false; // Check if selected blocks are correct
  34. this.delay = 0; // Counter for game dalays
  35. this.endLevel = false;
  36. self.A = {
  37. blocks: [], // List of selection blocks
  38. auxBlocks: [], // List of shadow under selection blocks
  39. fractions: [], // fraction numbers
  40. selected: 0, // Number of selected blocks for A
  41. hasClicked: false, // Check if player clicked blocks from A
  42. animate: false, // Animate blocks from A
  43. warningText: undefined,
  44. label: undefined,
  45. };
  46. self.B = {
  47. blocks: [],
  48. auxBlocks: [],
  49. fractions: [],
  50. selected: 0,
  51. hasClicked: false,
  52. animate: false,
  53. warningText: undefined,
  54. label: undefined,
  55. };
  56. // BACKGROUND AND KID
  57. // Add background image
  58. game.add.image(0, 0, 'bgimage');
  59. // Add clouds
  60. game.add.image(300, 100, 'cloud');
  61. game.add.image(660, 80, 'cloud');
  62. game.add.image(110, 85, 'cloud', 0.8);
  63. // Add floor of grass
  64. for (let i = 0; i < 9; i++) { game.add.image(i * 100, 501, 'floor'); }
  65. // Calls function that loads navigation icons
  66. navigationIcons.func_addIcons(true, true, false,
  67. true, false,
  68. difficultyScreen, false);
  69. //Add kid
  70. this.kidAnimation = game.add.sprite(100, 470, 'kid_standing', 5, 0.8);
  71. this.kidAnimation.anchor(0.5, 0.7);
  72. // Width and Height of A and B
  73. this.figureWidth = 400;
  74. const figureHeight = 50;
  75. // Coordinates for A and B
  76. let xA, xB, yA, yB;
  77. if (sublevelType != 'C') { // More subdivisions on B
  78. xA = 230;
  79. yA = 90;
  80. xB = xA;
  81. yB = yA + 3 * figureHeight + 30;
  82. } else { // More subdivisions on A
  83. xB = 230;
  84. yB = 90;
  85. xA = xB;
  86. yA = yB + 3 * figureHeight + 30;
  87. }
  88. // Possible points for A
  89. const points = [2, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20];
  90. // Random index for 'points'
  91. const randomIndex = game.math.randomInRange((gameDifficulty - 1) * 2 + 1, (gameDifficulty - 1) * 2 + 3);
  92. // number of subdivisions of A and B (blocks)
  93. const totalBlocksA = points[randomIndex];
  94. const totalBlocksB = game.math.randomDivisor(totalBlocksA);
  95. if (debugMode) {
  96. console.log("----------");
  97. console.log("Difficulty " + gameDifficulty + ", ini " + ((gameDifficulty - 1) * 2 + 1) + ", end " + ((gameDifficulty - 1) * 2 + 3));
  98. console.log("Rpoint " + randomIndex + ", val " + totalBlocksA);
  99. console.log("total blocks A " + totalBlocksA + ", total blocks B ");
  100. }
  101. // CREATING TOP FIGURE (A)
  102. let blockWidth = this.figureWidth / totalBlocksA; // width of each block in A
  103. let lineColor = colors.darkGreen;
  104. let fillColor = colors.lightGreen;
  105. let fillColorAux = colors.lighterGreen;
  106. // Create blocks
  107. for (let i = 0; i < totalBlocksA; i++) {
  108. const x = xA + i * blockWidth;
  109. // Blocks
  110. const block = game.add.graphic.rect(x, yA, blockWidth, figureHeight, lineColor, 2, fillColor, 0.5);
  111. block.figure = 'A';
  112. block.index = i;
  113. block.finalX = xA;
  114. self.A.blocks.push(block);
  115. // Auxiliar blocks
  116. const alpha = (sublevelType == 'A') ? 0.2 : 0;
  117. const yAux = yA + figureHeight + 10; // on the bottom of A
  118. const auxBlock = game.add.graphic.rect(x, yAux, blockWidth, figureHeight, lineColor, 1, fillColorAux, alpha);
  119. self.A.auxBlocks.push(auxBlock);
  120. }
  121. // 'total blocks' label for A : on the side of A
  122. let xLabel = xA + this.figureWidth + 30;
  123. let yLabel = yA + figureHeight / 2;
  124. this.A.label = game.add.text(xLabel, yLabel, this.A.blocks.length, textStyles.valueLabelBlue2);
  125. // 'selected blocks/fraction' label for A : at the bottom of A
  126. yLabel = yA + figureHeight + 34;
  127. self.A.fractions[0] = game.add.text(xLabel, yLabel, "", textStyles.valueLabelBlue2);
  128. self.A.fractions[1] = game.add.text(xLabel, yLabel + 21, "", textStyles.valueLabelBlue2);
  129. self.A.fractions[2] = game.add.text(xLabel, yLabel, "___", textStyles.valueLabelBlue2);
  130. self.A.fractions[0].alpha = 0;
  131. self.A.fractions[1].alpha = 0;
  132. self.A.fractions[2].alpha = 0;
  133. // CREATING BOTTOM FIGURE (B)
  134. blockWidth = this.figureWidth / totalBlocksB; // width of each block in B
  135. lineColor = colors.darkRed;
  136. fillColor = colors.lightRed;
  137. fillColorAux = colors.lighterRed;
  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. self.B.blocks.push(block);
  147. // Auxiliar blocks
  148. const alpha = (sublevelType == 'A') ? 0.2 : 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, fillColorAux, alpha);
  151. self.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.valueLabelBlue2);
  157. // Label fraction
  158. yLabel = yB + figureHeight + 34;
  159. self.B.fractions[0] = game.add.text(xLabel, yLabel, "", textStyles.valueLabelBlue2);
  160. self.B.fractions[1] = game.add.text(xLabel, yLabel + 21, "", textStyles.valueLabelBlue2);
  161. self.B.fractions[2] = game.add.text(xLabel, yLabel, "___", textStyles.valueLabelBlue2);
  162. self.B.fractions[0].alpha = 0;
  163. self.B.fractions[1].alpha = 0;
  164. self.B.fractions[2].alpha = 0;
  165. // Invalid selection text
  166. self.A.warningText = game.add.text(defaultWidth / 2, defaultHeight / 2 - 225, "", textStyles.overtitle);
  167. self.B.warningText = game.add.text(defaultWidth / 2, defaultHeight / 2 - 45, "", textStyles.overtitle);
  168. game.render.all();
  169. game.timer.start(); // Set a timer for the current level (used in func_postScore)
  170. game.event.add("click", squareTwo.func_onInputDown);
  171. game.event.add("mousemove", squareTwo.func_onInputOver);
  172. game.loop.start(this);
  173. },
  174. update: function () {
  175. // Animate blocks
  176. if (self.A.animate || self.B.animate) {
  177. ['A', 'B'].forEach(cur => {
  178. if (self[cur].animate) {
  179. // Lower selected blocks
  180. for (let i = 0; i < self[cur].selected; i++) {
  181. self[cur].blocks[i].y += 2;
  182. }
  183. // After fully lowering blocks, set fraction value
  184. if (self[cur].blocks[0].y >= self[cur].auxBlocks[0].y) {
  185. self[cur].fractions[0].name = self[cur].selected;
  186. self[cur].animate = false;
  187. }
  188. }
  189. });
  190. }
  191. // if A and B are already clicked
  192. if (self.A.hasClicked && self.B.hasClicked && !self.endLevel) {
  193. game.timer.stop();
  194. self.delay++;
  195. // After delay is over, check result
  196. if (self.delay > 50) {
  197. self.result = (self.A.selected / self.A.blocks.length) == (self.B.selected / self.B.blocks.length);
  198. // fractions are equivalent : CORRECT
  199. if (self.result) {
  200. if (audioStatus) game.audio.okSound.play();
  201. game.add.image(defaultWidth / 2, defaultHeight / 2, 'ok').anchor(0.5, 0.5);
  202. mapMove = true; // allow character to move to next level in map state
  203. completedLevels++;
  204. if (debugMode) console.log("completedLevels = " + completedLevels);
  205. // fractions are not equivalent : INCORRECT
  206. } else {
  207. if (audioStatus) game.audio.errorSound.play();
  208. game.add.image(defaultWidth / 2, defaultHeight / 2, 'error').anchor(0.5, 0.5);
  209. mapMove = false; // doesnt allow character to move to next level in map state
  210. }
  211. self.func_postScore();
  212. self.endLevel = true;
  213. // reset delay values for next delay
  214. self.delay = 0;
  215. }
  216. }
  217. // Wait a bit and go to map state
  218. if (self.endLevel) {
  219. self.delay++;
  220. if (self.delay >= 80) {
  221. mapScreen.preload();
  222. }
  223. }
  224. game.render.all();
  225. },
  226. /* EVENT HANDLER */
  227. func_onInputDown: function (mouseEvent) {
  228. const x = mouseEvent.offsetX;
  229. const y = mouseEvent.offsetY;
  230. // click block in A
  231. self.A.blocks.forEach(cur => {
  232. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) && (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  233. if (valid) self.func_clickSquare(cur);
  234. });
  235. // click block in B
  236. self.B.blocks.forEach(cur => {
  237. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) && (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  238. if (valid) self.func_clickSquare(cur);
  239. });
  240. // click navigation icons
  241. navigationIcons.func_onInputDown(x, y);
  242. game.render.all();
  243. },
  244. func_onInputOver: function (mouseEvent) {
  245. const x = mouseEvent.offsetX;
  246. const y = mouseEvent.offsetY;
  247. let flagA = false;
  248. let flagB = false;
  249. // mouse over A : show fraction
  250. self.A.blocks.forEach(cur => {
  251. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  252. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  253. if (valid) {
  254. flagA = true;
  255. self.func_overSquare(cur);
  256. }
  257. });
  258. if (!flagA) self.func_outSquare('A');
  259. // mouse over B : show fraction
  260. self.B.blocks.forEach(cur => {
  261. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  262. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  263. if (valid) {
  264. flagB = true;
  265. self.func_overSquare(cur);
  266. }
  267. });
  268. if (!flagB) self.func_outSquare('B');
  269. if (!flagA && !flagB) document.body.style.cursor = "auto";
  270. // mouse over navigation icons : show name
  271. navigationIcons.func_onInputOver(x, y);
  272. game.render.all();
  273. },
  274. /* CALLED BY EVENT HANDLER */
  275. func_overSquare: function (curBlock) { // curBlock : self.A.blocks[i] || self.B.blocks[i]
  276. const curSet = curBlock.figure; // "A" || "B"
  277. if (!self[curSet].hasClicked) { // self.A.hasClicked || self.B.hasClicked
  278. // If over fraction 'n/n' shows warning message not allowing it
  279. if (curBlock.index == self[curSet].blocks.length - 1) {
  280. const otherSet = (curSet == "A") ? "B" : "A";
  281. self[curSet].warningText.name = game.lang.error_msg;
  282. self[otherSet].warningText.name = "";
  283. self.func_outSquare(curSet);
  284. } else {
  285. document.body.style.cursor = "pointer";
  286. self.A.warningText.name = "";
  287. self.B.warningText.name = "";
  288. // selected blocks become fully visible
  289. for (let i in self[curSet].blocks) {
  290. self[curSet].blocks[i].alpha = (i <= curBlock.index) ? 1 : 0.5;
  291. }
  292. self[curSet].fractions[0].name = curBlock.index + 1; // nominator : selected blocks
  293. self[curSet].fractions[1].name = self[curSet].blocks.length; // denominator : total blocks
  294. const newX = curBlock.finalX + ((curBlock.index + 1) * (self.figureWidth / self[curSet].blocks.length)) + 25;;
  295. self[curSet].fractions[0].x = newX;
  296. self[curSet].fractions[1].x = newX;
  297. self[curSet].fractions[2].x = newX;
  298. self[curSet].fractions[0].alpha = 1;
  299. }
  300. }
  301. },
  302. func_outSquare: function (curSet) { // curSet : self.A || self.B
  303. if (!self[curSet].hasClicked) {
  304. self[curSet].fractions[0].alpha = 0;
  305. self[curSet].fractions[1].alpha = 0;
  306. self[curSet].fractions[2].alpha = 0;
  307. self[curSet].blocks.forEach(cur => {
  308. cur.alpha = 0.5;
  309. });
  310. }
  311. },
  312. func_clickSquare: function (curBlock) { // curBlock : self.A.blocks[i] || self.B.blocks[i]
  313. const curSet = curBlock.figure; // "A" || "B"
  314. if (!self[curSet].hasClicked && curBlock.index != self[curSet].blocks.length - 1) {
  315. document.body.style.cursor = "auto";
  316. // turn auxiliar blocks invisible
  317. for (let i in self[curSet].blocks) {
  318. if (i > curBlock.index) self[curSet].auxBlocks[i].alpha = 0;
  319. }
  320. // turn value label invisible
  321. self[curSet].label.alpha = 0;
  322. if (audioStatus) game.audio.beepSound.play();
  323. // Save number of selected blocks
  324. self[curSet].selected = curBlock.index + 1;
  325. // set fraction x position
  326. const newX = curBlock.finalX + (self[curSet].selected * (self.figureWidth / self[curSet].blocks.length)) + 25;
  327. self[curSet].fractions[0].x = newX;
  328. self[curSet].fractions[1].x = newX;
  329. self[curSet].fractions[2].x = newX;
  330. self[curSet].fractions[1].alpha = 1;
  331. self[curSet].fractions[2].alpha = 1;
  332. self[curSet].hasClicked = true; // inform player have clicked in current block set
  333. self[curSet].animate = true; // let it initiate animation
  334. }
  335. game.render.all();
  336. },
  337. /* METADATA FOR GAME */
  338. func_postScore: function () {
  339. // Create some variables we need to send to our PHP file
  340. const data = "&s_game=" + gameShape
  341. + "&s_mode=" + levelType
  342. + "&s_oper=Equal"
  343. + "&s_leve=" + gameDifficulty
  344. + "&s_posi=" + mapPosition
  345. + "&s_resu=" + self.result
  346. + "&s_time=" + game.timer.elapsed
  347. + "&s_deta="
  348. + "numBlocksA: " + self.A.blocks.length
  349. + ", valueA: " + self.A.selected
  350. + ", numBlocksB: " + self.B.blocks.length
  351. + ", valueB: " + self.B.selected;;
  352. postScore(data);
  353. }
  354. };