diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java index 5c3ff67..8695f9f 100644 --- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java +++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java @@ -22,8 +22,8 @@ public class TetrisGame { private final Map lastPresses = new HashMap<>(); private final List otherTetrisGames = new ArrayList<>(); private final Random random; - private Task tetrominoPlacementTask; - private Task hardTetrominoPlacementTask; + private Task tetrominoLockTask; + private Task hardTetrominoLockTask; public boolean lost = false; public boolean paused = true; public Tetromino currentTetromino; @@ -73,7 +73,7 @@ public class TetrisGame { case A -> this.moveLeft(); case S -> this.moveDown(); case D -> this.moveRight(); - case W -> this.hardDrop(true); + case W -> this.hardDrop(); case mouseLeft -> this.rotate(false); case mouseRight -> this.rotate(true); case space -> this.switchHold(); @@ -114,8 +114,9 @@ public class TetrisGame { public void tick() { if(this.lost || this.paused) return; + if(!this.currentTetromino.isGrounded()) this.stopTetrominoLockTask(true); if(!this.currentTetromino.moveDown()) { - this.scheduleTetrominoPlacement(); + this.scheduleTetrominoLock(); } } @@ -144,7 +145,7 @@ public class TetrisGame { private void moveDown() { if(!this.currentTetromino.moveDown()) { - this.scheduleTetrominoPlacement(); + this.scheduleTetrominoLock(); return; } this.score += 1; @@ -152,32 +153,32 @@ public class TetrisGame { } private void moveLeft() { - if(this.currentTetromino.moveLeft()) this.stopTetrominoPlacementTask(false); + if(this.currentTetromino.moveLeft()) this.stopTetrominoLockTask(false); } private void moveRight() { - if(this.currentTetromino.moveRight()) this.stopTetrominoPlacementTask(false); + if(this.currentTetromino.moveRight()) this.stopTetrominoLockTask(false); } 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()) { - this.setActiveTetrominoDown(); + this.lockActiveTetromino(); return; } do { - if(addScore) this.score += 2; - this.updateInfo(); + this.score += 2; } while(this.currentTetromino.moveDown()); - this.setActiveTetrominoDown(); + this.updateInfo(); + this.lockActiveTetromino(); } private void switchHold() { if(!this.holdPossible) return; - this.stopTetrominoPlacementTask(true); + this.stopTetrominoLockTask(true); Tetromino newCurrentTetromino; if(this.holdTetromino == null) { @@ -246,32 +247,40 @@ public class TetrisGame { this.sidebar.updateLineScore("2", this.level); } - private void scheduleTetrominoPlacement() { - if(this.tetrominoPlacementTask == null || !this.tetrominoPlacementTask.isAlive()) - this.tetrominoPlacementTask = this.instance.scheduler().scheduleTask(() -> { - this.hardDrop(false); + private void scheduleTetrominoLock() { + if(this.tetrominoLockTask == null || !this.tetrominoLockTask.isAlive()) + this.tetrominoLockTask = this.instance.scheduler().scheduleTask(() -> { + if(this.currentTetromino.isGrounded()) { + this.lockActiveTetromino(); + } else { + this.stopTetrominoLockTask(true); + } return TaskSchedule.stop(); }, TaskSchedule.millis(500)); - if(this.hardTetrominoPlacementTask == null || !this.hardTetrominoPlacementTask.isAlive()) - this.hardTetrominoPlacementTask = this.instance.scheduler().scheduleTask(() -> { - this.hardDrop(false); + if(this.hardTetrominoLockTask == null || !this.hardTetrominoLockTask.isAlive()) + this.hardTetrominoLockTask = this.instance.scheduler().scheduleTask(() -> { + if(this.currentTetromino.isGrounded()) { + this.lockActiveTetromino(); + } else { + this.stopTetrominoLockTask(true); + } return TaskSchedule.stop(); }, TaskSchedule.millis(6000)); } - private void stopTetrominoPlacementTask(boolean resetHard) { - if(this.tetrominoPlacementTask != null) { - this.tetrominoPlacementTask.cancel(); - this.tetrominoPlacementTask = null; + private void stopTetrominoLockTask(boolean resetHard) { + if(this.tetrominoLockTask != null) { + this.tetrominoLockTask.cancel(); + this.tetrominoLockTask = null; } - if(resetHard && this.hardTetrominoPlacementTask != null) { - this.hardTetrominoPlacementTask.cancel(); - this.hardTetrominoPlacementTask = null; + if(resetHard && this.hardTetrominoLockTask != null) { + this.hardTetrominoLockTask.cancel(); + this.hardTetrominoLockTask = null; } } - private void setActiveTetrominoDown() { - this.stopTetrominoPlacementTask(true); + private void lockActiveTetromino() { + this.stopTetrominoLockTask(true); this.currentTetromino.removeOwnEntities(); this.currentTetromino = this.nextTetrominoes.removeFirst(); this.currentTetromino.remove(); diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java index 69cbdb6..bce04ed 100644 --- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java +++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java @@ -231,6 +231,10 @@ public class Tetromino { return false; } + public boolean isGrounded() { + return this.hasCollision(this.position.sub(0, 1, 0), this.shapeArray); + } + private boolean checkCollisionAndMove(Pos newPosition, int[][] newShapeArray) { if(this.hasCollision(newPosition, newShapeArray)) return false; this.remove();