develop-tetris-srs #11

Open
Pupsi wants to merge 7 commits from develop-tetris-srs into develop
2 changed files with 43 additions and 30 deletions
Showing only changes of commit 56f56d48b6 - Show all commits

View File

@@ -22,8 +22,8 @@ public class TetrisGame {
private final Map<Button, Long> lastPresses = new HashMap<>(); private final Map<Button, Long> lastPresses = new HashMap<>();
private final List<TetrisGame> otherTetrisGames = new ArrayList<>(); private final List<TetrisGame> otherTetrisGames = new ArrayList<>();
private final Random random; private final Random random;
private Task tetrominoPlacementTask; private Task tetrominoLockTask;
private Task hardTetrominoPlacementTask; private Task hardTetrominoLockTask;
public boolean lost = false; public boolean lost = false;
public boolean paused = true; public boolean paused = true;
public Tetromino currentTetromino; public Tetromino currentTetromino;
@@ -73,7 +73,7 @@ public class TetrisGame {
case A -> this.moveLeft(); case A -> this.moveLeft();
case S -> this.moveDown(); case S -> this.moveDown();
case D -> this.moveRight(); case D -> this.moveRight();
case W -> this.hardDrop(true); case W -> this.hardDrop();
case mouseLeft -> this.rotate(false); case mouseLeft -> this.rotate(false);
case mouseRight -> this.rotate(true); case mouseRight -> this.rotate(true);
case space -> this.switchHold(); case space -> this.switchHold();
@@ -114,8 +114,9 @@ public class TetrisGame {
public void tick() { public void tick() {
if(this.lost || this.paused) return; if(this.lost || this.paused) return;
if(!this.currentTetromino.isGrounded()) this.stopTetrominoLockTask(true);
if(!this.currentTetromino.moveDown()) { if(!this.currentTetromino.moveDown()) {
this.scheduleTetrominoPlacement(); this.scheduleTetrominoLock();
} }
} }
@@ -144,7 +145,7 @@ public class TetrisGame {
private void moveDown() { private void moveDown() {
if(!this.currentTetromino.moveDown()) { if(!this.currentTetromino.moveDown()) {
this.scheduleTetrominoPlacement(); this.scheduleTetrominoLock();
return; return;
} }
this.score += 1; this.score += 1;
@@ -152,32 +153,32 @@ public class TetrisGame {
} }
private void moveLeft() { private void moveLeft() {
if(this.currentTetromino.moveLeft()) this.stopTetrominoPlacementTask(false); if(this.currentTetromino.moveLeft()) this.stopTetrominoLockTask(false);
} }
private void moveRight() { private void moveRight() {
if(this.currentTetromino.moveRight()) this.stopTetrominoPlacementTask(false); if(this.currentTetromino.moveRight()) this.stopTetrominoLockTask(false);
} }
private void rotate(boolean clockwise) { private void rotate(boolean clockwise) {
if(this.currentTetromino.rotate(clockwise)) this.stopTetrominoPlacementTask(false); if(this.currentTetromino.rotate(clockwise)) this.stopTetrominoLockTask(false);
} }
private void hardDrop(boolean addScore) { private void hardDrop() {
if(!this.currentTetromino.moveDown()) { if(!this.currentTetromino.moveDown()) {
this.setActiveTetrominoDown(); this.lockActiveTetromino();
return; return;
} }
do { do {
if(addScore) this.score += 2; this.score += 2;
this.updateInfo();
} while(this.currentTetromino.moveDown()); } while(this.currentTetromino.moveDown());
this.setActiveTetrominoDown(); this.updateInfo();
this.lockActiveTetromino();
} }
private void switchHold() { private void switchHold() {
if(!this.holdPossible) return; if(!this.holdPossible) return;
this.stopTetrominoPlacementTask(true); this.stopTetrominoLockTask(true);
Tetromino newCurrentTetromino; Tetromino newCurrentTetromino;
if(this.holdTetromino == null) { if(this.holdTetromino == null) {
@@ -246,32 +247,40 @@ public class TetrisGame {
this.sidebar.updateLineScore("2", this.level); this.sidebar.updateLineScore("2", this.level);
} }
private void scheduleTetrominoPlacement() { private void scheduleTetrominoLock() {
if(this.tetrominoPlacementTask == null || !this.tetrominoPlacementTask.isAlive()) if(this.tetrominoLockTask == null || !this.tetrominoLockTask.isAlive())
this.tetrominoPlacementTask = this.instance.scheduler().scheduleTask(() -> { this.tetrominoLockTask = this.instance.scheduler().scheduleTask(() -> {
this.hardDrop(false); if(this.currentTetromino.isGrounded()) {
this.lockActiveTetromino();
} else {
this.stopTetrominoLockTask(true);
}
return TaskSchedule.stop(); return TaskSchedule.stop();
}, TaskSchedule.millis(500)); }, TaskSchedule.millis(500));
if(this.hardTetrominoPlacementTask == null || !this.hardTetrominoPlacementTask.isAlive()) if(this.hardTetrominoLockTask == null || !this.hardTetrominoLockTask.isAlive())
this.hardTetrominoPlacementTask = this.instance.scheduler().scheduleTask(() -> { this.hardTetrominoLockTask = this.instance.scheduler().scheduleTask(() -> {
this.hardDrop(false); if(this.currentTetromino.isGrounded()) {
this.lockActiveTetromino();
} else {
this.stopTetrominoLockTask(true);
}
return TaskSchedule.stop(); return TaskSchedule.stop();
}, TaskSchedule.millis(6000)); }, TaskSchedule.millis(6000));
} }
private void stopTetrominoPlacementTask(boolean resetHard) { private void stopTetrominoLockTask(boolean resetHard) {
if(this.tetrominoPlacementTask != null) { if(this.tetrominoLockTask != null) {
this.tetrominoPlacementTask.cancel(); this.tetrominoLockTask.cancel();
this.tetrominoPlacementTask = null; this.tetrominoLockTask = null;
} }
if(resetHard && this.hardTetrominoPlacementTask != null) { if(resetHard && this.hardTetrominoLockTask != null) {
this.hardTetrominoPlacementTask.cancel(); this.hardTetrominoLockTask.cancel();
this.hardTetrominoPlacementTask = null; this.hardTetrominoLockTask = null;
} }
} }
private void setActiveTetrominoDown() { private void lockActiveTetromino() {
this.stopTetrominoPlacementTask(true); this.stopTetrominoLockTask(true);
this.currentTetromino.removeOwnEntities(); this.currentTetromino.removeOwnEntities();
this.currentTetromino = this.nextTetrominoes.removeFirst(); this.currentTetromino = this.nextTetrominoes.removeFirst();
this.currentTetromino.remove(); this.currentTetromino.remove();

View File

@@ -231,6 +231,10 @@ public class Tetromino {
return false; return false;
} }
public boolean isGrounded() {
return this.hasCollision(this.position.sub(0, 1, 0), this.shapeArray);
}
private boolean checkCollisionAndMove(Pos newPosition, int[][] newShapeArray) { private boolean checkCollisionAndMove(Pos newPosition, int[][] newShapeArray) {
if(this.hasCollision(newPosition, newShapeArray)) return false; if(this.hasCollision(newPosition, newShapeArray)) return false;
this.remove(); this.remove();