Added levels

This commit is contained in:
Arnaud Vergnet 2020-03-16 20:10:54 +01:00
parent e5bde81964
commit 07d8fb8d15
2 changed files with 63 additions and 5 deletions

View file

@ -4,6 +4,32 @@ import Tetromino from "./Tetromino";
export default class GameLogic { export default class GameLogic {
static levelTicks = {
'1': 1000,
'2': 900,
'3': 800,
'4': 700,
'5': 600,
'6': 500,
'7': 400,
'8': 300,
'9': 200,
'10': 150,
};
static levelThresholds = {
'1': 100,
'2': 300,
'3': 500,
'4': 700,
'5': 1000,
'7': 1500,
'8': 2000,
'9': 3000,
'10': 4000,
'11': 5000,
};
currentGrid: Array<Array<Object>>; currentGrid: Array<Array<Object>>;
height: number; height: number;
@ -13,6 +39,7 @@ export default class GameLogic {
gamePaused: boolean; gamePaused: boolean;
gameTime: number; gameTime: number;
score: number; score: number;
level: number;
currentObject: Tetromino; currentObject: Tetromino;
@ -31,7 +58,6 @@ export default class GameLogic {
this.width = width; this.width = width;
this.gameRunning = false; this.gameRunning = false;
this.gamePaused = false; this.gamePaused = false;
this.gameTick = 250;
this.colors = colors; this.colors = colors;
} }
@ -165,9 +191,21 @@ export default class GameLogic {
this.currentObject.rotate(false); this.currentObject.rotate(false);
} }
setNewGameTick(level: number) {
if (level > 10)
return;
this.gameTick = GameLogic.levelTicks[level];
clearInterval(this.gameTickInterval);
this.gameTickInterval = setInterval(this.onTick, this.gameTick);
}
onTick(callback: Function) { onTick(callback: Function) {
this.tryMoveTetromino(0, 1); this.tryMoveTetromino(0, 1);
callback(this.score, this.getFinalGrid()); callback(this.score, this.level, this.getFinalGrid());
if (this.level <= 10 && this.score > GameLogic.levelThresholds[this.level]) {
this.level++;
this.setNewGameTick(this.level);
}
} }
onClock(callback: Function) { onClock(callback: Function) {
@ -248,9 +286,11 @@ export default class GameLogic {
this.gamePaused = false; this.gamePaused = false;
this.gameTime = 0; this.gameTime = 0;
this.score = 0; this.score = 0;
this.level = 1;
this.gameTick = GameLogic.levelTicks[this.level];
this.currentGrid = this.getEmptyGrid(); this.currentGrid = this.getEmptyGrid();
this.createTetromino(); this.createTetromino();
tickCallback(this.score, this.getFinalGrid()); tickCallback(this.score, this.level, this.getFinalGrid());
clockCallback(this.gameTime); 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.onClock = this.onClock.bind(this, clockCallback);

View file

@ -16,7 +16,8 @@ type State = {
grid: Array<Array<Object>>, grid: Array<Array<Object>>,
gameRunning: boolean, gameRunning: boolean,
gameTime: number, gameTime: number,
gameScore: number gameScore: number,
gameLevel: number,
} }
class TetrisScreen extends React.Component<Props, State> { class TetrisScreen extends React.Component<Props, State> {
@ -39,6 +40,7 @@ class TetrisScreen extends React.Component<Props, State> {
gameRunning: false, gameRunning: false,
gameTime: 0, gameTime: 0,
gameScore: 0, gameScore: 0,
gameLevel: 0,
}; };
this.onTick = this.onTick.bind(this); this.onTick = this.onTick.bind(this);
this.onClock = this.onClock.bind(this); this.onClock = this.onClock.bind(this);
@ -83,9 +85,10 @@ class TetrisScreen extends React.Component<Props, State> {
this.showPausePopup(); this.showPausePopup();
} }
onTick(score: number, newGrid: Array<Array<Object>>) { onTick(score: number, level: number, newGrid: Array<Array<Object>>) {
this.setState({ this.setState({
gameScore: score, gameScore: score,
gameLevel: level,
grid: newGrid, grid: newGrid,
}); });
} }
@ -141,6 +144,7 @@ class TetrisScreen extends React.Component<Props, State> {
showGameOverConfirm() { showGameOverConfirm() {
let message = 'SCORE: ' + this.state.gameScore + '\n'; let message = 'SCORE: ' + this.state.gameScore + '\n';
message += 'LEVEL: ' + this.state.gameLevel + '\n';
message += 'TIME: ' + this.state.gameTime + '\n'; message += 'TIME: ' + this.state.gameTime + '\n';
Alert.alert( Alert.alert(
'GAME OVER', 'GAME OVER',
@ -191,6 +195,20 @@ class TetrisScreen extends React.Component<Props, State> {
color: this.colors.subtitle color: this.colors.subtitle
}}>{this.state.gameTime}</Text> }}>{this.state.gameTime}</Text>
</View> </View>
<View style={{
flexDirection: 'row',
position: 'absolute',
top: 50,
left: 10,
}}>
<MaterialCommunityIcons
name={'gamepad'}
color={this.colors.text}
size={20}/>
<Text style={{
marginLeft: 5
}}>{this.state.gameLevel}</Text>
</View>
<View style={{ <View style={{
flexDirection: 'row', flexDirection: 'row',
marginRight: 'auto', marginRight: 'auto',