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

View file

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