From 7da30e0af6a78677671667e2c4d4055066c7ede8 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Tue, 17 Mar 2020 14:22:49 +0100 Subject: [PATCH] Added previews --- screens/Tetris/GameLogic.js | 63 +++++++++++++++++++++------- screens/Tetris/TetrisScreen.js | 16 ++++++- screens/Tetris/Tetromino.js | 7 +++- screens/Tetris/components/Cell.js | 4 +- screens/Tetris/components/Grid.js | 6 ++- screens/Tetris/components/Preview.js | 51 ++++++++++++++++++++++ 6 files changed, 123 insertions(+), 24 deletions(-) create mode 100644 screens/Tetris/components/Preview.js diff --git a/screens/Tetris/GameLogic.js b/screens/Tetris/GameLogic.js index b94b4e7..7682e29 100644 --- a/screens/Tetris/GameLogic.js +++ b/screens/Tetris/GameLogic.js @@ -39,6 +39,8 @@ export default class GameLogic { autoRepeatActivationDelay: number; autoRepeatDelay: number; + nextPieces: Array; + nextPiecesCount: number; onTick: Function; onClock: Function; @@ -56,6 +58,19 @@ export default class GameLogic { this.colors = colors; this.autoRepeatActivationDelay = 300; this.autoRepeatDelay = 50; + this.nextPieces = []; + this.nextPiecesCount = 3; + } + + getNextPieces() { + let finalArray = []; + for (let i = 0; i < this.nextPieces.length; i++) { + finalArray.push(this.getEmptyGrid(4, 4)); + let coord = this.nextPieces[i].getCellsCoordinates(false); + this.tetrominoToGrid(this.nextPieces[i], coord, finalArray[i]); + } + + return finalArray; } getHeight(): number { @@ -74,9 +89,9 @@ export default class GameLogic { return this.gamePaused; } - getEmptyLine() { + getEmptyLine(width: number) { let line = []; - for (let col = 0; col < this.getWidth(); col++) { + for (let col = 0; col < width; col++) { line.push({ color: this.colors.tetrisBackground, isEmpty: true, @@ -85,10 +100,10 @@ export default class GameLogic { return line; } - getEmptyGrid() { + getEmptyGrid(height: number, width: number) { let grid = []; - for (let row = 0; row < this.getHeight(); row++) { - grid.push(this.getEmptyLine()); + for (let row = 0; row < height; row++) { + grid.push(this.getEmptyLine(width)); } return grid; } @@ -98,7 +113,7 @@ export default class GameLogic { } getFinalGrid() { - let coord = this.currentObject.getCellsCoordinates(); + let coord = this.currentObject.getCellsCoordinates(true); let finalGrid = this.getGridCopy(); for (let i = 0; i < coord.length; i++) { finalGrid[coord[i].y][coord[i].x] = { @@ -122,14 +137,18 @@ export default class GameLogic { return canLevel; } - freezeTetromino() { - let coord = this.currentObject.getCellsCoordinates(); + tetrominoToGrid(object: Object, coord : Array, grid: Array>) { for (let i = 0; i < coord.length; i++) { - this.currentGrid[coord[i].y][coord[i].x] = { - color: this.currentObject.getColor(), + grid[coord[i].y][coord[i].x] = { + color: object.getColor(), isEmpty: false, }; } + } + + freezeTetromino() { + let coord = this.currentObject.getCellsCoordinates(true); + this.tetrominoToGrid(this.currentObject, coord, this.currentGrid); this.clearLines(this.getLinesToClear(coord)); } @@ -137,7 +156,7 @@ export default class GameLogic { lines.sort(); for (let i = 0; i < lines.length; i++) { this.currentGrid.splice(lines[i], 1); - this.currentGrid.unshift(this.getEmptyLine()); + this.currentGrid.unshift(this.getEmptyLine(this.getWidth())); } switch (lines.length) { case 1: @@ -174,7 +193,7 @@ export default class GameLogic { isTetrominoPositionValid() { let isValid = true; - let coord = this.currentObject.getCellsCoordinates(); + let coord = this.currentObject.getCellsCoordinates(true); for (let i = 0; i < coord.length; i++) { if (coord[i].x >= this.getWidth() || coord[i].x < 0 @@ -286,10 +305,21 @@ export default class GameLogic { callback(this.getFinalGrid()); } + recoverNextPiece() { + this.currentObject = this.nextPieces.shift(); + this.generateNextPieces(); + } + + generateNextPieces() { + while (this.nextPieces.length < this.nextPiecesCount) { + let shape = Math.floor(Math.random() * 7); + this.nextPieces.push(new Tetromino(shape, this.colors)); + } + } + createTetromino() { this.pressedOut(); - let shape = Math.floor(Math.random() * 7); - this.currentObject = new Tetromino(shape, this.colors); + this.recoverNextPiece(); if (!this.isTetrominoPositionValid()) this.endGame(false); } @@ -325,7 +355,9 @@ export default class GameLogic { this.level = 0; this.levelProgression = 0; this.gameTick = GameLogic.levelTicks[this.level]; - this.currentGrid = this.getEmptyGrid(); + this.currentGrid = this.getEmptyGrid(this.getHeight(), this.getWidth()); + this.nextPieces = []; + this.generateNextPieces(); this.createTetromino(); tickCallback(this.score, this.level, this.getFinalGrid()); clockCallback(this.gameTime); @@ -335,5 +367,4 @@ export default class GameLogic { this.gameTimeInterval = setInterval(this.onClock, 1000); this.endCallback = endCallback; } - } diff --git a/screens/Tetris/TetrisScreen.js b/screens/Tetris/TetrisScreen.js index ceb3ac6..769fa60 100644 --- a/screens/Tetris/TetrisScreen.js +++ b/screens/Tetris/TetrisScreen.js @@ -7,6 +7,7 @@ import {MaterialCommunityIcons} from "@expo/vector-icons"; import GameLogic from "./GameLogic"; import Grid from "./components/Grid"; import HeaderButton from "../../components/HeaderButton"; +import Preview from "./components/Preview"; type Props = { navigation: Object, @@ -36,7 +37,7 @@ class TetrisScreen extends React.Component { this.colors = props.theme.colors; this.logic = new GameLogic(20, 10, this.colors); this.state = { - grid: this.logic.getEmptyGrid(), + grid: this.logic.getEmptyGrid(this.logic.getHeight(), this.logic.getWidth()), gameRunning: false, gameTime: 0, gameScore: 0, @@ -92,7 +93,7 @@ class TetrisScreen extends React.Component { date.setSeconds(seconds); let format; if (date.getHours()) - format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds(); + format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds(); else if (date.getMinutes()) format = date.getMinutes() + ':' + date.getSeconds(); else @@ -241,8 +242,19 @@ class TetrisScreen extends React.Component { + + + { >, + backgroundColor: string, height: number, width: number, + containerHeight: number|string, } class Grid extends React.Component{ @@ -31,7 +33,6 @@ class Grid extends React.Component{ return( {cells} @@ -50,10 +51,11 @@ class Grid extends React.Component{ return ( {this.getGrid()} diff --git a/screens/Tetris/components/Preview.js b/screens/Tetris/components/Preview.js new file mode 100644 index 0000000..66ff1bc --- /dev/null +++ b/screens/Tetris/components/Preview.js @@ -0,0 +1,51 @@ +// @flow + +import * as React from 'react'; +import {View} from 'react-native'; +import {withTheme} from 'react-native-paper'; +import Grid from "./Grid"; + +type Props = { + next: Object, +} + +class Preview extends React.PureComponent { + + colors: Object; + + constructor(props) { + super(props); + this.colors = props.theme.colors; + } + + getGrids() { + let grids = []; + for (let i = 0; i < this.props.next.length; i++) { + grids.push( + + ); + } + return grids; + } + + render() { + if (this.props.next.length > 0) { + return ( + + {this.getGrids()} + + ); + } else + return null; + } + + +} + +export default withTheme(Preview);