fixed ground behavior for tetrominoes
This commit is contained in:
@@ -82,11 +82,11 @@ class Tetris extends StatelessGame {
|
|||||||
|
|
||||||
private void onPlayerInteract(@NotNull PlayerUseItemEvent event) {
|
private void onPlayerInteract(@NotNull PlayerUseItemEvent event) {
|
||||||
event.setItemUseTime(0);
|
event.setItemUseTime(0);
|
||||||
this.tetrisGames.get(event.getPlayer()).pressedButton(TetrisGame.Button.mouseRight);
|
this.tetrisGames.get(event.getPlayer()).pressedButtonRaw(TetrisGame.Button.mouseRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPlayerAttack(@NotNull PlayerHandAnimationEvent event) {
|
private void onPlayerAttack(@NotNull PlayerHandAnimationEvent event) {
|
||||||
this.tetrisGames.get(event.getPlayer()).pressedButton(TetrisGame.Button.mouseLeft);
|
this.tetrisGames.get(event.getPlayer()).pressedButtonRaw(TetrisGame.Button.mouseLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPlayerTick(PlayerTickEvent event) {
|
private void onPlayerTick(PlayerTickEvent event) {
|
||||||
@@ -100,11 +100,11 @@ class Tetris extends StatelessGame {
|
|||||||
|
|
||||||
if(player.getGameMode() == GameMode.SPECTATOR) return;
|
if(player.getGameMode() == GameMode.SPECTATOR) return;
|
||||||
|
|
||||||
if(player.inputs().forward()) tetrisGame.pressedButton(TetrisGame.Button.W);
|
if(player.inputs().forward()) tetrisGame.pressedButtonRaw(TetrisGame.Button.W);
|
||||||
if(player.inputs().backward()) tetrisGame.pressedButton(TetrisGame.Button.S);
|
if(player.inputs().backward()) tetrisGame.pressedButtonRaw(TetrisGame.Button.S);
|
||||||
if(player.inputs().right()) tetrisGame.pressedButton(TetrisGame.Button.D);
|
if(player.inputs().right()) tetrisGame.pressedButtonRaw(TetrisGame.Button.D);
|
||||||
if(player.inputs().left()) tetrisGame.pressedButton(TetrisGame.Button.A);
|
if(player.inputs().left()) tetrisGame.pressedButtonRaw(TetrisGame.Button.A);
|
||||||
if(player.inputs().jump()) tetrisGame.pressedButton(TetrisGame.Button.space);
|
if(player.inputs().jump()) tetrisGame.pressedButtonRaw(TetrisGame.Button.space);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void letPlayerLoose(Player player) {
|
private void letPlayerLoose(Player player) {
|
||||||
|
|||||||
@@ -2,10 +2,9 @@ package eu.mhsl.minenet.minigames.instance.game.stateless.types.tetris.game;
|
|||||||
|
|
||||||
import eu.mhsl.minenet.minigames.instance.game.stateless.StatelessGame;
|
import eu.mhsl.minenet.minigames.instance.game.stateless.StatelessGame;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.MinecraftServer;
|
|
||||||
import net.minestom.server.coordinate.Pos;
|
import net.minestom.server.coordinate.Pos;
|
||||||
import net.minestom.server.scoreboard.Sidebar;
|
import net.minestom.server.scoreboard.Sidebar;
|
||||||
import net.minestom.server.timer.Scheduler;
|
import net.minestom.server.timer.Task;
|
||||||
import net.minestom.server.timer.TaskSchedule;
|
import net.minestom.server.timer.TaskSchedule;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -23,6 +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 hardTetrominoPlacementTask;
|
||||||
public boolean lost = false;
|
public boolean lost = false;
|
||||||
public boolean paused = true;
|
public boolean paused = true;
|
||||||
public Tetromino currentTetromino;
|
public Tetromino currentTetromino;
|
||||||
@@ -53,25 +54,28 @@ public class TetrisGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pressedButton(Button button) {
|
public void pressedButtonRaw(Button button) {
|
||||||
final int standardButtonDelay = 100;
|
final int standardButtonDelay = 95;
|
||||||
final int buttonDebounce = 70;
|
final int wsButtonDebounce = 70;
|
||||||
|
|
||||||
if(this.lastPresses.getOrDefault(button, 0L) >= System.currentTimeMillis() - standardButtonDelay) return;
|
if(this.lastPresses.getOrDefault(button, 0L) >= System.currentTimeMillis() - standardButtonDelay) return;
|
||||||
|
|
||||||
this.lastPresses.put(button, System.currentTimeMillis());
|
switch(button) {
|
||||||
if(button == Button.W) this.lastPresses.put(button, System.currentTimeMillis() + buttonDebounce);
|
case W -> this.lastPresses.put(button, System.currentTimeMillis() + wsButtonDebounce);
|
||||||
if(button == Button.S) this.lastPresses.put(button, System.currentTimeMillis() - buttonDebounce);
|
case S -> this.lastPresses.put(button, System.currentTimeMillis() - wsButtonDebounce);
|
||||||
|
case mouseLeft, mouseRight -> this.lastPresses.put(button, 0L);
|
||||||
|
default -> this.lastPresses.put(button, System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
if(this.lost || this.paused) return;
|
if(this.lost || this.paused) return;
|
||||||
|
|
||||||
switch(button) {
|
switch(button) {
|
||||||
case A -> this.currentTetromino.moveLeft();
|
case A -> this.moveLeft();
|
||||||
case S -> this.moveDown();
|
case S -> this.moveDown();
|
||||||
case D -> this.currentTetromino.moveRight();
|
case D -> this.moveRight();
|
||||||
case W -> this.hardDrop();
|
case W -> this.hardDrop(true);
|
||||||
case mouseLeft -> this.currentTetromino.rotate(false);
|
case mouseLeft -> this.rotate(false);
|
||||||
case mouseRight -> this.currentTetromino.rotate(true);
|
case mouseRight -> this.rotate(true);
|
||||||
case space -> this.switchHold();
|
case space -> this.switchHold();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,8 +86,7 @@ public class TetrisGame {
|
|||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
this.paused = false;
|
this.paused = false;
|
||||||
Scheduler scheduler = MinecraftServer.getSchedulerManager();
|
this.instance.scheduler().submitTask(() -> {
|
||||||
scheduler.submitTask(() -> {
|
|
||||||
if(this.lost) return TaskSchedule.stop();
|
if(this.lost) return TaskSchedule.stop();
|
||||||
int standardTickDelay = 40;
|
int standardTickDelay = 40;
|
||||||
if(this.isFast) standardTickDelay = 20;
|
if(this.isFast) standardTickDelay = 20;
|
||||||
@@ -112,7 +115,7 @@ public class TetrisGame {
|
|||||||
public void tick() {
|
public void tick() {
|
||||||
if(this.lost || this.paused) return;
|
if(this.lost || this.paused) return;
|
||||||
if(!this.currentTetromino.moveDown()) {
|
if(!this.currentTetromino.moveDown()) {
|
||||||
this.setActiveTetrominoDown();
|
this.scheduleTetrominoPlacement();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,33 +142,42 @@ public class TetrisGame {
|
|||||||
this.lost = true;
|
this.lost = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean moveDown() {
|
private void moveDown() {
|
||||||
if(!this.currentTetromino.moveDown()) {
|
if(!this.currentTetromino.moveDown()) {
|
||||||
this.setActiveTetrominoDown();
|
this.scheduleTetrominoPlacement();
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
this.score += 1;
|
this.score += 1;
|
||||||
this.updateInfo();
|
this.updateInfo();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hardDrop() {
|
private void moveLeft() {
|
||||||
|
if(this.currentTetromino.moveLeft()) this.stopTetrominoPlacementTask(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveRight() {
|
||||||
|
if(this.currentTetromino.moveRight()) this.stopTetrominoPlacementTask(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void rotate(boolean clockwise) {
|
||||||
|
if(this.currentTetromino.rotate(clockwise)) this.stopTetrominoPlacementTask(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void hardDrop(boolean addScore) {
|
||||||
if(!this.currentTetromino.moveDown()) {
|
if(!this.currentTetromino.moveDown()) {
|
||||||
this.setActiveTetrominoDown();
|
this.setActiveTetrominoDown();
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
this.score += 2;
|
do {
|
||||||
this.updateInfo();
|
if(addScore) this.score += 2;
|
||||||
while(this.currentTetromino.moveDown()) {
|
|
||||||
this.score += 2;
|
|
||||||
this.updateInfo();
|
this.updateInfo();
|
||||||
}
|
} while(this.currentTetromino.moveDown());
|
||||||
this.setActiveTetrominoDown();
|
this.setActiveTetrominoDown();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean switchHold() {
|
private void switchHold() {
|
||||||
if(!this.holdPossible) return false;
|
if(!this.holdPossible) return;
|
||||||
|
this.stopTetrominoPlacementTask(true);
|
||||||
|
|
||||||
Tetromino newCurrentTetromino;
|
Tetromino newCurrentTetromino;
|
||||||
if(this.holdTetromino == null) {
|
if(this.holdTetromino == null) {
|
||||||
@@ -189,7 +201,6 @@ public class TetrisGame {
|
|||||||
this.holdTetromino.setPosition(this.holdPosition.add(xChange, 0, 0));
|
this.holdTetromino.setPosition(this.holdPosition.add(xChange, 0, 0));
|
||||||
this.holdTetromino.drawAsEntities();
|
this.holdTetromino.drawAsEntities();
|
||||||
this.holdPossible = false;
|
this.holdPossible = false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateNextTetrominoes() {
|
private void updateNextTetrominoes() {
|
||||||
@@ -235,7 +246,32 @@ public class TetrisGame {
|
|||||||
this.sidebar.updateLineScore("2", this.level);
|
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);
|
||||||
|
return TaskSchedule.stop();
|
||||||
|
}, TaskSchedule.millis(500));
|
||||||
|
if(this.hardTetrominoPlacementTask == null || !this.hardTetrominoPlacementTask.isAlive())
|
||||||
|
this.hardTetrominoPlacementTask = this.instance.scheduler().scheduleTask(() -> {
|
||||||
|
this.hardDrop(false);
|
||||||
|
return TaskSchedule.stop();
|
||||||
|
}, TaskSchedule.millis(6000));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopTetrominoPlacementTask(boolean resetHard) {
|
||||||
|
if(this.tetrominoPlacementTask != null) {
|
||||||
|
this.tetrominoPlacementTask.cancel();
|
||||||
|
this.tetrominoPlacementTask = null;
|
||||||
|
}
|
||||||
|
if(resetHard && this.hardTetrominoPlacementTask != null) {
|
||||||
|
this.hardTetrominoPlacementTask.cancel();
|
||||||
|
this.hardTetrominoPlacementTask = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void setActiveTetrominoDown() {
|
private void setActiveTetrominoDown() {
|
||||||
|
this.stopTetrominoPlacementTask(true);
|
||||||
this.currentTetromino.removeOwnEntities();
|
this.currentTetromino.removeOwnEntities();
|
||||||
this.currentTetromino = this.nextTetrominoes.removeFirst();
|
this.currentTetromino = this.nextTetrominoes.removeFirst();
|
||||||
this.currentTetromino.remove();
|
this.currentTetromino.remove();
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class Tetromino {
|
|||||||
this.position = new Pos(newPosition.x(), newPosition.y(), newPosition.z());
|
this.position = new Pos(newPosition.x(), newPosition.y(), newPosition.z());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rotate(boolean clockwise) {
|
public boolean rotate(boolean clockwise) {
|
||||||
int[][] newShapeArray = this.getTurnedShapeArray(clockwise);
|
int[][] newShapeArray = this.getTurnedShapeArray(clockwise);
|
||||||
Orientation newOrientation = this.orientation.rotated(clockwise);
|
Orientation newOrientation = this.orientation.rotated(clockwise);
|
||||||
|
|
||||||
@@ -53,10 +53,11 @@ public class Tetromino {
|
|||||||
Pos candidate = new Pos(this.position.x() + k[0], this.position.y() + k[1], this.position.z());
|
Pos candidate = new Pos(this.position.x() + k[0], this.position.y() + k[1], this.position.z());
|
||||||
if(this.checkCollisionAndMove(candidate, newShapeArray)) {
|
if(this.checkCollisionAndMove(candidate, newShapeArray)) {
|
||||||
this.orientation = newOrientation;
|
this.orientation = newOrientation;
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean moveDown() {
|
public boolean moveDown() {
|
||||||
@@ -64,14 +65,14 @@ public class Tetromino {
|
|||||||
return this.checkCollisionAndMove(newPosition, this.shapeArray);
|
return this.checkCollisionAndMove(newPosition, this.shapeArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveLeft() {
|
public boolean moveLeft() {
|
||||||
Pos newPosition = this.position.sub(1, 0, 0);
|
Pos newPosition = this.position.sub(1, 0, 0);
|
||||||
this.checkCollisionAndMove(newPosition, this.shapeArray);
|
return this.checkCollisionAndMove(newPosition, this.shapeArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveRight() {
|
public boolean moveRight() {
|
||||||
Pos newPosition = this.position.add(1, 0, 0);
|
Pos newPosition = this.position.add(1, 0, 0);
|
||||||
this.checkCollisionAndMove(newPosition, this.shapeArray);
|
return this.checkCollisionAndMove(newPosition, this.shapeArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw() {
|
public void draw() {
|
||||||
|
|||||||
Reference in New Issue
Block a user