forked from vergnet/application-amicale
Improved piece rotation
This commit is contained in:
parent
b29973189f
commit
79e72784d1
3 changed files with 158 additions and 45 deletions
|
@ -187,8 +187,11 @@ export default class GameLogic {
|
||||||
|
|
||||||
tryRotateTetromino() {
|
tryRotateTetromino() {
|
||||||
this.currentObject.rotate(true);
|
this.currentObject.rotate(true);
|
||||||
if (!this.isTetrominoPositionValid())
|
if (!this.isTetrominoPositionValid()){
|
||||||
this.currentObject.rotate(false);
|
this.currentObject.rotate(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
setNewGameTick(level: number) {
|
setNewGameTick(level: number) {
|
||||||
|
|
|
@ -91,7 +91,6 @@ class TetrisScreen extends React.Component<Props, State> {
|
||||||
date.setMinutes(0);
|
date.setMinutes(0);
|
||||||
date.setSeconds(seconds);
|
date.setSeconds(seconds);
|
||||||
let format;
|
let format;
|
||||||
console.log(date);
|
|
||||||
if (date.getHours())
|
if (date.getHours())
|
||||||
format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
|
format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
|
||||||
else if (date.getMinutes())
|
else if (date.getMinutes())
|
||||||
|
|
|
@ -10,35 +10,156 @@ export default class Tetromino {
|
||||||
'L': 6,
|
'L': 6,
|
||||||
};
|
};
|
||||||
|
|
||||||
static shapes = {
|
static shapes = [
|
||||||
0: [
|
[
|
||||||
[1, 1, 1, 1]
|
[
|
||||||
|
[0, 0, 0, 0],
|
||||||
|
[1, 1, 1, 1],
|
||||||
|
[0, 0, 0, 0],
|
||||||
|
[0, 0, 0, 0],
|
||||||
],
|
],
|
||||||
1: [
|
[
|
||||||
|
[1, 1],
|
||||||
[1, 1],
|
[1, 1],
|
||||||
[1, 1]
|
|
||||||
],
|
],
|
||||||
2: [
|
[
|
||||||
[0, 1, 0],
|
[0, 1, 0],
|
||||||
[1, 1, 1],
|
[1, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
3: [
|
[
|
||||||
[0, 1, 1],
|
[0, 1, 1],
|
||||||
[1, 1, 0],
|
[1, 1, 0],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
4: [
|
[
|
||||||
[1, 1, 0],
|
[1, 1, 0],
|
||||||
[0, 1, 1],
|
[0, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
5: [
|
[
|
||||||
[1, 0, 0],
|
[1, 0, 0],
|
||||||
[1, 1, 1],
|
[1, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
6: [
|
[
|
||||||
[0, 0, 1],
|
[0, 0, 1],
|
||||||
[1, 1, 1],
|
[1, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
};
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[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],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[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],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[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],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
static colors: Object;
|
static colors: Object;
|
||||||
|
|
||||||
|
@ -50,8 +171,8 @@ export default class Tetromino {
|
||||||
|
|
||||||
constructor(type: Tetromino.types, colors: Object) {
|
constructor(type: Tetromino.types, colors: Object) {
|
||||||
this.currentType = type;
|
this.currentType = type;
|
||||||
this.currentShape = Tetromino.shapes[type];
|
|
||||||
this.currentRotation = 0;
|
this.currentRotation = 0;
|
||||||
|
this.currentShape = Tetromino.shapes[this.currentRotation][type];
|
||||||
this.position = {x: 0, y: 0};
|
this.position = {x: 0, y: 0};
|
||||||
if (this.currentType === Tetromino.types.O)
|
if (this.currentType === Tetromino.types.O)
|
||||||
this.position.x = 4;
|
this.position.x = 4;
|
||||||
|
@ -85,25 +206,15 @@ export default class Tetromino {
|
||||||
}
|
}
|
||||||
|
|
||||||
rotate(isForward) {
|
rotate(isForward) {
|
||||||
|
if (isForward)
|
||||||
this.currentRotation++;
|
this.currentRotation++;
|
||||||
|
else
|
||||||
|
this.currentRotation--;
|
||||||
if (this.currentRotation > 3)
|
if (this.currentRotation > 3)
|
||||||
this.currentRotation = 0;
|
this.currentRotation = 0;
|
||||||
|
else if (this.currentRotation < 0)
|
||||||
if (this.currentRotation === 0) {
|
this.currentRotation = 3;
|
||||||
this.currentShape = Tetromino.shapes[this.currentType];
|
this.currentShape = Tetromino.shapes[this.currentRotation][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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
move(x: number, y: number) {
|
move(x: number, y: number) {
|
||||||
|
|
Loading…
Reference in a new issue