From 79e72784d18c3c154233acbf50ade72de9478d0c Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Mon, 16 Mar 2020 23:36:01 +0100 Subject: [PATCH] Improved piece rotation --- screens/Tetris/GameLogic.js | 5 +- screens/Tetris/TetrisScreen.js | 1 - screens/Tetris/Tetromino.js | 197 ++++++++++++++++++++++++++------- 3 files changed, 158 insertions(+), 45 deletions(-) diff --git a/screens/Tetris/GameLogic.js b/screens/Tetris/GameLogic.js index 6887e12..8aed8fb 100644 --- a/screens/Tetris/GameLogic.js +++ b/screens/Tetris/GameLogic.js @@ -187,8 +187,11 @@ export default class GameLogic { tryRotateTetromino() { this.currentObject.rotate(true); - if (!this.isTetrominoPositionValid()) + if (!this.isTetrominoPositionValid()){ this.currentObject.rotate(false); + return false; + } + return true; } setNewGameTick(level: number) { diff --git a/screens/Tetris/TetrisScreen.js b/screens/Tetris/TetrisScreen.js index 532869b..a4937c9 100644 --- a/screens/Tetris/TetrisScreen.js +++ b/screens/Tetris/TetrisScreen.js @@ -91,7 +91,6 @@ class TetrisScreen extends React.Component { date.setMinutes(0); date.setSeconds(seconds); let format; - console.log(date); if (date.getHours()) format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds(); else if (date.getMinutes()) diff --git a/screens/Tetris/Tetromino.js b/screens/Tetris/Tetromino.js index 5a48fc1..094d764 100644 --- a/screens/Tetris/Tetromino.js +++ b/screens/Tetris/Tetromino.js @@ -10,35 +10,156 @@ export default class Tetromino { 'L': 6, }; - static shapes = { - 0: [ - [1, 1, 1, 1] + static shapes = [ + [ + [ + [0, 0, 0, 0], + [1, 1, 1, 1], + [0, 0, 0, 0], + [0, 0, 0, 0], + ], + [ + [1, 1], + [1, 1], + ], + [ + [0, 1, 0], + [1, 1, 1], + [0, 0, 0], + ], + [ + [0, 1, 1], + [1, 1, 0], + [0, 0, 0], + ], + [ + [1, 1, 0], + [0, 1, 1], + [0, 0, 0], + ], + [ + [1, 0, 0], + [1, 1, 1], + [0, 0, 0], + ], + [ + [0, 0, 1], + [1, 1, 1], + [0, 0, 0], + ], ], - 1: [ - [1, 1], - [1, 1] + [ + [ + [0, 0, 1, 0], + [0, 0, 1, 0], + [0, 0, 1, 0], + [0, 0, 1, 0], + ], + [ + [1, 1], + [1, 1], + ], + [ + [0, 1, 0], + [0, 1, 1], + [0, 1, 0], + ], + [ + [0, 1, 0], + [0, 1, 1], + [0, 0, 1], + ], + [ + [0, 0, 1], + [0, 1, 1], + [0, 1, 0], + ], + [ + [0, 1, 1], + [0, 1, 0], + [0, 1, 0], + ], + [ + [0, 1, 0], + [0, 1, 0], + [0, 1, 1], + ], ], - 2: [ - [0, 1, 0], - [1, 1, 1], + [ + [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [1, 1, 1, 1], + [0, 0, 0, 0], + ], + [ + [1, 1], + [1, 1], + ], + [ + [0, 0, 0], + [1, 1, 1], + [0, 1, 0], + ], + [ + [0, 0, 0], + [0, 1, 1], + [1, 1, 0], + ], + [ + [0, 0, 0], + [1, 1, 0], + [0, 1, 1], + ], + [ + [0, 0, 0], + [1, 1, 1], + [0, 0, 1], + ], + [ + [0, 0, 0], + [1, 1, 1], + [1, 0, 0], + ], ], - 3: [ - [0, 1, 1], - [1, 1, 0], + [ + [ + [0, 1, 0, 0], + [0, 1, 0, 0], + [0, 1, 0, 0], + [0, 1, 0, 0], + ], + [ + [1, 1], + [1, 1], + ], + [ + [0, 1, 0], + [1, 1, 0], + [0, 1, 0], + ], + [ + [1, 0, 0], + [1, 1, 0], + [0, 1, 0], + ], + [ + [0, 1, 0], + [1, 1, 0], + [1, 0, 0], + ], + [ + [0, 1, 0], + [0, 1, 0], + [1, 1, 0], + ], + [ + [1, 1, 0], + [0, 1, 0], + [0, 1, 0], + ], ], - 4: [ - [1, 1, 0], - [0, 1, 1], - ], - 5: [ - [1, 0, 0], - [1, 1, 1], - ], - 6: [ - [0, 0, 1], - [1, 1, 1], - ], - }; + ]; static colors: Object; @@ -50,8 +171,8 @@ export default class Tetromino { constructor(type: Tetromino.types, colors: Object) { this.currentType = type; - this.currentShape = Tetromino.shapes[type]; this.currentRotation = 0; + this.currentShape = Tetromino.shapes[this.currentRotation][type]; this.position = {x: 0, y: 0}; if (this.currentType === Tetromino.types.O) this.position.x = 4; @@ -85,25 +206,15 @@ export default class Tetromino { } rotate(isForward) { - this.currentRotation++; + if (isForward) + this.currentRotation++; + else + this.currentRotation--; if (this.currentRotation > 3) this.currentRotation = 0; - - if (this.currentRotation === 0) { - this.currentShape = Tetromino.shapes[this.currentType]; - } else { - let result = []; - for(let i = 0; i < this.currentShape[0].length; i++) { - let row = this.currentShape.map(e => e[i]); - - if (isForward) - result.push(row.reverse()); - else - result.push(row); - } - this.currentShape = result; - } - + else if (this.currentRotation < 0) + this.currentRotation = 3; + this.currentShape = Tetromino.shapes[this.currentRotation][this.currentType]; } move(x: number, y: number) {