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

View file

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

View file

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