2020-03-15 18:44:32 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2020-03-16 19:10:32 +01:00
|
|
|
import {Alert, View} from 'react-native';
|
2020-03-15 18:44:32 +01:00
|
|
|
import {IconButton, Text, withTheme} from 'react-native-paper';
|
2020-03-16 19:10:32 +01:00
|
|
|
import {MaterialCommunityIcons} from "@expo/vector-icons";
|
2020-03-15 18:44:32 +01:00
|
|
|
import GameLogic from "./GameLogic";
|
|
|
|
import Grid from "./components/Grid";
|
2020-03-16 19:10:32 +01:00
|
|
|
import HeaderButton from "../../components/HeaderButton";
|
2020-03-15 18:44:32 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
grid: Array<Array<Object>>,
|
2020-03-16 19:10:32 +01:00
|
|
|
gameRunning: boolean,
|
2020-03-15 18:44:32 +01:00
|
|
|
gameTime: number,
|
|
|
|
gameScore: number
|
|
|
|
}
|
|
|
|
|
|
|
|
class TetrisScreen extends React.Component<Props, State> {
|
|
|
|
|
|
|
|
colors: Object;
|
|
|
|
|
|
|
|
logic: GameLogic;
|
|
|
|
onTick: Function;
|
2020-03-15 19:28:41 +01:00
|
|
|
onGameEnd: Function;
|
2020-03-15 18:44:32 +01:00
|
|
|
updateGrid: Function;
|
2020-03-16 19:26:42 +01:00
|
|
|
updateGridScore: Function;
|
2020-03-15 18:44:32 +01:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.colors = props.theme.colors;
|
2020-03-15 20:34:20 +01:00
|
|
|
this.logic = new GameLogic(20, 10, this.colors);
|
2020-03-15 18:44:32 +01:00
|
|
|
this.state = {
|
|
|
|
grid: this.logic.getEmptyGrid(),
|
2020-03-16 19:10:32 +01:00
|
|
|
gameRunning: false,
|
2020-03-15 18:44:32 +01:00
|
|
|
gameTime: 0,
|
|
|
|
gameScore: 0,
|
|
|
|
};
|
|
|
|
this.onTick = this.onTick.bind(this);
|
2020-03-15 19:28:41 +01:00
|
|
|
this.onGameEnd = this.onGameEnd.bind(this);
|
2020-03-15 18:44:32 +01:00
|
|
|
this.updateGrid = this.updateGrid.bind(this);
|
2020-03-16 19:26:42 +01:00
|
|
|
this.updateGridScore = this.updateGridScore.bind(this);
|
2020-03-16 19:10:32 +01:00
|
|
|
this.props.navigation.addListener('blur', this.onScreenBlur.bind(this));
|
|
|
|
this.props.navigation.addListener('focus', this.onScreenFocus.bind(this));
|
2020-03-15 18:44:32 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 19:10:32 +01:00
|
|
|
componentDidMount() {
|
|
|
|
const rightButton = this.getRightButton.bind(this);
|
|
|
|
this.props.navigation.setOptions({
|
|
|
|
headerRight: rightButton,
|
|
|
|
});
|
|
|
|
this.startGame();
|
|
|
|
}
|
|
|
|
|
|
|
|
getRightButton() {
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
}}>
|
|
|
|
<HeaderButton icon={'pause'} onPress={() => this.togglePause()}/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2020-03-15 18:44:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any interval on un-focus
|
|
|
|
*/
|
|
|
|
onScreenBlur() {
|
2020-03-16 19:10:32 +01:00
|
|
|
if (!this.logic.isGamePaused())
|
|
|
|
this.logic.togglePause();
|
|
|
|
}
|
|
|
|
|
|
|
|
onScreenFocus() {
|
|
|
|
if (!this.logic.isGameRunning())
|
|
|
|
this.startGame();
|
|
|
|
else if (this.logic.isGamePaused())
|
|
|
|
this.showPausePopup();
|
2020-03-15 18:44:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onTick(time: number, score: number, newGrid: Array<Array<Object>>) {
|
|
|
|
this.setState({
|
|
|
|
gameTime: time,
|
|
|
|
gameScore: score,
|
|
|
|
grid: newGrid,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateGrid(newGrid: Array<Array<Object>>) {
|
|
|
|
this.setState({
|
|
|
|
grid: newGrid,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:26:42 +01:00
|
|
|
updateGridScore(newGrid: Array<Array<Object>>, score: number) {
|
|
|
|
this.setState({
|
|
|
|
grid: newGrid,
|
|
|
|
gameScore: score,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:10:32 +01:00
|
|
|
togglePause() {
|
|
|
|
this.logic.togglePause();
|
|
|
|
if (this.logic.isGamePaused())
|
|
|
|
this.showPausePopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
showPausePopup() {
|
|
|
|
Alert.alert(
|
|
|
|
'PAUSE',
|
|
|
|
'GAME PAUSED',
|
|
|
|
[
|
|
|
|
{text: 'RESTART', onPress: () => this.showRestartConfirm()},
|
|
|
|
{text: 'RESUME', onPress: () => this.togglePause()},
|
|
|
|
],
|
|
|
|
{cancelable: false},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
showRestartConfirm() {
|
|
|
|
Alert.alert(
|
|
|
|
'RESTART?',
|
|
|
|
'WHOA THERE',
|
|
|
|
[
|
|
|
|
{text: 'NO', onPress: () => this.showPausePopup()},
|
|
|
|
{text: 'YES', onPress: () => this.startGame()},
|
|
|
|
],
|
|
|
|
{cancelable: false},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
showGameOverConfirm() {
|
|
|
|
Alert.alert(
|
|
|
|
'GAME OVER',
|
|
|
|
'NOOB',
|
|
|
|
[
|
|
|
|
{text: 'LEAVE', onPress: () => this.props.navigation.goBack()},
|
|
|
|
{text: 'RESTART', onPress: () => this.startGame()},
|
|
|
|
],
|
|
|
|
{cancelable: false},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-15 18:44:32 +01:00
|
|
|
startGame() {
|
2020-03-16 19:10:32 +01:00
|
|
|
this.logic.startGame(this.onTick, this.onGameEnd);
|
|
|
|
this.setState({
|
|
|
|
gameRunning: true,
|
|
|
|
});
|
2020-03-15 18:44:32 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 19:10:32 +01:00
|
|
|
onGameEnd(time: number, score: number, isRestart: boolean) {
|
2020-03-15 19:28:41 +01:00
|
|
|
this.setState({
|
|
|
|
gameTime: time,
|
|
|
|
gameScore: score,
|
2020-03-16 19:10:32 +01:00
|
|
|
gameRunning: false,
|
|
|
|
});
|
|
|
|
if (!isRestart)
|
|
|
|
this.showGameOverConfirm();
|
2020-03-15 19:28:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 18:44:32 +01:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View style={{
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
}}>
|
2020-03-16 19:10:32 +01:00
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
position: 'absolute',
|
|
|
|
top: 10,
|
|
|
|
left: 10,
|
2020-03-15 18:44:32 +01:00
|
|
|
}}>
|
2020-03-16 19:10:32 +01:00
|
|
|
<MaterialCommunityIcons
|
|
|
|
name={'timer'}
|
|
|
|
color={this.colors.subtitle}
|
|
|
|
size={20}/>
|
|
|
|
<Text style={{
|
|
|
|
marginLeft: 5,
|
|
|
|
color: this.colors.subtitle
|
|
|
|
}}>{this.state.gameTime}</Text>
|
|
|
|
</View>
|
|
|
|
<View style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
marginRight: 'auto',
|
|
|
|
marginLeft: 'auto',
|
2020-03-15 18:44:32 +01:00
|
|
|
}}>
|
2020-03-16 19:10:32 +01:00
|
|
|
<MaterialCommunityIcons
|
|
|
|
name={'star'}
|
|
|
|
color={this.colors.tetrisScore}
|
|
|
|
size={30}/>
|
|
|
|
<Text style={{
|
|
|
|
marginLeft: 5,
|
|
|
|
fontSize: 22,
|
|
|
|
}}>{this.state.gameScore}</Text>
|
|
|
|
</View>
|
2020-03-15 18:44:32 +01:00
|
|
|
<Grid
|
|
|
|
width={this.logic.getWidth()}
|
|
|
|
height={this.logic.getHeight()}
|
|
|
|
grid={this.state.grid}
|
|
|
|
/>
|
|
|
|
<View style={{
|
2020-03-16 19:10:32 +01:00
|
|
|
flexDirection: 'row',
|
|
|
|
marginLeft: 'auto',
|
|
|
|
marginRight: 'auto',
|
2020-03-15 18:44:32 +01:00
|
|
|
}}>
|
|
|
|
<IconButton
|
2020-03-16 19:10:32 +01:00
|
|
|
icon="format-rotate-90"
|
2020-03-15 18:44:32 +01:00
|
|
|
size={40}
|
2020-03-16 19:10:32 +01:00
|
|
|
onPress={() => this.logic.rotatePressed(this.updateGrid)}
|
2020-03-15 18:44:32 +01:00
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
icon="arrow-left"
|
|
|
|
size={40}
|
|
|
|
onPress={() => this.logic.leftPressed(this.updateGrid)}
|
|
|
|
/>
|
|
|
|
<IconButton
|
2020-03-16 19:10:32 +01:00
|
|
|
icon="arrow-right"
|
2020-03-15 18:44:32 +01:00
|
|
|
size={40}
|
2020-03-16 19:10:32 +01:00
|
|
|
onPress={() => this.logic.rightPressed(this.updateGrid)}
|
2020-03-15 18:44:32 +01:00
|
|
|
/>
|
2020-03-15 19:28:41 +01:00
|
|
|
<IconButton
|
2020-03-16 19:10:32 +01:00
|
|
|
icon="arrow-down"
|
|
|
|
size={40}
|
2020-03-16 19:26:42 +01:00
|
|
|
onPress={() => this.logic.downPressed(this.updateGridScore)}
|
2020-03-16 19:10:32 +01:00
|
|
|
/>
|
2020-03-15 18:44:32 +01:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(TetrisScreen);
|