difficulty.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. let difficultyState = {
  3. preload: function(){},
  4. create: function(){},
  5. ---------------------------- end of phaser functions
  6. func_loadMap: function()
  7. func_setLabel: function()
  8. };
  9. */
  10. // DIFFICULTY SCREEN: player can select (I) sublevel type, (II) game difficulty and (III) turn on/off fractions labels
  11. let difficultyState = {
  12. preload: function () {
  13. let curMedia;
  14. switch (currentGameState) {
  15. case 'gameSquareOne': curMedia = media.gameSquareOne; break;
  16. case 'gameSquareTwo': curMedia = media.gameSquareTwo; break;
  17. case 'gameCircleOne': curMedia = media.gameCircleOne; break;
  18. }
  19. for (let i = 0, image = curMedia('image'); i < image.length; i++) {
  20. game.load.image(image[i][0], image[i][1]);
  21. }
  22. for (let i = 0, sprite = curMedia('spritesheet'); i < sprite.length; i++) {
  23. game.load.spritesheet(sprite[i][0], sprite[i][1], sprite[i][2], sprite[i][3], sprite[i][4]);
  24. }
  25. for (let i = 0, audio = curMedia('audio'); i < audio.length; i++) {
  26. game.load.audio(audio[i][0], audio[i][1][0], audio[i][1][1]);
  27. }
  28. },
  29. create: function () {
  30. // Calls function that loads navigation icons
  31. iconSettings.func_addIcons(true, true,
  32. false, true, false,
  33. true, false,
  34. false, false);
  35. // TITLE
  36. const title = game.add.text(game.world.centerX, 40, lang.game_menu_title, textStyles.title2);
  37. title.anchor.setTo(0.5, 0.5);
  38. // TURN LABEL ON/OFF ICON
  39. if (currentGameState != 'gameSquareTwo') {
  40. // Text : 'with/without' labeling the fractions
  41. let labelText = game.add.text(game.world.centerX - 110, 80, "", textStyles.subtitle2);
  42. labelText.text = levelLabel ? lang.with_name + " " + lang.label_name : lang.without_name + " " + lang.label_name;
  43. labelText.anchor.setTo(0, 0.5);
  44. // Selection box
  45. let selectionBox = game.add.sprite(game.world.centerX - 110 - 30, 75, 'select');
  46. selectionBox.frame = levelLabel ? 1 : 0;
  47. selectionBox.anchor.setTo(0.5, 0.5);
  48. selectionBox.scale.setTo(0.12);
  49. selectionBox.inputEnabled = true;
  50. selectionBox.input.useHandCursor = true;
  51. selectionBox.events.onInputDown.add(this.func_setLabel, { selectionBox: selectionBox, labelText: labelText });
  52. }
  53. // SETTING DIFFICULTY LEVELS (STAIR)
  54. const maxStairHeight = 120; // Maximum height of the stairs
  55. let stairHeight; // Height growth of a stair
  56. let stairWidth; // Width of the stairs
  57. let startStair; // Start 'x' position of the stairs
  58. let startTheme; // Start 'x' position of the illustrations on the left (character/arrow/equal sign)
  59. let startShape; // STart 'x' position of the illustrations on the right (shapes)
  60. let maxSublevel;
  61. let maxDifficulty;
  62. switch (currentGameState) {
  63. case "gameSquareOne":
  64. stairHeight = 40;
  65. stairWidth = 100;
  66. startStair = 320;
  67. startTheme = 180;
  68. startShape = (startTheme / 2) + startStair + stairWidth * 3;
  69. maxSublevel = 2;
  70. maxDifficulty = 3;
  71. break;
  72. case "gameSquareTwo":
  73. case "gameCircleOne":
  74. stairHeight = 29;
  75. stairWidth = 85;
  76. startStair = 240;
  77. startTheme = 150;
  78. startShape = (startTheme / 2) + startStair + stairWidth * 5;
  79. maxSublevel = 3;
  80. maxDifficulty = 5;
  81. break;
  82. default: if (debugMode) console.log("Error! Name of the game state is not valid!");
  83. }
  84. let themeIcons = [];
  85. let arrowIcons = [];
  86. let shapeIcons = [];
  87. let stairs = [];
  88. const aux = {
  89. maxSublevel: null,
  90. maxDifficulty: null,
  91. color: [colors.diffBlue, colors.diffRed, colors.diffPurple],
  92. base_y1: [135, 285, 435],
  93. sublevel_1: ['Plus', 'Minus', 'Mixed'],
  94. sublevel_2: ['A', 'B', 'C'],
  95. get sublevel() {
  96. return (currentGameState == 'gameSquareTwo') ? this.sublevel_2 : this.sublevel_1;
  97. },
  98. }
  99. // Placing icons
  100. switch (currentGameState) {
  101. case "gameSquareOne":
  102. // Blue square
  103. shapeIcons[0] = game.add.graphics(startShape, 175);
  104. shapeIcons[0].anchor.setTo(0.5, 0.5);
  105. shapeIcons[0].lineStyle(2, colors.darkBlue);
  106. shapeIcons[0].beginFill(colors.lightBlue);
  107. shapeIcons[0].drawRect(0, 0, 80, 40);
  108. shapeIcons[0].endFill();
  109. // Red square
  110. shapeIcons[1] = game.add.graphics(startShape, 330);
  111. shapeIcons[1].anchor.setTo(0.5, 0.5);
  112. shapeIcons[1].lineStyle(2, colors.red);
  113. shapeIcons[1].beginFill(colors.lightBlue);
  114. shapeIcons[1].drawRect(0, 0, 80, 40);
  115. shapeIcons[1].endFill();
  116. // Green tractor
  117. themeIcons[0] = game.add.sprite(startTheme + 30, 215, 'tractor');
  118. themeIcons[0].frame = 1;
  119. themeIcons[0].scale.setTo(0.5);
  120. themeIcons[0].alpha = 0.9;
  121. themeIcons[0].anchor.setTo(0.5, 0.5);
  122. // Red tractor
  123. themeIcons[1] = game.add.sprite(startTheme + 70, 370, 'tractor');
  124. themeIcons[1].frame = 5;
  125. themeIcons[1].scale.setTo(0.5);
  126. themeIcons[1].alpha = 0.9;
  127. themeIcons[1].anchor.setTo(0.5, 0.5);
  128. // Plus Arrow
  129. arrowIcons[0] = game.add.sprite(startTheme + 100, 215, 'h_arrow');
  130. arrowIcons[0].scale.setTo(0.3);
  131. arrowIcons[0].alpha = 0.9;
  132. arrowIcons[0].anchor.setTo(0.5, 0.5);
  133. // Minus Arrow
  134. arrowIcons[1] = game.add.sprite(startTheme, 370, 'h_arrow');
  135. arrowIcons[1].scale.setTo(0.3);
  136. arrowIcons[1].alpha = 0.9;
  137. arrowIcons[1].anchor.setTo(0.5, 0.5);
  138. arrowIcons[1].scale.x *= -1;
  139. break;
  140. case "gameCircleOne":
  141. // Blue Circle
  142. shapeIcons[0] = game.add.graphics(startShape, 175);
  143. shapeIcons[0].anchor.setTo(0.5, 0.5);
  144. shapeIcons[0].lineStyle(2, colors.darkBlue);
  145. shapeIcons[0].beginFill(colors.lightBlue);
  146. shapeIcons[0].drawCircle(0, 0, 60);
  147. shapeIcons[0].endFill();
  148. // Red Circle
  149. shapeIcons[1] = game.add.graphics(startShape, 330);
  150. shapeIcons[1].anchor.setTo(0.5, 0.5);
  151. shapeIcons[1].lineStyle(2, colors.red);
  152. shapeIcons[1].beginFill(colors.lightBlue);
  153. shapeIcons[1].drawCircle(0, 0, 60);
  154. shapeIcons[1].endFill();
  155. // Both blue and red circles
  156. shapeIcons[2] = game.add.graphics(startShape - 30, 485);
  157. shapeIcons[2].anchor.setTo(0.5, 0.5);
  158. shapeIcons[2].lineStyle(2, colors.darkBlue);
  159. shapeIcons[2].beginFill(colors.lightBlue);
  160. shapeIcons[2].drawCircle(0, 0, 60);
  161. shapeIcons[2].endFill();
  162. shapeIcons[3] = game.add.graphics(startShape + 40, 485);
  163. shapeIcons[3].anchor.setTo(0.5, 0.5);
  164. shapeIcons[3].lineStyle(2, colors.red);
  165. shapeIcons[3].beginFill(colors.lightBlue);
  166. shapeIcons[3].drawCircle(0, 0, 60);
  167. shapeIcons[3].endFill();
  168. // Kid plus
  169. themeIcons[0] = game.add.sprite(startTheme, 195, 'kid_walk');
  170. themeIcons[0].scale.setTo(0.6);
  171. themeIcons[0].alpha = 0.8;
  172. themeIcons[0].anchor.setTo(0.5, 0.5);
  173. // Kid minus
  174. themeIcons[1] = game.add.sprite(startTheme + 40, 350, 'kid_walk');
  175. themeIcons[1].scale.setTo(-0.6, 0.6);
  176. themeIcons[1].alpha = 0.8;
  177. themeIcons[1].anchor.setTo(0.5, 0.5);
  178. // Plus arrow
  179. arrowIcons[0] = game.add.sprite(startTheme + 40, 195, 'h_arrow');
  180. arrowIcons[0].scale.setTo(0.35);
  181. arrowIcons[0].alpha = 0.8;
  182. arrowIcons[0].anchor.setTo(0.5, 0.5);
  183. // Minus arrow
  184. arrowIcons[1] = game.add.sprite(startTheme, 350, 'h_arrow');
  185. arrowIcons[1].scale.setTo(-0.35, 0.35);
  186. arrowIcons[1].alpha = 0.8;
  187. arrowIcons[1].anchor.setTo(0.5, 0.5);
  188. // Both plus and minus arrows
  189. arrowIcons[2] = game.add.sprite(startTheme + 20, 500, 'h_double');
  190. arrowIcons[2].scale.setTo(0.5);
  191. arrowIcons[2].alpha = 0.8;
  192. arrowIcons[2].anchor.setTo(0.5, 0.5);
  193. break;
  194. case "gameSquareTwo":
  195. themeIcons[0] = game.add.sprite(startTheme, 370, 'equal');
  196. themeIcons[0].scale.setTo(0.7);
  197. themeIcons[0].anchor.setTo(0.5, 0.5);
  198. break;
  199. default: if (debugMode) console.log("Error: couldn't finish loading difficulty screen assets");
  200. }
  201. // Placing difficulty 'stairs'
  202. for (let sublevel = 0; sublevel < maxSublevel; sublevel++) {
  203. for (let difficulty = 1; difficulty <= maxDifficulty; difficulty++) {
  204. // Position
  205. const x1 = startStair + (stairWidth * (difficulty - 1));
  206. const y1 = aux.base_y1[sublevel] + maxStairHeight - difficulty * stairHeight;
  207. const x2 = stairWidth;//x1 + 40;
  208. const y2 = stairHeight * difficulty;//y1 + 24;
  209. // Graphics
  210. stairs[difficulty] = game.add.graphics(0, 0);
  211. stairs[difficulty].lineStyle(1, colors.blueBckg);
  212. stairs[difficulty].beginFill(aux.color[sublevel]);
  213. stairs[difficulty].drawRect(x1, y1, x2, y2);
  214. stairs[difficulty].endFill();
  215. stairs[difficulty].inputEnabled = true;
  216. stairs[difficulty].input.useHandCursor = true;
  217. stairs[difficulty].events.onInputDown.add(this.func_loadMap, { difficulty: difficulty, sublevelType: aux.sublevel[sublevel] });
  218. stairs[difficulty].events.onInputOver.add(function (item) { item.alpha = 0.5; }, this);
  219. stairs[difficulty].events.onInputOut.add(function (item) { item.alpha = 1; }, this);
  220. // Labels
  221. const xl = x1 + stairWidth / 2; //x label
  222. const yl = y1 + (stairHeight * difficulty) / 2; //y label
  223. const label = game.add.text(xl, yl, difficulty, textStyles.difficultyLabel);
  224. label.anchor.setTo(0.5, 0.4);
  225. }
  226. }
  227. },
  228. // Calls map state
  229. func_loadMap: function () {
  230. if (audioStatus) sound.beepSound.play();
  231. mapPosition = 0; //Map position
  232. mapCanMove = true; //Move no next point
  233. levelDifficulty = this.difficulty; // difficulty for selected level (1..3 or 1..5)
  234. sublevelType = this.sublevelType; //Type of game
  235. completedLevels = 0; //reset the game progress when entering a new level
  236. game.state.start('map');
  237. },
  238. func_setLabel: function () {
  239. if (levelLabel) {
  240. levelLabel = false;
  241. this.selectionBox.frame = 0;
  242. this.labelText.text = lang.without_name + " " + lang.label_name;
  243. } else {
  244. levelLabel = true;
  245. this.selectionBox.frame = 1;
  246. this.labelText.text = lang.with_name + " " + lang.label_name;
  247. }
  248. if (audioStatus) sound.beepSound.play();
  249. }
  250. };