feat: display score & game over, code cleanup
This commit is contained in:
@@ -20,34 +20,20 @@ public class Tetromino {
|
||||
public void fall(int length) {
|
||||
Vec2i newPos = centerPos.offset(Vec2i.of(0, length));
|
||||
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++) {
|
||||
affectedLines[i] = line;
|
||||
line++;
|
||||
}
|
||||
Tetris.space.onLinesChanged(this, affectedLines);
|
||||
Tetris.space.spawnTetromino();
|
||||
Tetris.getSpace().onLinesChanged(this, affectedLines);
|
||||
if (fallLength >= 1) Tetris.getSpace().spawnTetromino();
|
||||
else Tetris.stopGame();
|
||||
}
|
||||
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 (collidesHorizontally(newPos, xOffset)) {
|
||||
@@ -56,15 +42,34 @@ public class Tetromino {
|
||||
centerPos = newPos;
|
||||
}
|
||||
|
||||
private boolean collidesVertically(Vec2i newPos) {
|
||||
if (Tetris.getSpace() == null) return false;
|
||||
|
||||
boolean collides = newPos.getY() + this.collision.length > Tetris.getSpace().getMapHeight(); // Bottom check
|
||||
|
||||
if (!collides) { // Check for other tetrominos
|
||||
for (int i = 0; i < collision[0].length; i++) {
|
||||
int maxCollisionY = collision.length - 1;
|
||||
while (collision[maxCollisionY][i] == 0) maxCollisionY--; // Figure out the collision box's bounding
|
||||
|
||||
if (newPos.getY()+maxCollisionY >= Tetris.getSpace().getMapHeight() || newPos.getX() + i >= Tetris.getSpace().getMapWidth()) continue;
|
||||
collides |= Tetris.getSpace().getGameMap()[newPos.getY() + maxCollisionY][newPos.getX() + i] != null;
|
||||
}
|
||||
}
|
||||
return collides;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (Tetris.getSpace() == null) return false;
|
||||
|
||||
boolean collides = newPos.getX() < 0 || newPos.getX() + collision[0].length > Tetris.getSpace().getMapWidth(); // Side check
|
||||
if (!collides) { // Check for other tetrominos
|
||||
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
|
||||
while (collision[i][maxCollisionX] == 0) maxCollisionX += xOffset > 0 ? -1 : 1; // Figure out the collision box's bounding
|
||||
|
||||
if (newPos.getY()+maxCollisionX >= Tetris.getSpace().getMapHeight()) continue;
|
||||
collides |= Tetris.getSpace().getGameMap()[newPos.getY() + i][newPos.getX() + maxCollisionX] != null;
|
||||
}
|
||||
}
|
||||
return collides;
|
||||
|
||||
Reference in New Issue
Block a user