feat: check for collisions with other tetrominos
This commit is contained in:
@@ -9,6 +9,7 @@ public class Tetromino {
|
||||
private final TetrominoShape shape;
|
||||
private int[][] collision;
|
||||
private Vec2i centerPos;
|
||||
private int fallLength = 0;
|
||||
|
||||
public Tetromino(TetrominoShape shape) {
|
||||
this.shape = shape;
|
||||
@@ -18,7 +19,8 @@ public class Tetromino {
|
||||
|
||||
public void fall(int length) {
|
||||
Vec2i newPos = centerPos.offset(Vec2i.of(0, length));
|
||||
if (Tetris.space != null && newPos.getY()+this.collision.length > Tetris.space.getMapHeight()) {
|
||||
if (collidesVertically(newPos)) {
|
||||
if (fallLength < 1) System.out.println("Game over!");
|
||||
int[] affectedLines = new int[this.collision.length];
|
||||
int line = centerPos.getY();
|
||||
for (int i = 0; i < this.collision.length; i++) {
|
||||
@@ -28,17 +30,46 @@ public class Tetromino {
|
||||
Tetris.space.onLinesChanged(this, affectedLines);
|
||||
Tetris.space.spawnTetromino();
|
||||
}
|
||||
fallLength += 1;
|
||||
centerPos = newPos;
|
||||
}
|
||||
|
||||
private boolean collidesVertically(Vec2i newPos) {
|
||||
if (Tetris.space == null) return false;
|
||||
boolean collides = newPos.getY() + this.collision.length > Tetris.space.getMapHeight(); // Bottom check
|
||||
if (!collides) {
|
||||
for (int i = 0; i < collision[0].length; i++) {
|
||||
int maxCollisionY = collision.length - 1;
|
||||
while (collision[maxCollisionY][i] == 0) maxCollisionY--;
|
||||
if (newPos.getY()+maxCollisionY >= Tetris.space.getGameMap().length) continue;
|
||||
collides |= Tetris.space.getGameMap()[newPos.getY() + maxCollisionY][newPos.getX() + i] != null; // Check for other tetrominos
|
||||
}
|
||||
}
|
||||
return collides;
|
||||
}
|
||||
|
||||
public void move(int xOffset) {
|
||||
Vec2i newPos = centerPos.offset(Vec2i.of(xOffset, 0));
|
||||
if (Tetris.space == null || newPos.getX() < 0 || newPos.getX() + collision[0].length > Tetris.space.getGameMap()[0].length) {
|
||||
if (collidesHorizontally(newPos, xOffset)) {
|
||||
return;
|
||||
}
|
||||
centerPos = newPos;
|
||||
}
|
||||
|
||||
private boolean collidesHorizontally(Vec2i newPos, int xOffset) {
|
||||
if (Tetris.space == null) return false;
|
||||
boolean collides = newPos.getX() < 0 || newPos.getX() + collision[0].length > Tetris.space.getGameMap()[0].length;
|
||||
if (!collides) {
|
||||
for (int i = 0; i < collision.length; i++) {
|
||||
int maxCollisionX = xOffset > 0 ? collision[i].length - 1 : 0;
|
||||
while (collision[i][maxCollisionX] == 0) maxCollisionX += xOffset > 0 ? -1 : 1;
|
||||
if (newPos.getY()+maxCollisionX >= Tetris.space.getGameMap().length) continue;
|
||||
collides |= Tetris.space.getGameMap()[newPos.getY() + i][newPos.getX() + maxCollisionX] != null; // Check for other tetrominos
|
||||
}
|
||||
}
|
||||
return collides;
|
||||
}
|
||||
|
||||
public void rotate() {
|
||||
int M = collision.length;
|
||||
int N = collision[0].length;
|
||||
|
||||
Reference in New Issue
Block a user