Compare commits

..

No commits in common. "7da30e0af6a78677671667e2c4d4055066c7ede8" and "879ae46abe042b5d5af0400868039b410be4591e" have entirely different histories.

6 changed files with 29 additions and 131 deletions

View file

@ -39,8 +39,6 @@ export default class GameLogic {
autoRepeatActivationDelay: number;
autoRepeatDelay: number;
nextPieces: Array<Tetromino>;
nextPiecesCount: number;
onTick: Function;
onClock: Function;
@ -57,20 +55,7 @@ export default class GameLogic {
this.gamePaused = false;
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;
this.autoRepeatDelay = 100;
}
getHeight(): number {
@ -89,9 +74,9 @@ export default class GameLogic {
return this.gamePaused;
}
getEmptyLine(width: number) {
getEmptyLine() {
let line = [];
for (let col = 0; col < width; col++) {
for (let col = 0; col < this.getWidth(); col++) {
line.push({
color: this.colors.tetrisBackground,
isEmpty: true,
@ -100,10 +85,10 @@ export default class GameLogic {
return line;
}
getEmptyGrid(height: number, width: number) {
getEmptyGrid() {
let grid = [];
for (let row = 0; row < height; row++) {
grid.push(this.getEmptyLine(width));
for (let row = 0; row < this.getHeight(); row++) {
grid.push(this.getEmptyLine());
}
return grid;
}
@ -113,7 +98,7 @@ export default class GameLogic {
}
getFinalGrid() {
let coord = this.currentObject.getCellsCoordinates(true);
let coord = this.currentObject.getCellsCoordinates();
let finalGrid = this.getGridCopy();
for (let i = 0; i < coord.length; i++) {
finalGrid[coord[i].y][coord[i].x] = {
@ -131,24 +116,17 @@ export default class GameLogic {
}
canLevelUp() {
let canLevel = this.levelProgression > this.level * 5;
if (canLevel)
this.levelProgression -= this.level * 5;
return canLevel;
}
tetrominoToGrid(object: Object, coord : Array<Object>, grid: Array<Array<Object>>) {
for (let i = 0; i < coord.length; i++) {
grid[coord[i].y][coord[i].x] = {
color: object.getColor(),
isEmpty: false,
};
}
return this.levelProgression > this.level * 5;
}
freezeTetromino() {
let coord = this.currentObject.getCellsCoordinates(true);
this.tetrominoToGrid(this.currentObject, coord, this.currentGrid);
let coord = this.currentObject.getCellsCoordinates();
for (let i = 0; i < coord.length; i++) {
this.currentGrid[coord[i].y][coord[i].x] = {
color: this.currentObject.getColor(),
isEmpty: false,
};
}
this.clearLines(this.getLinesToClear(coord));
}
@ -156,7 +134,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.getWidth()));
this.currentGrid.unshift(this.getEmptyLine());
}
switch (lines.length) {
case 1:
@ -193,7 +171,7 @@ export default class GameLogic {
isTetrominoPositionValid() {
let isValid = true;
let coord = this.currentObject.getCellsCoordinates(true);
let coord = this.currentObject.getCellsCoordinates();
for (let i = 0; i < coord.length; i++) {
if (coord[i].x >= this.getWidth()
|| coord[i].x < 0
@ -305,21 +283,10 @@ 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();
this.recoverNextPiece();
let shape = Math.floor(Math.random() * 7);
this.currentObject = new Tetromino(shape, this.colors);
if (!this.isTetrominoPositionValid())
this.endGame(false);
}
@ -355,9 +322,7 @@ export default class GameLogic {
this.level = 0;
this.levelProgression = 0;
this.gameTick = GameLogic.levelTicks[this.level];
this.currentGrid = this.getEmptyGrid(this.getHeight(), this.getWidth());
this.nextPieces = [];
this.generateNextPieces();
this.currentGrid = this.getEmptyGrid();
this.createTetromino();
tickCallback(this.score, this.level, this.getFinalGrid());
clockCallback(this.gameTime);
@ -367,4 +332,5 @@ export default class GameLogic {
this.gameTimeInterval = setInterval(this.onClock, 1000);
this.endCallback = endCallback;
}
}

View file

@ -7,7 +7,6 @@ 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,
@ -37,7 +36,7 @@ class TetrisScreen extends React.Component<Props, State> {
this.colors = props.theme.colors;
this.logic = new GameLogic(20, 10, this.colors);
this.state = {
grid: this.logic.getEmptyGrid(this.logic.getHeight(), this.logic.getWidth()),
grid: this.logic.getEmptyGrid(),
gameRunning: false,
gameTime: 0,
gameScore: 0,
@ -93,7 +92,7 @@ class TetrisScreen extends React.Component<Props, State> {
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
@ -242,19 +241,8 @@ class TetrisScreen extends React.Component<Props, State> {
<Grid
width={this.logic.getWidth()}
height={this.logic.getHeight()}
containerHeight={'80%'}
grid={this.state.grid}
backgroundColor={this.colors.tetrisBackground}
/>
<View style={{
position: 'absolute',
top: 50,
right: 5,
}}>
<Preview
next={this.logic.getNextPieces()}
/>
</View>
<View style={{
flexDirection: 'row',
width: '100%',

View file

@ -194,15 +194,12 @@ export default class Tetromino {
return Tetromino.colors[this.currentType];
}
getCellsCoordinates(isAbsolute: boolean) {
getCellsCoordinates() {
let coordinates = [];
for (let row = 0; row < this.currentShape.length; row++) {
for (let col = 0; col < this.currentShape[row].length; col++) {
if (this.currentShape[row][col] === 1)
if (isAbsolute)
coordinates.push({x: this.position.x + col, y: this.position.y + row});
else
coordinates.push({x: col, y: row});
coordinates.push({x: this.position.x + col, y: this.position.y + row});
}
}
return coordinates;

View file

@ -24,8 +24,8 @@ class Cell extends React.PureComponent<Props> {
<View
style={{
flex: 1,
backgroundColor: this.props.isEmpty ? 'transparent' : this.props.color,
borderColor: this.props.isEmpty ? 'transparent' : this.colors.tetrisBorder,
backgroundColor: this.props.color,
borderColor: this.props.isEmpty ? this.props.color : this.colors.tetrisBorder,
borderStyle: 'solid',
borderWidth: 1,
aspectRatio: 1,

View file

@ -8,10 +8,8 @@ import Cell from "./Cell";
type Props = {
navigation: Object,
grid: Array<Array<Object>>,
backgroundColor: string,
height: number,
width: number,
containerHeight: number|string,
}
class Grid extends React.Component<Props>{
@ -33,6 +31,7 @@ class Grid extends React.Component<Props>{
return(
<View style={{
flexDirection: 'row',
backgroundColor: this.colors.tetrisBackground
}}>
{cells}
</View>
@ -51,11 +50,10 @@ class Grid extends React.Component<Props>{
return (
<View style={{
flexDirection: 'column',
height: this.props.containerHeight,
height: '80%',
aspectRatio: this.props.width/this.props.height,
marginLeft: 'auto',
marginRight: 'auto',
backgroundColor: this.props.backgroundColor,
}}>
{this.getGrid()}
</View>

View file

@ -1,51 +0,0 @@
// @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<Props> {
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(
<Grid
width={this.props.next[i][0].length}
height={this.props.next[i].length}
grid={this.props.next[i]}
containerHeight={50}
backgroundColor={'transparent'}
/>
);
}
return grids;
}
render() {
if (this.props.next.length > 0) {
return (
<View>
{this.getGrids()}
</View>
);
} else
return null;
}
}
export default withTheme(Preview);