difficulty.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // DIFFICULTY SCREEN: player can select (I) sublevel type, (II) game difficulty and (III) turn on/off fractions labels
  2. const difficultyScreen = {
  3. preload: function () {
  4. document.body.style.cursor = "auto";
  5. game.loop.stop();
  6. game.event.clear();
  7. game.animation.clear();
  8. self = this;
  9. // LOADING MEDIA
  10. game.load.sprite(game.url[gameTypeString].sprite);
  11. game.load.image(game.url[gameTypeString].image);
  12. },
  13. create: function () {
  14. game.render.clear();
  15. // Background color
  16. game.add.graphic.rect(0, 0, 900, 600, colors.white, 0, colors.blueBckg, 1);
  17. // Calls function that loads navigation icons
  18. navigationIcons.func_addIcons(false, true, false,
  19. true, false,
  20. false, false);
  21. // Title
  22. game.add.text(defaultWidth / 2, 40, game.lang.game_menu_title, textStyles.title2);
  23. // Icon that turns fraction labels ON/OFF
  24. if (gameTypeString != 'squareTwo') {
  25. const frame = (fractionLabel) ? 1 : 0;
  26. // Text : 'with/without' labeling the fractions
  27. this.labelText = game.add.text(defaultWidth / 2 - 100, 105, "", textStyles.subtitle2l);
  28. this.labelText.name = fractionLabel ? game.lang.with_name + " " + game.lang.label_name : game.lang.without_name + " " + game.lang.label_name;
  29. // Selection box
  30. this.selectionBox = game.add.sprite(defaultWidth / 2 - 110 - 30, 75, 'select', frame, 0.12);
  31. }
  32. // SETTING DIFFICULTY LEVELS (STAIR)
  33. this.stairs = [];
  34. const maxStairHeight = 120; // Maximum height of the stairs
  35. let stairHeight; // Height growth of a stair
  36. let stairWidth; // Width of the stairs
  37. let maxSublevel; // number of sublevels
  38. let maxDifficulty; // Number of difficulty stairs (in each sublevel)
  39. let xTheme; // Start 'x' position of the illustrations on the left (theme: character/arrow/equalSign)
  40. let xStair; // Start 'x' position of the stairs
  41. let xShape; // Start 'x' position of the illustrations on the right (shapes)
  42. switch (gameTypeString) {
  43. case "squareOne":
  44. stairHeight = 40;
  45. stairWidth = 100;
  46. maxSublevel = 2;
  47. maxDifficulty = 3;
  48. xTheme = 180;
  49. xStair = 320;
  50. xShape = (xTheme / 2) + xStair + stairWidth * maxDifficulty;
  51. break;
  52. case "squareTwo":
  53. case "circleOne":
  54. stairHeight = 29;
  55. stairWidth = 85;
  56. maxSublevel = 3;
  57. maxDifficulty = 5;
  58. xTheme = 150;
  59. xStair = 240;
  60. xShape = (xTheme / 2) + xStair + stairWidth * maxDifficulty;
  61. break;
  62. }
  63. // Placing icons
  64. switch (gameTypeString) {
  65. case "squareOne":
  66. // Blue square
  67. game.add.graphic.rect(xShape, 175, 80, 40, colors.darkBlue, 2, colors.lightBlue, 1);
  68. // Red square
  69. game.add.graphic.rect(xShape, 330, 80, 40, colors.red, 2, colors.lightBlue, 1);
  70. // Green tractor
  71. game.add.sprite(xTheme + 30 - 50, 215 - 40, 'tractor', 1, 0.5);
  72. // Red tractor
  73. game.add.sprite(xTheme + 70 - 50, 370 - 41, 'tractor', 5, 0.5);
  74. // Plus Arrow
  75. game.add.image(xTheme + 100 - 20, 215 - 20, 'arrow_right', 0.3);
  76. // Minus Arrow
  77. game.add.image(xTheme - 20, 370 - 20, 'arrow_left', 0.3);
  78. break;
  79. case "circleOne":
  80. // Blue Circle
  81. game.add.graphic.circle(xShape, 175, 60, colors.darkBlue, 2, colors.lightBlue, 1);
  82. // Red Circle
  83. game.add.graphic.circle(xShape, 330, 60, colors.red, 2, colors.lightBlue, 1);
  84. // Blue and red circle
  85. game.add.graphic.circle(xShape - 30, 485, 60, colors.darkBlue, 2, colors.lightBlue, 1);
  86. game.add.graphic.circle(xShape + 40, 485, 60, colors.red, 2, colors.lightBlue, 1);
  87. // Kid plus
  88. game.add.sprite(xTheme, 195, 'kid_walk', 0, 0.6).anchor(0.5, 0.5);
  89. // Kid minus
  90. game.add.sprite(xTheme + 40, 350, 'kid_walk', 12, 0.6).anchor(0.5, 0.5);
  91. // Plus arrow
  92. game.add.image(xTheme + 40 - 20, 195, 'arrow_right', 0.35);
  93. // Minus arrow
  94. game.add.image(xTheme - 20, 350 - 20, 'arrow_left', 0.35);
  95. // Both plus and minus arrows
  96. game.add.image(xTheme + 20 - 30, 500 - 40, 'arrow_double', 0.5);
  97. break;
  98. case "squareTwo":
  99. // Equal sign
  100. game.add.image(xTheme - 40, 370 - 40, 'equal', 0.7);
  101. break;
  102. }
  103. const aux = {
  104. color: [colors.diffBlue, colors.diffRed, colors.diffPurple],
  105. y: [135, 285, 435],
  106. _sublevel: [['Plus', 'Minus', 'Mixed'], ['A', 'B', 'C']],
  107. get sublevel() { return (gameTypeString == 'squareTwo') ? this._sublevel[1] : this._sublevel[0]; },
  108. };
  109. // Placing difficulty 'stairs'
  110. for (let i_subl = 0; i_subl < maxSublevel; i_subl++) { // sublevel (vertical)
  111. for (let j_dif = 1; j_dif <= maxDifficulty; j_dif++) { // difficulty (horizontal)
  112. // Parameters
  113. const x = xStair + (stairWidth * (j_dif - 1));
  114. const y = aux.y[i_subl] + maxStairHeight - j_dif * stairHeight;
  115. const width = stairWidth;
  116. const height = stairHeight * j_dif;
  117. // Difficulty stairs
  118. const stair = game.add.graphic.rect(x, y, width, height, colors.blueBckg, 1, aux.color[i_subl], 1);
  119. stair.difficulty = j_dif;
  120. stair.sublevelType = aux.sublevel[i_subl];
  121. this.stairs.push(stair);
  122. // Labels
  123. const xLabel = x + stairWidth / 2;
  124. const yLabel = y + (stairHeight * j_dif) / 2 + 10;
  125. game.add.text(xLabel, yLabel, j_dif, textStyles.difficultyLabel);
  126. }
  127. }
  128. game.event.add('click', difficultyScreen.func_onInputDown);
  129. game.event.add('mousemove', difficultyScreen.func_onInputOver);
  130. game.render.all();
  131. },
  132. /* EVENT HANDLER */
  133. func_onInputDown: function (mouseEvent) {
  134. const x = mouseEvent.offsetX;
  135. const y = mouseEvent.offsetY;
  136. // check if turned labels ON/OFF
  137. if (gameTypeString != 'squareTwo') {
  138. const cur = difficultyScreen.selectionBox;
  139. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  140. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  141. if (valid) difficultyScreen.func_setLabel(cur, difficultyScreen.labelText);
  142. }
  143. // check difficulty stairs
  144. difficultyScreen.stairs.forEach((cur) => {
  145. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  146. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  147. if (valid) {
  148. game.event.clear(self);
  149. difficultyScreen.func_loadMap(cur.difficulty, cur.sublevelType);
  150. }
  151. });
  152. navigationIcons.func_onInputDown(x, y);
  153. },
  154. func_onInputOver: function (mouseEvent) {
  155. const x = mouseEvent.offsetX;
  156. const y = mouseEvent.offsetY;
  157. const flag = [false, false];
  158. // check difficulty stairs
  159. difficultyScreen.stairs.forEach((cur) => {
  160. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  161. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  162. if (valid) {
  163. flag[0] = true;
  164. cur.alpha = 0.5;
  165. } else {
  166. cur.alpha = 1;
  167. }
  168. });
  169. if (gameTypeString != 'squareTwo') {
  170. const cur = difficultyScreen.selectionBox;
  171. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  172. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  173. if (valid) {
  174. flag[1] = true;
  175. }
  176. }
  177. if (flag[0] || flag[1]) document.body.style.cursor = "pointer";
  178. else document.body.style.cursor = "auto";
  179. navigationIcons.func_onInputOver(x, y);
  180. game.render.all();
  181. },
  182. /* GAME FUNCTIONS */
  183. // Calls map state
  184. func_loadMap: function (stairDifficulty, stairSublevelType) {
  185. if (audioStatus) game.audio.beepSound.play();
  186. mapPosition = 0; //Map position
  187. mapMove = true; //Move no next point
  188. gameDifficulty = stairDifficulty; // difficulty for selected level (1..3 or 1..5)
  189. sublevelType = stairSublevelType; //Type of game
  190. completedLevels = 0; //reset the game progress when entering a new level
  191. mapScreen.preload();
  192. },
  193. func_setLabel: function (selectionBox, selectionLabel) {
  194. if (fractionLabel) {
  195. fractionLabel = false;
  196. selectionBox.curFrame = 0;
  197. selectionLabel.name = game.lang.without_name + " " + game.lang.label_name;
  198. } else {
  199. fractionLabel = true;
  200. selectionBox.curFrame = 1;
  201. selectionLabel.name = game.lang.with_name + " " + game.lang.label_name;
  202. }
  203. if (audioStatus) game.audio.beepSound.play();
  204. game.render.all();
  205. }
  206. };