application-amicale/screens/Tetris/Tetromino.js

229 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-03-15 18:44:32 +01:00
export default class Tetromino {
static types = {
'I': 0,
'O': 1,
'T': 2,
'S': 3,
'Z': 4,
'J': 5,
'L': 6,
};
2020-03-16 23:36:01 +01:00
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],
],
2020-03-15 18:44:32 +01:00
],
2020-03-16 23:36:01 +01:00
[
[
[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],
],
2020-03-15 18:44:32 +01:00
],
2020-03-16 23:36:01 +01:00
[
[
[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],
],
2020-03-15 18:44:32 +01:00
],
2020-03-16 23:36:01 +01:00
[
[
[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],
],
2020-03-15 18:44:32 +01:00
],
2020-03-16 23:36:01 +01:00
];
2020-03-15 18:44:32 +01:00
2020-03-15 20:34:20 +01:00
static colors: Object;
2020-03-15 18:44:32 +01:00
2020-03-15 19:28:41 +01:00
currentType: Object;
currentShape: Object;
currentRotation: number;
2020-03-15 18:44:32 +01:00
position: Object;
2020-03-15 20:34:20 +01:00
colors: Object;
2020-03-15 18:44:32 +01:00
2020-03-15 20:34:20 +01:00
constructor(type: Tetromino.types, colors: Object) {
2020-03-15 18:44:32 +01:00
this.currentType = type;
2020-03-15 19:28:41 +01:00
this.currentRotation = 0;
2020-03-16 23:36:01 +01:00
this.currentShape = Tetromino.shapes[this.currentRotation][type];
2020-03-15 18:44:32 +01:00
this.position = {x: 0, y: 0};
2020-03-16 08:22:18 +01:00
if (this.currentType === Tetromino.types.O)
this.position.x = 4;
else
this.position.x = 3;
2020-03-15 20:34:20 +01:00
this.colors = colors;
Tetromino.colors = {
0: colors.tetrisI,
1: colors.tetrisO,
2: colors.tetrisT,
3: colors.tetrisS,
4: colors.tetrisZ,
5: colors.tetrisJ,
6: colors.tetrisL,
};
2020-03-15 18:44:32 +01:00
}
getColor() {
return Tetromino.colors[this.currentType];
}
2020-03-17 14:22:49 +01:00
getCellsCoordinates(isAbsolute: boolean) {
2020-03-15 18:44:32 +01:00
let coordinates = [];
2020-03-15 19:28:41 +01:00
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)
2020-03-17 14:22:49 +01:00
if (isAbsolute)
coordinates.push({x: this.position.x + col, y: this.position.y + row});
else
coordinates.push({x: col, y: row});
2020-03-15 18:44:32 +01:00
}
}
return coordinates;
}
2020-03-15 19:28:41 +01:00
rotate(isForward) {
2020-03-16 23:36:01 +01:00
if (isForward)
this.currentRotation++;
else
this.currentRotation--;
2020-03-15 19:28:41 +01:00
if (this.currentRotation > 3)
this.currentRotation = 0;
2020-03-16 23:36:01 +01:00
else if (this.currentRotation < 0)
this.currentRotation = 3;
this.currentShape = Tetromino.shapes[this.currentRotation][this.currentType];
2020-03-15 19:28:41 +01:00
}
2020-03-15 18:44:32 +01:00
move(x: number, y: number) {
this.position.x += x;
this.position.y += y;
}
}