Improved score updates

This commit is contained in:
Arnaud Vergnet 2020-03-16 19:26:42 +01:00
parent 7f33c8376d
commit c9237cc824
2 changed files with 30 additions and 9 deletions

View file

@ -100,6 +100,7 @@ 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;
} }
} }
@ -151,7 +152,9 @@ export default class GameLogic {
this.currentObject.move(0, -y); this.currentObject.move(0, -y);
this.freezeTetromino(); this.freezeTetromino();
this.createTetromino(); this.createTetromino();
} } else
return true;
return false;
} }
tryRotateTetromino() { tryRotateTetromino() {
@ -162,7 +165,6 @@ export default class GameLogic {
onTick(callback: Function) { onTick(callback: Function) {
this.gameTime++; this.gameTime++;
this.score++;
this.tryMoveTetromino(0, 1); this.tryMoveTetromino(0, 1);
callback(this.gameTime, this.score, this.getFinalGrid()); callback(this.gameTime, this.score, this.getFinalGrid());
} }
@ -175,7 +177,7 @@ export default class GameLogic {
if (!this.canUseInput()) if (!this.canUseInput())
return; return;
this.tryMoveTetromino(1, 0); if (this.tryMoveTetromino(1, 0))
callback(this.getFinalGrid()); callback(this.getFinalGrid());
} }
@ -183,15 +185,25 @@ export default class GameLogic {
if (!this.canUseInput()) if (!this.canUseInput())
return; return;
this.tryMoveTetromino(-1, 0); if (this.tryMoveTetromino(-1, 0))
callback(this.getFinalGrid()); 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) { rotatePressed(callback: Function) {
if (!this.canUseInput()) if (!this.canUseInput())
return; return;
this.tryRotateTetromino(); if (this.tryRotateTetromino())
callback(this.getFinalGrid()); callback(this.getFinalGrid());
} }

View file

@ -27,6 +27,7 @@ class TetrisScreen extends React.Component<Props, State> {
onTick: Function; onTick: Function;
onGameEnd: Function; onGameEnd: Function;
updateGrid: Function; updateGrid: Function;
updateGridScore: Function;
constructor(props) { constructor(props) {
super(props); super(props);
@ -41,6 +42,7 @@ class TetrisScreen extends React.Component<Props, State> {
this.onTick = this.onTick.bind(this); this.onTick = this.onTick.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.props.navigation.addListener('blur', this.onScreenBlur.bind(this)); this.props.navigation.addListener('blur', this.onScreenBlur.bind(this));
this.props.navigation.addListener('focus', this.onScreenFocus.bind(this)); this.props.navigation.addListener('focus', this.onScreenFocus.bind(this));
} }
@ -93,6 +95,13 @@ class TetrisScreen extends React.Component<Props, State> {
}); });
} }
updateGridScore(newGrid: Array<Array<Object>>, score: number) {
this.setState({
grid: newGrid,
gameScore: score,
});
}
togglePause() { togglePause() {
this.logic.togglePause(); this.logic.togglePause();
if (this.logic.isGamePaused()) if (this.logic.isGamePaused())
@ -215,7 +224,7 @@ class TetrisScreen extends React.Component<Props, State> {
<IconButton <IconButton
icon="arrow-down" icon="arrow-down"
size={40} size={40}
onPress={() => this.logic.rightPressed(this.updateGrid)} onPress={() => this.logic.downPressed(this.updateGridScore)}
/> />
</View> </View>
</View> </View>