added score above playfield

This commit is contained in:
Lars Neuhaus 2024-10-23 22:43:14 +02:00
parent bef697e5fc
commit e397d69d7a
3 changed files with 62 additions and 8 deletions

View File

@ -143,7 +143,7 @@ class Tetris extends StatelessGame {
if(this.tetrisGames.get(p) == null) {
this.tetrisGames.put(p, new TetrisGame(
this,
getSpawn().sub(6, 8, 15).add(this.tetrisGames.size()*24, 0, 0),
getSpawn().sub(6, 8, 15).add(this.tetrisGames.size()*30, 0, 0),
Tetromino.Shape.J,
this.nextTetrominoesCount,
this.isFast,

View File

@ -5,6 +5,7 @@ import eu.mhsl.minenet.minigames.util.BatchUtil;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.instance.batch.AbsoluteBlockBatch;
import net.minestom.server.instance.block.Block;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Random;
@ -12,6 +13,7 @@ public class Playfield {
private final Pos lowerLeftCorner;
private final StatelessGame instance;
private final static int height = 22;
private final static Block scoreBlock = Block.STONE;
private final int nextTetrominoesCount;
private final Random random;
@ -24,7 +26,7 @@ public class Playfield {
public Pos getPlayerSpawnPosition() {
// return this.lowerLeftCorner.add(6, 9+((double) 3/16), 20).withView(180, 0);
return this.lowerLeftCorner.add(6, 9, 20).withView(180, 0);
return this.lowerLeftCorner.add(6, 12, 25).withView(180, 0);
}
public Pos getTetrominoSpawnPosition() {
@ -36,7 +38,11 @@ public class Playfield {
}
public Pos getNextPosition() {
return this.lowerLeftCorner.add(14, 18, 1);
return this.lowerLeftCorner.add(15, 18, 1);
}
public Pos getScorePosition() {
return this.lowerLeftCorner.add(-5, height+3, 0);
}
public void generate() {
@ -130,6 +136,52 @@ public class Playfield {
}
}
public void updateScore(int score) {
this.removeDigits();
String scoreString = String.valueOf(score);
char[] characters = scoreString.toCharArray();
ArrayUtils.reverse(characters);
for(int i = 6; i > 0; i--) {
char digit;
if(i <= characters.length) {
digit = characters[i-1];
} else {
digit = '0';
}
this.displayDigit(digit, 6-i);
}
}
private void displayDigit(char digit, int positionFromLeft) {
int[][] digitArray;
switch (digit) {
case '1' -> digitArray = new int[][]{{0,0,1},{0,1,1},{0,0,1},{0,0,1},{0,0,1}};
case '2' -> digitArray = new int[][]{{1,1,1},{0,0,1},{1,1,1},{1,0,0},{1,1,1}};
case '3' -> digitArray = new int[][]{{1,1,1},{0,0,1},{0,1,1},{0,0,1},{1,1,1}};
case '4' -> digitArray = new int[][]{{1,0,1},{1,0,1},{1,1,1},{0,0,1},{0,0,1}};
case '5' -> digitArray = new int[][]{{1,1,1},{1,0,0},{1,1,1},{0,0,1},{1,1,1}};
case '6' -> digitArray = new int[][]{{1,1,1},{1,0,0},{1,1,1},{1,0,1},{1,1,1}};
case '7' -> digitArray = new int[][]{{1,1,1},{0,0,1},{0,1,0},{0,1,0},{0,1,0}};
case '8' -> digitArray = new int[][]{{1,1,1},{1,0,1},{1,1,1},{1,0,1},{1,1,1}};
case '9' -> digitArray = new int[][]{{1,1,1},{1,0,1},{1,1,1},{0,0,1},{1,1,1}};
default -> digitArray = new int[][]{{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}};
}
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 5; y++) {
if(digitArray[4-y][x] == 1) this.instance.setBlock(this.getScorePosition().add(positionFromLeft*4+x, y, 0), scoreBlock);
}
}
}
private void removeDigits() {
for (int x = 0; x < 4 * 6; x++) {
for (int y = 0; y < 5; y++) {
this.instance.setBlock(this.getScorePosition().add(x, y, 0), Block.AIR);
}
}
}
private void moveAllLinesUp() {
for (int y = height + 3; y > 1; y--) {

View File

@ -101,6 +101,7 @@ public class TetrisGame {
return TaskSchedule.tick(Math.round((float) standardTickDelay /this.level));
});
this.updateInfo();
this.nextTetrominoes.forEach(tetromino -> {
double xChange;
switch (tetromino.shape) {
@ -172,7 +173,7 @@ public class TetrisGame {
return false;
}
this.score += 1;
this.updateSidebar();
this.updateInfo();
return true;
}
@ -183,10 +184,10 @@ public class TetrisGame {
return false;
}
this.score += 2;
this.updateSidebar();
this.updateInfo();
while(this.currentTetromino.moveDown()) {
this.score += 2;
this.updateSidebar();
this.updateInfo();
}
this.setActiveTetrominoDown();
return true;
@ -266,7 +267,8 @@ public class TetrisGame {
));
}
private void updateSidebar() {
private void updateInfo() {
this.playfield.updateScore(this.score);
this.sidebar.updateLineScore("0", this.score);
this.sidebar.updateLineScore("1", this.lines);
this.sidebar.updateLineScore("2", this.level);
@ -332,7 +334,7 @@ public class TetrisGame {
this.level = (int) Math.floor((double) this.lines / 10) + 1;
this.holdPossible = true;
this.updateSidebar();
this.updateInfo();
this.currentTetromino.setPosition(this.tetrominoSpawnPosition);
this.currentTetromino.draw();