application-amicale/screens/Tetris/Piece.js

106 lines
3 KiB
JavaScript
Raw Normal View History

2020-03-23 11:32:50 +01:00
import ShapeL from "./Shapes/ShapeL";
import ShapeI from "./Shapes/ShapeI";
import ShapeJ from "./Shapes/ShapeJ";
import ShapeO from "./Shapes/ShapeO";
import ShapeS from "./Shapes/ShapeS";
import ShapeT from "./Shapes/ShapeT";
import ShapeZ from "./Shapes/ShapeZ";
export default class Piece {
#shapes = [
ShapeL,
ShapeI,
ShapeJ,
ShapeO,
ShapeS,
ShapeT,
ShapeZ,
];
#currentShape: Object;
#colors: Object;
2020-03-23 11:32:50 +01:00
constructor(colors: Object) {
2020-03-23 17:30:24 +01:00
this.#currentShape = this.getRandomShape(colors);
this.#colors = colors;
2020-03-23 17:30:24 +01:00
}
getRandomShape(colors: Object) {
return new this.#shapes[Math.floor(Math.random() * 7)](colors);
2020-03-23 11:32:50 +01:00
}
removeFromGrid(grid) {
const coord = this.#currentShape.getCellsCoordinates(true);
for (let i = 0; i < coord.length; i++) {
grid[coord[i].y][coord[i].x] = {
color: this.#colors.tetrisBackground,
isEmpty: true,
};
}
}
2020-03-23 11:32:50 +01:00
toGrid(grid: Array<Array<Object>>, isPreview: boolean) {
const coord = this.#currentShape.getCellsCoordinates(!isPreview);
for (let i = 0; i < coord.length; i++) {
grid[coord[i].y][coord[i].x] = {
color: this.#currentShape.getColor(),
isEmpty: false,
};
}
}
isPositionValid(grid, width, height) {
let isValid = true;
const coord = this.#currentShape.getCellsCoordinates(true);
for (let i = 0; i < coord.length; i++) {
if (coord[i].x >= width
|| coord[i].x < 0
|| coord[i].y >= height
|| coord[i].y < 0
|| !grid[coord[i].y][coord[i].x].isEmpty) {
isValid = false;
break;
}
}
return isValid;
}
tryMove(x: number, y: number, grid, width, height, freezeCallback: Function) {
if (x > 1) x = 1; // Prevent moving from more than one tile
if (x < -1) x = -1;
if (y > 1) y = 1;
if (y < -1) y = -1;
if (x !== 0 && y !== 0) y = 0; // Prevent diagonal movement
this.removeFromGrid(grid);
2020-03-23 11:32:50 +01:00
this.#currentShape.move(x, y);
let isValid = this.isPositionValid(grid, width, height);
let shouldFreeze = false;
2020-03-23 11:32:50 +01:00
if (!isValid)
this.#currentShape.move(-x, -y);
shouldFreeze = !isValid && y !== 0;
this.toGrid(grid, false);
if (shouldFreeze)
2020-03-23 11:32:50 +01:00
freezeCallback();
return isValid;
2020-03-23 11:32:50 +01:00
}
tryRotate(grid, width, height) {
this.removeFromGrid(grid);
2020-03-23 11:32:50 +01:00
this.#currentShape.rotate(true);
if (!this.isPositionValid(grid, width, height)) {
this.#currentShape.rotate(false);
this.toGrid(grid, false);
2020-03-23 11:32:50 +01:00
return false;
}
this.toGrid(grid, false);
2020-03-23 11:32:50 +01:00
return true;
}
getCoordinates() {
return this.#currentShape.getCellsCoordinates(true);
}
}