squareTwo.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. menuScreenCustom, 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. // Create blocks
  106. for (let i = 0; i < totalBlocksA; i++) {
  107. const x = xA + i * blockWidth;
  108. // Blocks
  109. const block = game.add.graphic.rect(x, yA, blockWidth, figureHeight, lineColor, 2, fillColor, 0.5);
  110. block.figure = 'A';
  111. block.index = i;
  112. block.finalX = xA;
  113. self.A.blocks.push(block);
  114. // Auxiliar blocks
  115. const alpha = (sublevelType == 'A') ? 0.2 : 0;
  116. const yAux = yA + figureHeight + 10; // on the bottom of A
  117. const auxBlock = game.add.graphic.rect(x, yAux, blockWidth, figureHeight, lineColor, 1, fillColor, alpha);
  118. self.A.auxBlocks.push(auxBlock);
  119. }
  120. // 'total blocks' label for A : on the side of A
  121. let xLabel = xA + this.figureWidth + 30;
  122. let yLabel = yA + figureHeight / 2;
  123. this.A.label = game.add.text(xLabel, yLabel, this.A.blocks.length, textStyles.h4_blue);
  124. // 'selected blocks/fraction' label for A : at the bottom of A
  125. yLabel = yA + figureHeight + 34;
  126. self.A.fractions[0] = game.add.text(xLabel, yLabel, "", textStyles.h4_blue);
  127. self.A.fractions[1] = game.add.text(xLabel, yLabel + 21, "", textStyles.h4_blue);
  128. self.A.fractions[2] = game.add.text(xLabel, yLabel, "___", textStyles.h4_blue);
  129. self.A.fractions[0].alpha = 0;
  130. self.A.fractions[1].alpha = 0;
  131. self.A.fractions[2].alpha = 0;
  132. // CREATING BOTTOM FIGURE (B)
  133. blockWidth = this.figureWidth / totalBlocksB; // width of each block in B
  134. lineColor = colors.darkRed;
  135. fillColor = colors.lightRed;
  136. // Blocks and auxiliar blocks
  137. for (let i = 0; i < totalBlocksB; i++) {
  138. const x = xB + i * blockWidth;
  139. // Blocks
  140. const block = game.add.graphic.rect(x, yB, blockWidth, figureHeight, lineColor, 2, fillColor, 0.5);
  141. block.figure = 'B';
  142. block.index = i;
  143. block.finalX = xB;
  144. self.B.blocks.push(block);
  145. // Auxiliar blocks
  146. const alpha = (sublevelType == 'A') ? 0.1 : 0;
  147. const yAux = yB + figureHeight + 10; // on the bottom of B
  148. const auxBlock = game.add.graphic.rect(x, yAux, blockWidth, figureHeight, lineColor, 1, fillColor, alpha);
  149. self.B.auxBlocks.push(auxBlock);
  150. }
  151. // Label block B
  152. xLabel = xB + this.figureWidth + 30;
  153. yLabel = yB + figureHeight / 2;
  154. this.B.label = game.add.text(xLabel, yLabel, this.B.blocks.length, textStyles.h4_blue);
  155. // Label fraction
  156. yLabel = yB + figureHeight + 34;
  157. self.B.fractions[0] = game.add.text(xLabel, yLabel, "", textStyles.h4_blue);
  158. self.B.fractions[1] = game.add.text(xLabel, yLabel + 21, "", textStyles.h4_blue);
  159. self.B.fractions[2] = game.add.text(xLabel, yLabel, "___", textStyles.h4_blue);
  160. self.B.fractions[0].alpha = 0;
  161. self.B.fractions[1].alpha = 0;
  162. self.B.fractions[2].alpha = 0;
  163. // Invalid selection text
  164. self.A.warningText = game.add.text(defaultWidth / 2, defaultHeight / 2 - 225, "", textStyles.h4_brown);
  165. self.B.warningText = game.add.text(defaultWidth / 2, defaultHeight / 2 - 45, "", textStyles.h4_brown);
  166. game.render.all();
  167. game.timer.start(); // Set a timer for the current level (used in func_postScore)
  168. game.event.add("click", squareTwo.func_onInputDown);
  169. game.event.add("mousemove", squareTwo.func_onInputOver);
  170. game.loop.start(this);
  171. },
  172. update: function () {
  173. // Animate blocks
  174. if (self.A.animate || self.B.animate) {
  175. ['A', 'B'].forEach(cur => {
  176. if (self[cur].animate) {
  177. // Lower selected blocks
  178. for (let i = 0; i < self[cur].selected; i++) {
  179. self[cur].blocks[i].y += 2;
  180. }
  181. // After fully lowering blocks, set fraction value
  182. if (self[cur].blocks[0].y >= self[cur].auxBlocks[0].y) {
  183. self[cur].fractions[0].name = self[cur].selected;
  184. self[cur].animate = false;
  185. }
  186. }
  187. });
  188. }
  189. // if A and B are already clicked
  190. if (self.A.hasClicked && self.B.hasClicked && !self.endLevel) {
  191. game.timer.stop();
  192. self.delay++;
  193. // After delay is over, check result
  194. if (self.delay > 50) {
  195. self.result = (self.A.selected / self.A.blocks.length) == (self.B.selected / self.B.blocks.length);
  196. // fractions are equivalent : CORRECT
  197. if (self.result) {
  198. if (audioStatus) game.audio.okSound.play();
  199. game.add.image(defaultWidth / 2, defaultHeight / 2, 'ok').anchor(0.5, 0.5);
  200. mapMove = true; // allow character to move to next level in map state
  201. completedLevels++;
  202. if (debugMode) console.log("completedLevels = " + completedLevels);
  203. // fractions are not equivalent : INCORRECT
  204. } else {
  205. if (audioStatus) game.audio.errorSound.play();
  206. game.add.image(defaultWidth / 2, defaultHeight / 2, 'error').anchor(0.5, 0.5);
  207. mapMove = false; // doesnt allow character to move to next level in map state
  208. }
  209. self.func_postScore();
  210. self.endLevel = true;
  211. // reset delay values for next delay
  212. self.delay = 0;
  213. }
  214. }
  215. // Wait a bit and go to map state
  216. if (self.endLevel) {
  217. self.delay++;
  218. if (self.delay >= 80) {
  219. mapScreen.preload();
  220. }
  221. }
  222. game.render.all();
  223. },
  224. /* EVENT HANDLER */
  225. func_onInputDown: function (mouseEvent) {
  226. const x = mouseEvent.offsetX;
  227. const y = mouseEvent.offsetY;
  228. // click block in A
  229. self.A.blocks.forEach(cur => {
  230. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) && (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  231. if (valid) self.func_clickSquare(cur);
  232. });
  233. // click block in B
  234. self.B.blocks.forEach(cur => {
  235. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) && (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  236. if (valid) self.func_clickSquare(cur);
  237. });
  238. // click navigation icons
  239. navigationIcons.func_onInputDown(x, y);
  240. game.render.all();
  241. },
  242. func_onInputOver: function (mouseEvent) {
  243. const x = mouseEvent.offsetX;
  244. const y = mouseEvent.offsetY;
  245. let flagA = false;
  246. let flagB = false;
  247. // mouse over A : show fraction
  248. self.A.blocks.forEach(cur => {
  249. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) &&
  250. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  251. if (valid) {
  252. flagA = true;
  253. self.func_overSquare(cur);
  254. }
  255. });
  256. if (!flagA) self.func_outSquare('A');
  257. // mouse over B : show fraction
  258. self.B.blocks.forEach(cur => {
  259. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) &&
  260. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  261. if (valid) {
  262. flagB = true;
  263. self.func_overSquare(cur);
  264. }
  265. });
  266. if (!flagB) self.func_outSquare('B');
  267. if (!flagA && !flagB) document.body.style.cursor = "auto";
  268. // mouse over navigation icons : show name
  269. navigationIcons.func_onInputOver(x, y);
  270. game.render.all();
  271. },
  272. /* CALLED BY EVENT HANDLER */
  273. func_overSquare: function (curBlock) { // curBlock : self.A.blocks[i] || self.B.blocks[i]
  274. const curSet = curBlock.figure; // "A" || "B"
  275. if (!self[curSet].hasClicked) { // self.A.hasClicked || self.B.hasClicked
  276. // If over fraction 'n/n' shows warning message not allowing it
  277. if (curBlock.index == self[curSet].blocks.length - 1) {
  278. const otherSet = (curSet == "A") ? "B" : "A";
  279. self[curSet].warningText.name = game.lang.error_msg;
  280. self[otherSet].warningText.name = "";
  281. self.func_outSquare(curSet);
  282. } else {
  283. document.body.style.cursor = "pointer";
  284. self.A.warningText.name = "";
  285. self.B.warningText.name = "";
  286. // selected blocks become fully visible
  287. for (let i in self[curSet].blocks) {
  288. self[curSet].blocks[i].alpha = (i <= curBlock.index) ? 1 : 0.5;
  289. }
  290. self[curSet].fractions[0].name = curBlock.index + 1; // nominator : selected blocks
  291. self[curSet].fractions[1].name = self[curSet].blocks.length; // denominator : total blocks
  292. const newX = curBlock.finalX + ((curBlock.index + 1) * (self.figureWidth / self[curSet].blocks.length)) + 25;;
  293. self[curSet].fractions[0].x = newX;
  294. self[curSet].fractions[1].x = newX;
  295. self[curSet].fractions[2].x = newX;
  296. self[curSet].fractions[0].alpha = 1;
  297. }
  298. }
  299. },
  300. func_outSquare: function (curSet) { // curSet : self.A || self.B
  301. if (!self[curSet].hasClicked) {
  302. self[curSet].fractions[0].alpha = 0;
  303. self[curSet].fractions[1].alpha = 0;
  304. self[curSet].fractions[2].alpha = 0;
  305. self[curSet].blocks.forEach(cur => {
  306. cur.alpha = 0.5;
  307. });
  308. }
  309. },
  310. func_clickSquare: function (curBlock) { // curBlock : self.A.blocks[i] || self.B.blocks[i]
  311. const curSet = curBlock.figure; // "A" || "B"
  312. if (!self[curSet].hasClicked && curBlock.index != self[curSet].blocks.length - 1) {
  313. document.body.style.cursor = "auto";
  314. // turn auxiliar blocks invisible
  315. for (let i in self[curSet].blocks) {
  316. if (i > curBlock.index) self[curSet].auxBlocks[i].alpha = 0;
  317. }
  318. // turn value label invisible
  319. self[curSet].label.alpha = 0;
  320. if (audioStatus) game.audio.beepSound.play();
  321. // Save number of selected blocks
  322. self[curSet].selected = curBlock.index + 1;
  323. // set fraction x position
  324. const newX = curBlock.finalX + (self[curSet].selected * (self.figureWidth / self[curSet].blocks.length)) + 25;
  325. self[curSet].fractions[0].x = newX;
  326. self[curSet].fractions[1].x = newX;
  327. self[curSet].fractions[2].x = newX;
  328. self[curSet].fractions[1].alpha = 1;
  329. self[curSet].fractions[2].alpha = 1;
  330. self[curSet].hasClicked = true; // inform player have clicked in current block set
  331. self[curSet].animate = true; // let it initiate animation
  332. }
  333. game.render.all();
  334. },
  335. /* METADATA FOR GAME */
  336. func_postScore: function () {
  337. // Create some variables we need to send to our PHP file
  338. const data = "&s_game=" + gameShape
  339. + "&s_mode=" + levelType
  340. + "&s_oper=Equal"
  341. + "&s_leve=" + gameDifficulty
  342. + "&s_posi=" + mapPosition
  343. + "&s_resu=" + self.result
  344. + "&s_time=" + game.timer.elapsed
  345. + "&s_deta="
  346. + "numBlocksA: " + self.A.blocks.length
  347. + ", valueA: " + self.A.selected
  348. + ", numBlocksB: " + self.B.blocks.length
  349. + ", valueB: " + self.B.selected;;
  350. postScore(data);
  351. }
  352. };