solved pr comments; switched from hard lock task to reset counter

This commit is contained in:
2026-02-04 19:25:03 +01:00
parent 56f56d48b6
commit c176cfaf2c
5 changed files with 54 additions and 49 deletions

View File

@@ -82,11 +82,11 @@ class Tetris extends StatelessGame {
private void onPlayerInteract(@NotNull PlayerUseItemEvent event) {
event.setItemUseTime(0);
this.tetrisGames.get(event.getPlayer()).pressedButtonRaw(TetrisGame.Button.mouseRight);
this.tetrisGames.get(event.getPlayer()).pressedButton(TetrisGame.Button.mouseRight);
}
private void onPlayerAttack(@NotNull PlayerHandAnimationEvent event) {
this.tetrisGames.get(event.getPlayer()).pressedButtonRaw(TetrisGame.Button.mouseLeft);
this.tetrisGames.get(event.getPlayer()).pressedButton(TetrisGame.Button.mouseLeft);
}
private void onPlayerTick(PlayerTickEvent event) {
@@ -100,11 +100,11 @@ class Tetris extends StatelessGame {
if(player.getGameMode() == GameMode.SPECTATOR) return;
if(player.inputs().forward()) tetrisGame.pressedButtonRaw(TetrisGame.Button.W);
if(player.inputs().backward()) tetrisGame.pressedButtonRaw(TetrisGame.Button.S);
if(player.inputs().right()) tetrisGame.pressedButtonRaw(TetrisGame.Button.D);
if(player.inputs().left()) tetrisGame.pressedButtonRaw(TetrisGame.Button.A);
if(player.inputs().jump()) tetrisGame.pressedButtonRaw(TetrisGame.Button.space);
if(player.inputs().forward()) tetrisGame.pressedButton(TetrisGame.Button.W);
if(player.inputs().backward()) tetrisGame.pressedButton(TetrisGame.Button.S);
if(player.inputs().right()) tetrisGame.pressedButton(TetrisGame.Button.D);
if(player.inputs().left()) tetrisGame.pressedButton(TetrisGame.Button.A);
if(player.inputs().jump()) tetrisGame.pressedButton(TetrisGame.Button.space);
}
private void letPlayerLoose(Player player) {

View File

@@ -7,22 +7,11 @@ public enum Orientation {
UPSIDE_DOWN;
public Orientation rotated(boolean clockwise) {
switch(this) {
case NONE -> {
return clockwise ? Orientation.RIGHT : Orientation.LEFT;
}
case RIGHT -> {
return clockwise ? Orientation.UPSIDE_DOWN : Orientation.NONE;
}
case UPSIDE_DOWN -> {
return clockwise ? Orientation.LEFT : Orientation.RIGHT;
}
case LEFT -> {
return clockwise ? Orientation.NONE : Orientation.UPSIDE_DOWN;
}
default -> {
return Orientation.NONE;
}
}
return switch(this) {
case NONE -> clockwise ? Orientation.RIGHT : Orientation.LEFT;
case RIGHT -> clockwise ? Orientation.UPSIDE_DOWN : Orientation.NONE;
case UPSIDE_DOWN -> clockwise ? Orientation.LEFT : Orientation.RIGHT;
case LEFT -> clockwise ? Orientation.NONE : Orientation.UPSIDE_DOWN;
};
}
}

View File

@@ -3,7 +3,8 @@ package eu.mhsl.minenet.minigames.instance.game.stateless.types.tetris.game;
import java.util.Map;
import java.util.stream.IntStream;
public final class RotationChecker {private static final Map<Orientation, int[][]> STANDARD_WALL_KICKS = Map.of(
public final class RotationChecker {
private static final Map<Orientation, int[][]> STANDARD_WALL_KICKS = Map.of(
Orientation.NONE, new int[][] {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}},
Orientation.RIGHT, new int[][] {{0,0}, {1,0}, {1,-1}, {0,2}, {1,2}},
Orientation.UPSIDE_DOWN, new int[][] {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}},

View File

@@ -23,7 +23,8 @@ public class TetrisGame {
private final List<TetrisGame> otherTetrisGames = new ArrayList<>();
private final Random random;
private Task tetrominoLockTask;
private Task hardTetrominoLockTask;
private int lockDelayResets;
private int currentTetrominoLowestY = Integer.MAX_VALUE;
public boolean lost = false;
public boolean paused = true;
public Tetromino currentTetromino;
@@ -54,7 +55,7 @@ public class TetrisGame {
}
}
public void pressedButtonRaw(Button button) {
public void pressedButton(Button button) {
final int standardButtonDelay = 95;
final int wsButtonDebounce = 70;
@@ -114,10 +115,12 @@ public class TetrisGame {
public void tick() {
if(this.lost || this.paused) return;
if(!this.currentTetromino.isGrounded()) this.stopTetrominoLockTask(true);
if(!this.currentTetromino.moveDown()) {
if(this.currentTetromino.moveDown()) {
this.stopTetrominoLockTask(this.currentTetromino.getYPosition() < this.currentTetrominoLowestY, false);
} else {
this.scheduleTetrominoLock();
}
this.currentTetrominoLowestY = Math.min(this.currentTetrominoLowestY, this.currentTetromino.getYPosition());
}
public int getScore() {
@@ -148,20 +151,21 @@ public class TetrisGame {
this.scheduleTetrominoLock();
return;
}
this.stopTetrominoLockTask(this.currentTetromino.getYPosition() < this.currentTetrominoLowestY, false);
this.score += 1;
this.updateInfo();
}
private void moveLeft() {
if(this.currentTetromino.moveLeft()) this.stopTetrominoLockTask(false);
if(this.currentTetromino.moveLeft()) this.onSuccessfulMoveOrRotate();
}
private void moveRight() {
if(this.currentTetromino.moveRight()) this.stopTetrominoLockTask(false);
if(this.currentTetromino.moveRight()) this.onSuccessfulMoveOrRotate();
}
private void rotate(boolean clockwise) {
if(this.currentTetromino.rotate(clockwise)) this.stopTetrominoLockTask(false);
if(this.currentTetromino.rotate(clockwise)) this.onSuccessfulMoveOrRotate();
}
private void hardDrop() {
@@ -194,6 +198,7 @@ public class TetrisGame {
this.holdTetromino = new Tetromino(this.instance, this.currentTetromino.getShape());
this.currentTetromino = newCurrentTetromino;
this.currentTetrominoLowestY = Integer.MAX_VALUE;
this.currentTetromino.setPosition(this.tetrominoSpawnPosition);
this.currentTetromino.draw();
if(!this.currentTetromino.moveDown()) this.loose();
@@ -204,6 +209,12 @@ public class TetrisGame {
this.holdPossible = false;
}
private void onSuccessfulMoveOrRotate() {
if(!this.currentTetromino.isGrounded()) return;
this.stopTetrominoLockTask(false);
this.scheduleTetrominoLock();
}
private void updateNextTetrominoes() {
if(this.tetrominoBag.isEmpty()) {
for(Tetromino.Shape shape : Tetromino.Shape.values()) {
@@ -253,38 +264,33 @@ public class TetrisGame {
if(this.currentTetromino.isGrounded()) {
this.lockActiveTetromino();
} else {
this.stopTetrominoLockTask(true);
this.stopTetrominoLockTask(false, false);
}
return TaskSchedule.stop();
}, TaskSchedule.millis(500));
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 stopTetrominoLockTask(boolean resetHard) {
this.stopTetrominoLockTask(resetHard, true);
}
private void stopTetrominoLockTask(boolean resetHard, boolean addToResets) {
if(resetHard) this.lockDelayResets = 0;
if(this.lockDelayResets >= 15 && addToResets) {
this.lockActiveTetromino();
return;
}
if(this.tetrominoLockTask != null) {
this.tetrominoLockTask.cancel();
this.tetrominoLockTask = null;
}
if(resetHard && this.hardTetrominoLockTask != null) {
this.hardTetrominoLockTask.cancel();
this.hardTetrominoLockTask = null;
if(!resetHard && addToResets) this.lockDelayResets++;
}
}
private void lockActiveTetromino() {
this.stopTetrominoLockTask(true);
this.currentTetromino.removeOwnEntities();
this.currentTetromino = this.nextTetrominoes.removeFirst();
this.currentTetromino.remove();
this.updateNextTetrominoes();
int removedLines = this.playfield.removeFullLines();
int combatLines = 0;
@@ -342,6 +348,11 @@ public class TetrisGame {
this.updateInfo();
this.currentTetromino = this.nextTetrominoes.removeFirst();
this.currentTetromino.remove();
this.updateNextTetrominoes();
this.currentTetrominoLowestY = Integer.MAX_VALUE;
this.currentTetromino.setPosition(this.tetrominoSpawnPosition);
this.currentTetromino.draw();
if(!this.currentTetromino.moveDown()) {

View File

@@ -244,6 +244,10 @@ public class Tetromino {
return true;
}
public int getYPosition() {
return Math.toIntExact(Math.round(this.position.y()));
}
public enum Shape {
I,
J,