application-amicale/screens/Tetris/TetrisScreen.js

304 lines
9.1 KiB
JavaScript
Raw Normal View History

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-17 14:22:49 +01:00
import Preview from "./components/Preview";
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,
2020-03-16 20:10:54 +01:00
gameScore: number,
gameLevel: number,
2020-03-15 18:44:32 +01:00
}
class TetrisScreen extends React.Component<Props, State> {
colors: Object;
logic: GameLogic;
onTick: Function;
2020-03-16 19:40:52 +01:00
onClock: 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 = {
2020-03-17 14:22:49 +01:00
grid: this.logic.getEmptyGrid(this.logic.getHeight(), this.logic.getWidth()),
2020-03-16 19:10:32 +01:00
gameRunning: false,
2020-03-15 18:44:32 +01:00
gameTime: 0,
gameScore: 0,
2020-03-16 20:10:54 +01:00
gameLevel: 0,
2020-03-15 18:44:32 +01:00
};
this.onTick = this.onTick.bind(this);
2020-03-16 19:40:52 +01:00
this.onClock = this.onClock.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
}
2020-03-16 20:23:29 +01:00
getFormattedTime(seconds: number) {
let date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(seconds);
let format;
if (date.getHours())
2020-03-17 14:22:49 +01:00
format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
2020-03-16 20:23:29 +01:00
else if (date.getMinutes())
format = date.getMinutes() + ':' + date.getSeconds();
else
format = date.getSeconds();
return format;
}
2020-03-16 20:10:54 +01:00
onTick(score: number, level: number, newGrid: Array<Array<Object>>) {
2020-03-15 18:44:32 +01:00
this.setState({
gameScore: score,
2020-03-16 20:10:54 +01:00
gameLevel: level,
2020-03-15 18:44:32 +01:00
grid: newGrid,
});
}
2020-03-16 19:40:52 +01:00
onClock(time: number) {
this.setState({
gameTime: time,
});
}
2020-03-15 18:44:32 +01:00
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() {
let message = 'SCORE: ' + this.state.gameScore + '\n';
2020-03-16 20:10:54 +01:00
message += 'LEVEL: ' + this.state.gameLevel + '\n';
2020-03-16 20:23:29 +01:00
message += 'TIME: ' + this.getFormattedTime(this.state.gameTime) + '\n';
2020-03-16 19:10:32 +01:00
Alert.alert(
'GAME OVER',
message,
2020-03-16 19:10:32 +01:00
[
{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:40:52 +01:00
this.logic.startGame(this.onTick, this.onClock, this.onGameEnd);
2020-03-16 19:10:32 +01:00
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',
2020-03-20 21:15:37 +01:00
top: 5,
2020-03-16 19:10:32 +01:00
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
2020-03-16 20:23:29 +01:00
}}>{this.getFormattedTime(this.state.gameTime)}</Text>
2020-03-16 19:10:32 +01:00
</View>
<View style={{
flexDirection: 'row',
2020-03-16 20:10:54 +01:00
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={{
flexDirection: 'row',
2020-03-16 19:10:32 +01:00
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()}
2020-03-20 21:15:37 +01:00
containerMaxHeight={'80%'}
containerMaxWidth={'60%'}
2020-03-15 18:44:32 +01:00
grid={this.state.grid}
2020-03-17 14:22:49 +01:00
backgroundColor={this.colors.tetrisBackground}
2020-03-15 18:44:32 +01:00
/>
2020-03-17 14:22:49 +01:00
<View style={{
position: 'absolute',
top: 50,
right: 5,
}}>
<Preview
next={this.logic.getNextPieces()}
/>
</View>
2020-03-15 18:44:32 +01:00
<View style={{
2020-03-20 21:15:37 +01:00
position: 'absolute',
bottom: 0,
2020-03-16 19:10:32 +01:00
flexDirection: 'row',
2020-03-16 19:29:57 +01:00
width: '100%',
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-16 19:29:57 +01:00
style={{marginRight: 'auto'}}
2020-03-15 18:44:32 +01:00
/>
2020-03-16 19:29:57 +01:00
<View style={{
flexDirection: 'row',
}}>
<IconButton
icon="arrow-left"
size={40}
2020-03-17 00:24:57 +01:00
onPress={() => this.logic.pressedOut()}
onPressIn={() => this.logic.leftPressedIn(this.updateGrid)}
2020-03-16 19:29:57 +01:00
/>
<IconButton
icon="arrow-right"
size={40}
2020-03-17 00:24:57 +01:00
onPress={() => this.logic.pressedOut()}
onPressIn={() => this.logic.rightPressed(this.updateGrid)}
2020-03-16 19:29:57 +01:00
/>
</View>
2020-03-15 19:28:41 +01:00
<IconButton
2020-03-16 19:10:32 +01:00
icon="arrow-down"
size={40}
2020-03-17 00:24:57 +01:00
onPressIn={() => this.logic.downPressedIn(this.updateGridScore)}
onPress={() => this.logic.pressedOut()}
2020-03-16 19:29:57 +01:00
style={{marginLeft: 'auto'}}
2020-03-20 21:15:37 +01:00
color={this.colors.tetrisScore}
2020-03-16 19:10:32 +01:00
/>
2020-03-15 18:44:32 +01:00
</View>
</View>
);
}
}
export default withTheme(TetrisScreen);