// @flow import * as React from 'react'; import {View} from 'react-native'; import {IconButton, Text, withTheme} from 'react-native-paper'; import GameLogic from "./GameLogic"; import Grid from "./components/Grid"; type Props = { navigation: Object, } type State = { grid: Array>, gameTime: number, gameScore: number } class TetrisScreen extends React.Component { colors: Object; logic: GameLogic; onTick: Function; updateGrid: Function; constructor(props) { super(props); this.colors = props.theme.colors; this.logic = new GameLogic(20, 10); this.state = { grid: this.logic.getEmptyGrid(), gameTime: 0, gameScore: 0, }; this.onTick = this.onTick.bind(this); this.updateGrid = this.updateGrid.bind(this); const onScreenBlur = this.onScreenBlur.bind(this); this.props.navigation.addListener('blur', onScreenBlur); } /** * Remove any interval on un-focus */ onScreenBlur() { this.logic.endGame(); } onTick(time: number, score: number, newGrid: Array>) { this.setState({ gameTime: time, gameScore: score, grid: newGrid, }); } updateGrid(newGrid: Array>) { this.setState({ grid: newGrid, }); } startGame() { if (!this.logic.isGameRunning()) { this.logic.startGame(this.onTick); } } render() { return ( Score: {this.state.gameScore} time: {this.state.gameTime} this.logic.rightPressed(this.updateGrid)} /> this.logic.leftPressed(this.updateGrid)} /> this.startGame()} /> ); } } export default withTheme(TetrisScreen);