From c176cfaf2cb719bdc3d27c52ebeee05b73ce1e8d Mon Sep 17 00:00:00 2001 From: lars Date: Wed, 4 Feb 2026 19:25:03 +0100 Subject: [PATCH] solved pr comments; switched from hard lock task to reset counter --- .../game/stateless/types/tetris/Tetris.java | 14 ++--- .../types/tetris/game/Orientation.java | 23 ++------ .../types/tetris/game/RotationChecker.java | 3 +- .../types/tetris/game/TetrisGame.java | 59 +++++++++++-------- .../types/tetris/game/Tetromino.java | 4 ++ 5 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java index 80318c8..0f3336f 100644 --- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java +++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java @@ -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) { diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Orientation.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Orientation.java index 6ebf10d..efbc78b 100644 --- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Orientation.java +++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Orientation.java @@ -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; + }; } } diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/RotationChecker.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/RotationChecker.java index f0f91ef..3f755a0 100644 --- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/RotationChecker.java +++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/RotationChecker.java @@ -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 STANDARD_WALL_KICKS = Map.of( +public final class RotationChecker { + private static final Map 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}}, 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 8695f9f..5098939 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 @@ -23,7 +23,8 @@ public class TetrisGame { private final List 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()) { 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 bce04ed..6d8b0b0 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 @@ -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,