Improved score management

This commit is contained in:
Arnaud Vergnet 2020-03-17 11:00:45 +01:00
parent dbe03a2a2d
commit 879ae46abe

View file

@ -4,31 +4,18 @@ import Tetromino from "./Tetromino";
export default class GameLogic { export default class GameLogic {
static levelTicks = { static levelTicks = [
'1': 1000, 1000,
'2': 900, 800,
'3': 800, 600,
'4': 700, 400,
'5': 600, 300,
'6': 500, 200,
'7': 400, 150,
'8': 300, 100,
'9': 200, ];
'10': 150,
};
static levelThresholds = { static scoreLinesModifier = [40, 100, 300, 1200];
'1': 100,
'2': 300,
'3': 500,
'4': 700,
'5': 1000,
'6': 1500,
'7': 2000,
'8': 3000,
'9': 4000,
'10': 5000,
};
currentGrid: Array<Array<Object>>; currentGrid: Array<Array<Object>>;
@ -59,6 +46,8 @@ export default class GameLogic {
colors: Object; colors: Object;
levelProgression: number;
constructor(height: number, width: number, colors: Object) { constructor(height: number, width: number, colors: Object) {
this.height = height; this.height = height;
this.width = width; this.width = width;
@ -120,6 +109,16 @@ export default class GameLogic {
return finalGrid; return finalGrid;
} }
getLinesRemovedPoints(numberRemoved: number) {
if (numberRemoved < 1 || numberRemoved > 4)
return 0;
return GameLogic.scoreLinesModifier[numberRemoved-1] * (this.level + 1);
}
canLevelUp() {
return this.levelProgression > this.level * 5;
}
freezeTetromino() { freezeTetromino() {
let coord = this.currentObject.getCellsCoordinates(); let coord = this.currentObject.getCellsCoordinates();
for (let i = 0; i < coord.length; i++) { for (let i = 0; i < coord.length; i++) {
@ -136,8 +135,22 @@ export default class GameLogic {
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {
this.currentGrid.splice(lines[i], 1); this.currentGrid.splice(lines[i], 1);
this.currentGrid.unshift(this.getEmptyLine()); this.currentGrid.unshift(this.getEmptyLine());
this.score += 100;
} }
switch (lines.length) {
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;
}
this.score += this.getLinesRemovedPoints(lines.length);
} }
getLinesToClear(coord: Object) { getLinesToClear(coord: Object) {
@ -203,7 +216,7 @@ export default class GameLogic {
} }
setNewGameTick(level: number) { setNewGameTick(level: number) {
if (level > 10) if (level >= GameLogic.levelTicks.length)
return; return;
this.gameTick = GameLogic.levelTicks[level]; this.gameTick = GameLogic.levelTicks[level];
clearInterval(this.gameTickInterval); clearInterval(this.gameTickInterval);
@ -213,7 +226,7 @@ export default class GameLogic {
onTick(callback: Function) { onTick(callback: Function) {
this.tryMoveTetromino(0, 1); this.tryMoveTetromino(0, 1);
callback(this.score, this.level, this.getFinalGrid()); callback(this.score, this.level, this.getFinalGrid());
if (this.level <= 10 && this.score > GameLogic.levelThresholds[this.level]) { if (this.canLevelUp()) {
this.level++; this.level++;
this.setNewGameTick(this.level); this.setNewGameTick(this.level);
} }
@ -306,7 +319,8 @@ export default class GameLogic {
this.gamePaused = false; this.gamePaused = false;
this.gameTime = 0; this.gameTime = 0;
this.score = 0; this.score = 0;
this.level = 1; this.level = 0;
this.levelProgression = 0;
this.gameTick = GameLogic.levelTicks[this.level]; this.gameTick = GameLogic.levelTicks[this.level];
this.currentGrid = this.getEmptyGrid(); this.currentGrid = this.getEmptyGrid();
this.createTetromino(); this.createTetromino();