Made clock use seconds not game ticks

This commit is contained in:
Arnaud Vergnet 2020-03-16 19:40:52 +01:00
parent 3affbfcb40
commit 7980b8b422
2 changed files with 26 additions and 7 deletions

View file

@ -18,8 +18,10 @@ export default class GameLogic {
gameTick: number;
gameTickInterval: IntervalID;
gameTimeInterval: IntervalID;
onTick: Function;
onClock: Function;
endCallback: Function;
colors: Object;
@ -164,9 +166,13 @@ export default class GameLogic {
}
onTick(callback: Function) {
this.gameTime++;
this.tryMoveTetromino(0, 1);
callback(this.gameTime, this.score, this.getFinalGrid());
callback(this.score, this.getFinalGrid());
}
onClock(callback: Function) {
this.gameTime++;
callback(this.gameTime);
}
canUseInput() {
@ -220,8 +226,10 @@ export default class GameLogic {
this.gamePaused = !this.gamePaused;
if (this.gamePaused) {
clearInterval(this.gameTickInterval);
clearInterval(this.gameTimeInterval);
} else {
this.gameTickInterval = setInterval(this.onTick, this.gameTick);
this.gameTimeInterval = setInterval(this.onClock, 1000);
}
}
@ -229,10 +237,11 @@ export default class GameLogic {
this.gameRunning = false;
this.gamePaused = false;
clearInterval(this.gameTickInterval);
clearInterval(this.gameTimeInterval);
this.endCallback(this.gameTime, this.score, isRestart);
}
startGame(tickCallback: Function, endCallback: Function) {
startGame(tickCallback: Function, clockCallback: Function, endCallback: Function) {
if (this.gameRunning)
this.endGame(true);
this.gameRunning = true;
@ -241,9 +250,12 @@ export default class GameLogic {
this.score = 0;
this.currentGrid = this.getEmptyGrid();
this.createTetromino();
tickCallback(this.gameTime, this.score, this.getFinalGrid());
tickCallback(this.score, this.getFinalGrid());
clockCallback(this.gameTime);
this.onTick = this.onTick.bind(this, tickCallback);
this.onClock = this.onClock.bind(this, clockCallback);
this.gameTickInterval = setInterval(this.onTick, this.gameTick);
this.gameTimeInterval = setInterval(this.onClock, 1000);
this.endCallback = endCallback;
}

View file

@ -25,6 +25,7 @@ class TetrisScreen extends React.Component<Props, State> {
logic: GameLogic;
onTick: Function;
onClock: Function;
onGameEnd: Function;
updateGrid: Function;
updateGridScore: Function;
@ -40,6 +41,7 @@ class TetrisScreen extends React.Component<Props, State> {
gameScore: 0,
};
this.onTick = this.onTick.bind(this);
this.onClock = this.onClock.bind(this);
this.onGameEnd = this.onGameEnd.bind(this);
this.updateGrid = this.updateGrid.bind(this);
this.updateGridScore = this.updateGridScore.bind(this);
@ -81,14 +83,19 @@ class TetrisScreen extends React.Component<Props, State> {
this.showPausePopup();
}
onTick(time: number, score: number, newGrid: Array<Array<Object>>) {
onTick(score: number, newGrid: Array<Array<Object>>) {
this.setState({
gameTime: time,
gameScore: score,
grid: newGrid,
});
}
onClock(time: number) {
this.setState({
gameTime: time,
});
}
updateGrid(newGrid: Array<Array<Object>>) {
this.setState({
grid: newGrid,
@ -145,7 +152,7 @@ class TetrisScreen extends React.Component<Props, State> {
}
startGame() {
this.logic.startGame(this.onTick, this.onGameEnd);
this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
this.setState({
gameRunning: true,
});