2020-03-23 18:10:45 +01:00
|
|
|
// @flow
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Class used to manage game score
|
|
|
|
*/
|
2020-03-23 18:10:45 +01:00
|
|
|
export default class ScoreManager {
|
|
|
|
|
|
|
|
#scoreLinesModifier = [40, 100, 300, 1200];
|
|
|
|
|
|
|
|
#score: number;
|
|
|
|
#level: number;
|
|
|
|
#levelProgression: number;
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Initializes score to 0
|
|
|
|
*/
|
2020-03-23 18:10:45 +01:00
|
|
|
constructor() {
|
|
|
|
this.#score = 0;
|
|
|
|
this.#level = 0;
|
|
|
|
this.#levelProgression = 0;
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Gets the current score
|
|
|
|
*
|
|
|
|
* @return {number} The current score
|
|
|
|
*/
|
2020-03-23 18:10:45 +01:00
|
|
|
getScore(): number {
|
|
|
|
return this.#score;
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Gets the current level
|
|
|
|
*
|
|
|
|
* @return {number} The current level
|
|
|
|
*/
|
2020-03-23 18:10:45 +01:00
|
|
|
getLevel(): number {
|
|
|
|
return this.#level;
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Gets the current level progression
|
|
|
|
*
|
|
|
|
* @return {number} The current level progression
|
|
|
|
*/
|
2020-03-23 19:31:02 +01:00
|
|
|
getLevelProgression(): number {
|
|
|
|
return this.#levelProgression;
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Increments the score by one
|
|
|
|
*/
|
2020-03-23 18:10:45 +01:00
|
|
|
incrementScore() {
|
|
|
|
this.#score++;
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Add score corresponding to the number of lines removed at the same time.
|
|
|
|
* Also updates the level progression.
|
|
|
|
*
|
|
|
|
* The more lines cleared at the same time, the more points and level progression the player gets.
|
|
|
|
*
|
|
|
|
* @param numberRemoved The number of lines removed at the same time
|
|
|
|
*/
|
2020-03-23 18:10:45 +01:00
|
|
|
addLinesRemovedPoints(numberRemoved: number) {
|
|
|
|
if (numberRemoved < 1 || numberRemoved > 4)
|
2020-03-28 12:08:08 +01:00
|
|
|
return;
|
2020-03-23 18:10:45 +01:00
|
|
|
this.#score += this.#scoreLinesModifier[numberRemoved-1] * (this.#level + 1);
|
|
|
|
switch (numberRemoved) {
|
|
|
|
case 1:
|
|
|
|
this.#levelProgression += 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
this.#levelProgression += 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
this.#levelProgression += 5;
|
|
|
|
break;
|
|
|
|
case 4: // Did a tetris !
|
|
|
|
this.#levelProgression += 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:08:08 +01:00
|
|
|
/**
|
|
|
|
* Checks if the player can go to the next level.
|
|
|
|
*
|
|
|
|
* If he can, change the level.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if the current level has changed
|
|
|
|
*/
|
2020-03-23 18:10:45 +01:00
|
|
|
canLevelUp() {
|
|
|
|
let canLevel = this.#levelProgression > this.#level * 5;
|
|
|
|
if (canLevel){
|
|
|
|
this.#levelProgression -= this.#level * 5;
|
|
|
|
this.#level++;
|
|
|
|
}
|
|
|
|
return canLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|