added score above playfield
This commit is contained in:
parent
bef697e5fc
commit
e397d69d7a
@ -143,7 +143,7 @@ class Tetris extends StatelessGame {
|
|||||||
if(this.tetrisGames.get(p) == null) {
|
if(this.tetrisGames.get(p) == null) {
|
||||||
this.tetrisGames.put(p, new TetrisGame(
|
this.tetrisGames.put(p, new TetrisGame(
|
||||||
this,
|
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,
|
Tetromino.Shape.J,
|
||||||
this.nextTetrominoesCount,
|
this.nextTetrominoesCount,
|
||||||
this.isFast,
|
this.isFast,
|
||||||
|
@ -5,6 +5,7 @@ import eu.mhsl.minenet.minigames.util.BatchUtil;
|
|||||||
import net.minestom.server.coordinate.Pos;
|
import net.minestom.server.coordinate.Pos;
|
||||||
import net.minestom.server.instance.batch.AbsoluteBlockBatch;
|
import net.minestom.server.instance.batch.AbsoluteBlockBatch;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ public class Playfield {
|
|||||||
private final Pos lowerLeftCorner;
|
private final Pos lowerLeftCorner;
|
||||||
private final StatelessGame instance;
|
private final StatelessGame instance;
|
||||||
private final static int height = 22;
|
private final static int height = 22;
|
||||||
|
private final static Block scoreBlock = Block.STONE;
|
||||||
private final int nextTetrominoesCount;
|
private final int nextTetrominoesCount;
|
||||||
private final Random random;
|
private final Random random;
|
||||||
|
|
||||||
@ -24,7 +26,7 @@ public class Playfield {
|
|||||||
|
|
||||||
public Pos getPlayerSpawnPosition() {
|
public Pos getPlayerSpawnPosition() {
|
||||||
// return this.lowerLeftCorner.add(6, 9+((double) 3/16), 20).withView(180, 0);
|
// 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() {
|
public Pos getTetrominoSpawnPosition() {
|
||||||
@ -36,7 +38,11 @@ public class Playfield {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Pos getNextPosition() {
|
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() {
|
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() {
|
private void moveAllLinesUp() {
|
||||||
for (int y = height + 3; y > 1; y--) {
|
for (int y = height + 3; y > 1; y--) {
|
||||||
|
@ -101,6 +101,7 @@ public class TetrisGame {
|
|||||||
return TaskSchedule.tick(Math.round((float) standardTickDelay /this.level));
|
return TaskSchedule.tick(Math.round((float) standardTickDelay /this.level));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.updateInfo();
|
||||||
this.nextTetrominoes.forEach(tetromino -> {
|
this.nextTetrominoes.forEach(tetromino -> {
|
||||||
double xChange;
|
double xChange;
|
||||||
switch (tetromino.shape) {
|
switch (tetromino.shape) {
|
||||||
@ -172,7 +173,7 @@ public class TetrisGame {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
this.score += 1;
|
this.score += 1;
|
||||||
this.updateSidebar();
|
this.updateInfo();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,10 +184,10 @@ public class TetrisGame {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
this.score += 2;
|
this.score += 2;
|
||||||
this.updateSidebar();
|
this.updateInfo();
|
||||||
while(this.currentTetromino.moveDown()) {
|
while(this.currentTetromino.moveDown()) {
|
||||||
this.score += 2;
|
this.score += 2;
|
||||||
this.updateSidebar();
|
this.updateInfo();
|
||||||
}
|
}
|
||||||
this.setActiveTetrominoDown();
|
this.setActiveTetrominoDown();
|
||||||
return true;
|
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("0", this.score);
|
||||||
this.sidebar.updateLineScore("1", this.lines);
|
this.sidebar.updateLineScore("1", this.lines);
|
||||||
this.sidebar.updateLineScore("2", this.level);
|
this.sidebar.updateLineScore("2", this.level);
|
||||||
@ -332,7 +334,7 @@ public class TetrisGame {
|
|||||||
this.level = (int) Math.floor((double) this.lines / 10) + 1;
|
this.level = (int) Math.floor((double) this.lines / 10) + 1;
|
||||||
this.holdPossible = true;
|
this.holdPossible = true;
|
||||||
|
|
||||||
this.updateSidebar();
|
this.updateInfo();
|
||||||
|
|
||||||
this.currentTetromino.setPosition(this.tetrominoSpawnPosition);
|
this.currentTetromino.setPosition(this.tetrominoSpawnPosition);
|
||||||
this.currentTetromino.draw();
|
this.currentTetromino.draw();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user