Compare commits

..

No commits in common. "e5bde81964329cf0c5b2e3a8b5190742f84a256b" and "7f33c8376d45e368a41a05f154f57082e65ebc58" have entirely different histories.

3 changed files with 38 additions and 85 deletions

View file

@ -18,10 +18,8 @@ export default class GameLogic {
gameTick: number;
gameTickInterval: IntervalID;
gameTimeInterval: IntervalID;
onTick: Function;
onClock: Function;
endCallback: Function;
colors: Object;
@ -102,7 +100,6 @@ 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;
}
}
@ -154,9 +151,7 @@ export default class GameLogic {
this.currentObject.move(0, -y);
this.freezeTetromino();
this.createTetromino();
} else
return true;
return false;
}
}
tryRotateTetromino() {
@ -166,13 +161,10 @@ export default class GameLogic {
}
onTick(callback: Function) {
this.tryMoveTetromino(0, 1);
callback(this.score, this.getFinalGrid());
}
onClock(callback: Function) {
this.gameTime++;
callback(this.gameTime);
this.score++;
this.tryMoveTetromino(0, 1);
callback(this.gameTime, this.score, this.getFinalGrid());
}
canUseInput() {
@ -183,34 +175,24 @@ export default class GameLogic {
if (!this.canUseInput())
return;
if (this.tryMoveTetromino(1, 0))
callback(this.getFinalGrid());
this.tryMoveTetromino(1, 0);
callback(this.getFinalGrid());
}
leftPressed(callback: Function) {
if (!this.canUseInput())
return;
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);
}
this.tryMoveTetromino(-1, 0);
callback(this.getFinalGrid());
}
rotatePressed(callback: Function) {
if (!this.canUseInput())
return;
if (this.tryRotateTetromino())
callback(this.getFinalGrid());
this.tryRotateTetromino();
callback(this.getFinalGrid());
}
createTetromino() {
@ -226,10 +208,8 @@ 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);
}
}
@ -237,11 +217,10 @@ 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, clockCallback: Function, endCallback: Function) {
startGame(tickCallback: Function, endCallback: Function) {
if (this.gameRunning)
this.endGame(true);
this.gameRunning = true;
@ -250,12 +229,9 @@ export default class GameLogic {
this.score = 0;
this.currentGrid = this.getEmptyGrid();
this.createTetromino();
tickCallback(this.score, this.getFinalGrid());
clockCallback(this.gameTime);
tickCallback(this.gameTime, this.score, this.getFinalGrid());
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;
}

View file

@ -25,10 +25,8 @@ class TetrisScreen extends React.Component<Props, State> {
logic: GameLogic;
onTick: Function;
onClock: Function;
onGameEnd: Function;
updateGrid: Function;
updateGridScore: Function;
constructor(props) {
super(props);
@ -41,10 +39,8 @@ class TetrisScreen extends React.Component<Props, State> {
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);
this.props.navigation.addListener('blur', this.onScreenBlur.bind(this));
this.props.navigation.addListener('focus', this.onScreenFocus.bind(this));
}
@ -83,16 +79,11 @@ class TetrisScreen extends React.Component<Props, State> {
this.showPausePopup();
}
onTick(score: number, newGrid: Array<Array<Object>>) {
this.setState({
gameScore: score,
grid: newGrid,
});
}
onClock(time: number) {
onTick(time: number, score: number, newGrid: Array<Array<Object>>) {
this.setState({
gameTime: time,
gameScore: score,
grid: newGrid,
});
}
@ -102,13 +93,6 @@ class TetrisScreen extends React.Component<Props, State> {
});
}
updateGridScore(newGrid: Array<Array<Object>>, score: number) {
this.setState({
grid: newGrid,
gameScore: score,
});
}
togglePause() {
this.logic.togglePause();
if (this.logic.isGamePaused())
@ -140,11 +124,9 @@ class TetrisScreen extends React.Component<Props, State> {
}
showGameOverConfirm() {
let message = 'SCORE: ' + this.state.gameScore + '\n';
message += 'TIME: ' + this.state.gameTime + '\n';
Alert.alert(
'GAME OVER',
message,
'NOOB',
[
{text: 'LEAVE', onPress: () => this.props.navigation.goBack()},
{text: 'RESTART', onPress: () => this.startGame()},
@ -154,7 +136,7 @@ class TetrisScreen extends React.Component<Props, State> {
}
startGame() {
this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
this.logic.startGame(this.onTick, this.onGameEnd);
this.setState({
gameRunning: true,
});
@ -212,33 +194,28 @@ class TetrisScreen extends React.Component<Props, State> {
/>
<View style={{
flexDirection: 'row',
width: '100%',
marginLeft: 'auto',
marginRight: 'auto',
}}>
<IconButton
icon="format-rotate-90"
size={40}
onPress={() => this.logic.rotatePressed(this.updateGrid)}
style={{marginRight: 'auto'}}
/>
<View style={{
flexDirection: 'row',
}}>
<IconButton
icon="arrow-left"
size={40}
onPress={() => this.logic.leftPressed(this.updateGrid)}
/>
<IconButton
icon="arrow-right"
size={40}
onPress={() => this.logic.rightPressed(this.updateGrid)}
/>
</View>
<IconButton
icon="arrow-left"
size={40}
onPress={() => this.logic.leftPressed(this.updateGrid)}
/>
<IconButton
icon="arrow-right"
size={40}
onPress={() => this.logic.rightPressed(this.updateGrid)}
/>
<IconButton
icon="arrow-down"
size={40}
onPress={() => this.logic.downPressed(this.updateGridScore)}
style={{marginLeft: 'auto'}}
onPress={() => this.logic.rightPressed(this.updateGrid)}
/>
</View>
</View>

View file

@ -56,16 +56,16 @@ export default class ThemeManager {
tutorinsaColor: '#f93943',
// Tetris
tetrisBackground:'#e6e6e6',
tetrisBorder:'#2f2f2f',
tetrisScore:'#e2bd33',
tetrisI : '#3cd9e6',
tetrisBackground:'#d1d1d1',
tetrisBorder:'#afafaf',
tetrisScore:'#fff307',
tetrisI : '#42f1ff',
tetrisO : '#ffdd00',
tetrisT : '#a716e5',
tetrisS : '#09c528',
tetrisT : '#ba19ff',
tetrisS : '#0bff34',
tetrisZ : '#ff0009',
tetrisJ : '#2a67e3',
tetrisL : '#da742d',
tetrisJ : '#134cff',
tetrisL : '#ff8834',
},
};
}