diff --git a/screens/Tetris/GameLogic.js b/screens/Tetris/GameLogic.js index 5d526e3..d8a6709 100644 --- a/screens/Tetris/GameLogic.js +++ b/screens/Tetris/GameLogic.js @@ -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; } diff --git a/screens/Tetris/TetrisScreen.js b/screens/Tetris/TetrisScreen.js index ec9af16..14535bc 100644 --- a/screens/Tetris/TetrisScreen.js +++ b/screens/Tetris/TetrisScreen.js @@ -25,6 +25,7 @@ class TetrisScreen extends React.Component { logic: GameLogic; onTick: Function; + onClock: Function; onGameEnd: Function; updateGrid: Function; updateGridScore: Function; @@ -40,6 +41,7 @@ class TetrisScreen extends React.Component { 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 { this.showPausePopup(); } - onTick(time: number, score: number, newGrid: Array>) { + onTick(score: number, newGrid: Array>) { this.setState({ - gameTime: time, gameScore: score, grid: newGrid, }); } + onClock(time: number) { + this.setState({ + gameTime: time, + }); + } + updateGrid(newGrid: Array>) { this.setState({ grid: newGrid, @@ -145,7 +152,7 @@ class TetrisScreen extends React.Component { } startGame() { - this.logic.startGame(this.onTick, this.onGameEnd); + this.logic.startGame(this.onTick, this.onClock, this.onGameEnd); this.setState({ gameRunning: true, });