added timeout for tetris and display points at end of PointsWinScore
This commit is contained in:
		| @@ -51,7 +51,9 @@ public class StatelessGame extends Game { | ||||
|                 int timeLeft = timeLimit - timePlayed; | ||||
|                 switch (timeLeft) { | ||||
|                     case 60, 30, 10, 5, 4, 3, 2, 1 -> | ||||
|                             new ChatMessage(Icon.SCIENCE).appendStatic("Noch " + timeLeft + " Sekunden!").send(getPlayers()); | ||||
|                         new ChatMessage(Icon.SCIENCE).appendStatic("Noch " + timeLeft + " Sekunden!").send(getPlayers()); | ||||
|                     case 120 -> | ||||
|                         new ChatMessage(Icon.SCIENCE).appendStatic("Noch 2 Minuten!").send(getPlayers()); | ||||
|                 } | ||||
|  | ||||
|                 timePlayed++; | ||||
| @@ -100,7 +102,7 @@ public class StatelessGame extends Game { | ||||
|         countdownUnload(); | ||||
|     } | ||||
|  | ||||
|     private void countdownUnload() { | ||||
|     protected void countdownUnload() { | ||||
|         new TitleMessage(Duration.ofSeconds(1)).appendTranslated("score#done").send(getPlayers()); | ||||
|         scheduler().scheduleTask(this::unload, TaskSchedule.seconds(5), TaskSchedule.stop()); | ||||
|     } | ||||
|   | ||||
| @@ -18,16 +18,18 @@ import org.jetbrains.annotations.NotNull; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Random; | ||||
|  | ||||
| class Tetris extends StatelessGame { | ||||
|     private final Map<Player, TetrisGame> tetrisGames = new HashMap<>(); | ||||
|     private final int nextTetrominoesCount; | ||||
|     private final boolean isFast; | ||||
|     private final boolean hasCombat; | ||||
|     private boolean setTimeLimit = false; | ||||
|     private final long randomSeed; | ||||
|  | ||||
|     public Tetris(int nextTetrominoesCount, boolean isFast, boolean hasCombat) { | ||||
|         super(Dimension.THE_END.key, "Tetris", new PointsWinScore()); | ||||
| //        this.setGenerator(new CircularPlateTerrainGenerator(20)); | ||||
|  | ||||
|         eventNode() | ||||
|             .addListener(PlayerUseItemEvent.class, this::onPlayerInteract) | ||||
| @@ -37,6 +39,9 @@ class Tetris extends StatelessGame { | ||||
|         this.nextTetrominoesCount = nextTetrominoesCount; | ||||
|         this.isFast = isFast; | ||||
|         this.hasCombat = hasCombat; | ||||
|  | ||||
|         Random random = new Random(); | ||||
|         this.randomSeed = random.nextLong(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -54,6 +59,8 @@ class Tetris extends StatelessGame { | ||||
|  | ||||
|     @Override | ||||
|     protected void onStop() { | ||||
|         this.tetrisGames.forEach((player, tetrisGame) -> tetrisGame.loose()); | ||||
|         this.tetrisGames.forEach((player, tetrisGame) -> getScore().insertResult(player, tetrisGame.getScore())); | ||||
|         this.tetrisGames.forEach((player, tetrisGame) -> tetrisGame.sidebar.removeViewer(player)); | ||||
|     } | ||||
|  | ||||
| @@ -120,7 +127,11 @@ class Tetris extends StatelessGame { | ||||
|         if(tetrisGame.lost && event.getPlayer().getGameMode() != GameMode.SPECTATOR) { | ||||
|             event.getPlayer().setGameMode(GameMode.SPECTATOR); | ||||
|             getScore().insertResult(event.getPlayer(), tetrisGame.getScore()); | ||||
|             tetrisGame.sidebar.removeViewer(event.getPlayer()); | ||||
|  | ||||
|             if(!setTimeLimit) { | ||||
|                 this.setTimeLimit(120); | ||||
|                 setTimeLimit = true; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -136,7 +147,8 @@ class Tetris extends StatelessGame { | ||||
|                 Tetromino.Shape.J, | ||||
|                 this.nextTetrominoesCount, | ||||
|                 this.isFast, | ||||
|                 this.hasCombat | ||||
|                 this.hasCombat, | ||||
|                 this.randomSeed | ||||
|             )); | ||||
|             this.tetrisGames.get(p).generate(); | ||||
|             this.tetrisGames.forEach((player, tetrisGame) -> tetrisGame.updateOtherTetrisGames(this.tetrisGames.values().stream().toList())); | ||||
|   | ||||
| @@ -32,6 +32,7 @@ public class TetrisGame { | ||||
|     public Sidebar sidebar = new Sidebar(Component.text("Info:")); | ||||
|     private final Map<Button, Long> lastPresses = new HashMap<>(); | ||||
|     private final List<TetrisGame> otherTetrisGames = new ArrayList<>(); | ||||
|     private final Random random; | ||||
|  | ||||
|     public enum Button { | ||||
|         W, | ||||
| @@ -43,15 +44,16 @@ public class TetrisGame { | ||||
|         space | ||||
|     } | ||||
|  | ||||
|     public TetrisGame(StatelessGame instance, Pos lowerLeftCorner) { | ||||
|         this(instance, lowerLeftCorner, Tetromino.Shape.J, 3, false, false); | ||||
|     public TetrisGame(StatelessGame instance, Pos lowerLeftCorner, long randomSeed) { | ||||
|         this(instance, lowerLeftCorner, Tetromino.Shape.J, 3, false, false, randomSeed); | ||||
|     } | ||||
|  | ||||
|     public TetrisGame(StatelessGame instance, Pos lowerLeftCorner, Tetromino.Shape startTetrominoShape, int nextTetrominoesCount, boolean isfast, boolean hasCombat) { | ||||
|     public TetrisGame(StatelessGame instance, Pos lowerLeftCorner, Tetromino.Shape startTetrominoShape, int nextTetrominoesCount, boolean isfast, boolean hasCombat, long randomSeed) { | ||||
|         this.isFast = isfast; | ||||
|         this.hasCombat = hasCombat; | ||||
|         this.instance = instance; | ||||
|         this.playfield = new Playfield(lowerLeftCorner, this.instance, nextTetrominoesCount); | ||||
|         this.random = new Random(randomSeed); | ||||
|  | ||||
|         this.holdPosition = this.playfield.getHoldPosition(); | ||||
|         this.nextPosition = this.playfield.getNextPosition(); | ||||
| @@ -130,17 +132,22 @@ public class TetrisGame { | ||||
|     public void updateOtherTetrisGames(List<TetrisGame> tetrisGames) { | ||||
|         List<TetrisGame> games = new ArrayList<>(tetrisGames); | ||||
|         games.remove(this); | ||||
|         games.removeIf(tetrisGame -> tetrisGame.lost); | ||||
|         this.otherTetrisGames.clear(); | ||||
|         this.otherTetrisGames.addAll(games); | ||||
|     } | ||||
|  | ||||
|     public void getAttacked(int lines) { | ||||
|         if(this.hasCombat) { | ||||
|         if(this.hasCombat && !this.lost) { | ||||
|             this.attackingLines += lines; | ||||
|             this.playfield.updateAttackingLines(this.attackingLines); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void loose() { | ||||
|         this.lost = true; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     private boolean rotate(boolean clockwise) { | ||||
|         if(this.lost || this.paused) return false; | ||||
| @@ -223,7 +230,7 @@ public class TetrisGame { | ||||
|             for(Tetromino.Shape shape : Tetromino.Shape.values()) { | ||||
|                 this.tetrominoBag.add(new Tetromino(this.instance, shape)); | ||||
|             } | ||||
|             Collections.shuffle(this.tetrominoBag); | ||||
|             Collections.shuffle(this.tetrominoBag, this.random); | ||||
|         } | ||||
|  | ||||
|         if(!this.nextTetrominoes.isEmpty()) this.nextTetrominoes.forEach(Tetromino::remove); | ||||
| @@ -240,10 +247,6 @@ public class TetrisGame { | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     private void loose() { | ||||
|         this.lost = true; | ||||
|     } | ||||
|  | ||||
|     private void buildSidebar() { | ||||
|         this.sidebar.createLine(new Sidebar.ScoreboardLine( | ||||
|             "0", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user