develop-tetris-srs #11

Open
Pupsi wants to merge 6 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 List<TetrisGame> 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(() -> {
Review

tasks hierfür fühlen sich falsch an...
Wo kommen die Limits (500ms) her? Ist das im Original auch genau so?

tasks hierfür fühlen sich falsch an... Wo kommen die Limits (500ms) her? Ist das im Original auch genau so?
Review

Ja, die 500ms sind wie in den Tetris Guidelines. Allerdings gibt es dort keinen hard lock task, sondern einen Counter.
Änder ich um.

Ja, die 500ms sind wie in den Tetris Guidelines. Allerdings gibt es dort keinen hard lock task, sondern einen Counter. Änder ich um.
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();

View File

@@ -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();