Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScoreManager.ts 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * Class used to manage game score
  21. */
  22. export default class ScoreManager {
  23. #scoreLinesModifier = [40, 100, 300, 1200];
  24. #score: number;
  25. #level: number;
  26. #levelProgression: number;
  27. /**
  28. * Initializes score to 0
  29. */
  30. constructor() {
  31. this.#score = 0;
  32. this.#level = 0;
  33. this.#levelProgression = 0;
  34. }
  35. /**
  36. * Gets the current score
  37. *
  38. * @return {number} The current score
  39. */
  40. getScore(): number {
  41. return this.#score;
  42. }
  43. /**
  44. * Gets the current level
  45. *
  46. * @return {number} The current level
  47. */
  48. getLevel(): number {
  49. return this.#level;
  50. }
  51. /**
  52. * Gets the current level progression
  53. *
  54. * @return {number} The current level progression
  55. */
  56. getLevelProgression(): number {
  57. return this.#levelProgression;
  58. }
  59. /**
  60. * Increments the score by one
  61. */
  62. incrementScore() {
  63. this.#score += 1;
  64. }
  65. /**
  66. * Add score corresponding to the number of lines removed at the same time.
  67. * Also updates the level progression.
  68. *
  69. * The more lines cleared at the same time, the more points and level progression the player gets.
  70. *
  71. * @param numberRemoved The number of lines removed at the same time
  72. */
  73. addLinesRemovedPoints(numberRemoved: number) {
  74. if (numberRemoved < 1 || numberRemoved > 4) {
  75. return;
  76. }
  77. this.#score +=
  78. this.#scoreLinesModifier[numberRemoved - 1] * (this.#level + 1);
  79. switch (numberRemoved) {
  80. case 1:
  81. this.#levelProgression += 1;
  82. break;
  83. case 2:
  84. this.#levelProgression += 3;
  85. break;
  86. case 3:
  87. this.#levelProgression += 5;
  88. break;
  89. case 4: // Did a tetris !
  90. this.#levelProgression += 8;
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. /**
  97. * Checks if the player can go to the next level.
  98. *
  99. * If he can, change the level.
  100. *
  101. * @return {boolean} True if the current level has changed
  102. */
  103. canLevelUp(): boolean {
  104. const canLevel = this.#levelProgression > this.#level * 5;
  105. if (canLevel) {
  106. this.#levelProgression -= this.#level * 5;
  107. this.#level += 1;
  108. }
  109. return canLevel;
  110. }
  111. }