From c9237cc8243566e3650d7744dad26dd9d58baef1 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Mon, 16 Mar 2020 19:26:42 +0100 Subject: [PATCH] Improved score updates --- screens/Tetris/GameLogic.js | 28 ++++++++++++++++++++-------- screens/Tetris/TetrisScreen.js | 11 ++++++++++- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/screens/Tetris/GameLogic.js b/screens/Tetris/GameLogic.js index 4863e01..5d526e3 100644 --- a/screens/Tetris/GameLogic.js +++ b/screens/Tetris/GameLogic.js @@ -100,6 +100,7 @@ export default class GameLogic { for (let i = 0; i < lines.length; i++) { this.currentGrid.splice(lines[i], 1); this.currentGrid.unshift(this.getEmptyLine()); + this.score += 100; } } @@ -151,7 +152,9 @@ export default class GameLogic { this.currentObject.move(0, -y); this.freezeTetromino(); this.createTetromino(); - } + } else + return true; + return false; } tryRotateTetromino() { @@ -162,7 +165,6 @@ export default class GameLogic { onTick(callback: Function) { this.gameTime++; - this.score++; this.tryMoveTetromino(0, 1); callback(this.gameTime, this.score, this.getFinalGrid()); } @@ -175,24 +177,34 @@ export default class GameLogic { if (!this.canUseInput()) return; - this.tryMoveTetromino(1, 0); - callback(this.getFinalGrid()); + if (this.tryMoveTetromino(1, 0)) + callback(this.getFinalGrid()); } leftPressed(callback: Function) { if (!this.canUseInput()) return; - this.tryMoveTetromino(-1, 0); - callback(this.getFinalGrid()); + if (this.tryMoveTetromino(-1, 0)) + callback(this.getFinalGrid()); + } + + downPressed(callback: Function) { + if (!this.canUseInput()) + return; + + if (this.tryMoveTetromino(0, 1)){ + this.score++; + callback(this.getFinalGrid(), this.score); + } } rotatePressed(callback: Function) { if (!this.canUseInput()) return; - this.tryRotateTetromino(); - callback(this.getFinalGrid()); + if (this.tryRotateTetromino()) + callback(this.getFinalGrid()); } createTetromino() { diff --git a/screens/Tetris/TetrisScreen.js b/screens/Tetris/TetrisScreen.js index f9675de..c4ac291 100644 --- a/screens/Tetris/TetrisScreen.js +++ b/screens/Tetris/TetrisScreen.js @@ -27,6 +27,7 @@ class TetrisScreen extends React.Component { onTick: Function; onGameEnd: Function; updateGrid: Function; + updateGridScore: Function; constructor(props) { super(props); @@ -41,6 +42,7 @@ class TetrisScreen extends React.Component { this.onTick = this.onTick.bind(this); this.onGameEnd = this.onGameEnd.bind(this); this.updateGrid = this.updateGrid.bind(this); + this.updateGridScore = this.updateGridScore.bind(this); this.props.navigation.addListener('blur', this.onScreenBlur.bind(this)); this.props.navigation.addListener('focus', this.onScreenFocus.bind(this)); } @@ -93,6 +95,13 @@ class TetrisScreen extends React.Component { }); } + updateGridScore(newGrid: Array>, score: number) { + this.setState({ + grid: newGrid, + gameScore: score, + }); + } + togglePause() { this.logic.togglePause(); if (this.logic.isGamePaused()) @@ -215,7 +224,7 @@ class TetrisScreen extends React.Component { this.logic.rightPressed(this.updateGrid)} + onPress={() => this.logic.downPressed(this.updateGridScore)} />