centered hold and next tetrominoes

This commit is contained in:
Lars Neuhaus 2024-10-23 14:53:07 +02:00
parent 3c25f5f079
commit f838317af0
3 changed files with 51 additions and 40 deletions

View File

@ -3,8 +3,6 @@ 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 eu.mhsl.minenet.minigames.util.BatchUtil; import eu.mhsl.minenet.minigames.util.BatchUtil;
import net.minestom.server.coordinate.Pos; import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType;
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;
@ -106,19 +104,6 @@ public class Playfield {
return removedLinesCounter; return removedLinesCounter;
} }
public void removeEntity(EntityType entityType) {
this.instance.getEntities().stream()
.filter(entity -> entity.getEntityType().equals(entityType))
.filter(entity -> {
Pos position = entity.getPosition();
if(position.x() > this.lowerLeftCorner.x() && position.y() > this.lowerLeftCorner.y()) {
return position.x() < this.lowerLeftCorner.x() + 11 && position.y() < this.lowerLeftCorner.y() + height;
}
return false;
})
.forEach(Entity::remove);
}
public void addLines(int lines) { public void addLines(int lines) {
int xPosMissing = random.nextInt(1, 10); int xPosMissing = random.nextInt(1, 10);

View File

@ -58,9 +58,9 @@ public class TetrisGame {
this.tetrominoSpawnPosition = this.playfield.getTetrominoSpawnPosition(); this.tetrominoSpawnPosition = this.playfield.getTetrominoSpawnPosition();
this.buildSidebar(); this.buildSidebar();
this.currentTetromino = new Tetromino(this.instance, startTetrominoShape, this.playfield); this.currentTetromino = new Tetromino(this.instance, startTetrominoShape);
for (int i = 0; i < nextTetrominoesCount; i++) { for (int i = 0; i < nextTetrominoesCount; i++) {
this.getNextTetromino(); this.updateNextTetrominoes();
} }
} }
@ -97,6 +97,16 @@ public class TetrisGame {
this.tick(); this.tick();
return TaskSchedule.tick(Math.round((float) standardTickDelay /this.level)); return TaskSchedule.tick(Math.round((float) standardTickDelay /this.level));
}); });
this.nextTetrominoes.forEach(tetromino -> {
double xChange;
switch (tetromino.shape) {
case O, I -> xChange = 0;
case null, default -> xChange = -0.5;
}
tetromino.setPosition(this.nextPosition.sub(xChange, 4*this.nextTetrominoes.indexOf(tetromino), 0));
tetromino.drawAsEntities();
});
} }
public void generate() { public void generate() {
@ -182,31 +192,36 @@ public class TetrisGame {
if(this.holdTetromino == null) { if(this.holdTetromino == null) {
newCurrentTetromino = this.nextTetrominoes.removeFirst(); newCurrentTetromino = this.nextTetrominoes.removeFirst();
newCurrentTetromino.remove(); newCurrentTetromino.remove();
this.getNextTetromino(); this.updateNextTetrominoes();
} else { } else {
newCurrentTetromino = this.holdTetromino; newCurrentTetromino = this.holdTetromino;
this.holdTetromino.remove(); this.holdTetromino.remove();
} }
this.currentTetromino.remove(); this.currentTetromino.remove();
this.holdTetromino = new Tetromino(this.instance, this.currentTetromino.shape, this.playfield); this.holdTetromino = new Tetromino(this.instance, this.currentTetromino.shape);
this.currentTetromino = newCurrentTetromino; this.currentTetromino = newCurrentTetromino;
this.currentTetromino.setPosition(this.tetrominoSpawnPosition); this.currentTetromino.setPosition(this.tetrominoSpawnPosition);
this.currentTetromino.draw(); this.currentTetromino.draw();
if(!this.currentTetromino.moveDown()) loose(); if(!this.currentTetromino.moveDown()) loose();
this.holdTetromino.setPosition(this.holdPosition); double xChange;
this.holdTetromino.draw(false); switch (this.holdTetromino.shape) {
case O, I -> xChange = 0;
case null, default -> xChange = 0.5;
}
this.holdTetromino.setPosition(this.holdPosition.add(xChange, 0, 0));
this.holdTetromino.drawAsEntities();
this.holdPossible = false; this.holdPossible = false;
return true; return true;
} }
private Tetromino getNextTetromino() { private void updateNextTetrominoes() {
if(this.tetrominoBag.isEmpty()) { if(this.tetrominoBag.isEmpty()) {
for(Tetromino.Shape shape : Tetromino.Shape.values()) { for(Tetromino.Shape shape : Tetromino.Shape.values()) {
this.tetrominoBag.add(new Tetromino(this.instance, shape, this.playfield)); this.tetrominoBag.add(new Tetromino(this.instance, shape));
} }
Collections.shuffle(this.tetrominoBag); Collections.shuffle(this.tetrominoBag);
} }
@ -215,11 +230,14 @@ public class TetrisGame {
Tetromino newTetromino = this.tetrominoBag.removeFirst(); Tetromino newTetromino = this.tetrominoBag.removeFirst();
this.nextTetrominoes.add(newTetromino); this.nextTetrominoes.add(newTetromino);
this.nextTetrominoes.forEach(tetromino -> { this.nextTetrominoes.forEach(tetromino -> {
tetromino.setPosition(this.nextPosition.sub(0, 4*this.nextTetrominoes.indexOf(tetromino), 0)); double xChange;
tetromino.draw(false); switch (tetromino.shape) {
case O, I -> xChange = 0;
case null, default -> xChange = -0.5;
}
tetromino.setPosition(this.nextPosition.sub(xChange, 4*this.nextTetrominoes.indexOf(tetromino), 0));
tetromino.drawAsEntities();
}); });
return newTetromino;
} }
private void loose() { private void loose() {
@ -251,9 +269,10 @@ public class TetrisGame {
} }
private void setActiveTetrominoDown() { private void setActiveTetrominoDown() {
this.currentTetromino.removeOwnEntities();
this.currentTetromino = this.nextTetrominoes.removeFirst(); this.currentTetromino = this.nextTetrominoes.removeFirst();
this.currentTetromino.remove(); this.currentTetromino.remove();
this.getNextTetromino(); this.updateNextTetrominoes();
int removedLines = this.playfield.removeFullLines(); int removedLines = this.playfield.removeFullLines();
int combatLines = 0; int combatLines = 0;

View File

@ -6,6 +6,7 @@ import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType; import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.metadata.other.FallingBlockMeta; import net.minestom.server.entity.metadata.other.FallingBlockMeta;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
import net.minestom.server.tag.Tag;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -15,11 +16,11 @@ import java.util.UUID;
public class Tetromino { public class Tetromino {
public final Shape shape; public final Shape shape;
private final StatelessGame instance; private final StatelessGame instance;
private final Playfield playfield;
private Pos position; private Pos position;
private int[][] shapeArray; private int[][] shapeArray;
private final static EntityType ghostEntityType = EntityType.FALLING_BLOCK; private final static EntityType ghostEntityType = EntityType.FALLING_BLOCK;
private final UUID uuid; private final UUID uuid;
private final static Tag<String> uuidTag = Tag.String("uuid");
public enum Shape { public enum Shape {
I, I,
@ -31,10 +32,9 @@ public class Tetromino {
Z Z
} }
public Tetromino(StatelessGame instance, Shape shape, Playfield playfield) { public Tetromino(StatelessGame instance, Shape shape) {
this.instance = instance; this.instance = instance;
this.shape = shape; this.shape = shape;
this.playfield = playfield;
this.uuid = UUID.randomUUID(); this.uuid = UUID.randomUUID();
switch (this.shape) { switch (this.shape) {
@ -82,7 +82,6 @@ public class Tetromino {
public void draw(boolean withGhost) { public void draw(boolean withGhost) {
if(withGhost) { if(withGhost) {
this.playfield.removeEntity(ghostEntityType);
Pos ghostPos = this.position; Pos ghostPos = this.position;
while (!checkCollision(ghostPos.sub(0, 1, 0), this.shapeArray)) { while (!checkCollision(ghostPos.sub(0, 1, 0), this.shapeArray)) {
ghostPos = ghostPos.sub(0, 1, 0); ghostPos = ghostPos.sub(0, 1, 0);
@ -94,6 +93,7 @@ public class Tetromino {
// ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock()); // ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock());
ghostBlock.setNoGravity(true); ghostBlock.setNoGravity(true);
ghostBlock.setGlowing(true); ghostBlock.setGlowing(true);
ghostBlock.setTag(uuidTag, this.uuid.toString());
ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(0.5, 0, 0.5)); ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(0.5, 0, 0.5));
// ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(1, 0, 1)); // ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(1, 0, 1));
}); });
@ -105,16 +105,17 @@ public class Tetromino {
public void drawAsEntities() { public void drawAsEntities() {
getBlockPositions().forEach(pos -> { getBlockPositions().forEach(pos -> {
Entity ghostBlock = new Entity(ghostEntityType); Entity ghostBlock = new Entity(ghostEntityType);
((FallingBlockMeta) ghostBlock.getEntityMeta()).setBlock(this.getGhostBlock()); ((FallingBlockMeta) ghostBlock.getEntityMeta()).setBlock(this.getColoredBlock());
// ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock()); // ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock());
ghostBlock.setNoGravity(true); ghostBlock.setNoGravity(true);
ghostBlock.setGlowing(true); ghostBlock.setTag(uuidTag, this.uuid.toString());
ghostBlock.setInstance(this.instance, pos.add(0.5, 0, 0.5)); ghostBlock.setInstance(this.instance, pos.add(0.5, 0, 0.5));
// ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(1, 0, 1)); // ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(1, 0, 1));
}); });
} }
public void remove() { public void remove() {
this.removeOwnEntities();
this.getBlockPositions().forEach(pos -> this.instance.setBlock(pos, Block.AIR)); this.getBlockPositions().forEach(pos -> this.instance.setBlock(pos, Block.AIR));
} }
@ -133,6 +134,16 @@ public class Tetromino {
return returnBlock; return returnBlock;
} }
public void removeOwnEntities() {
this.instance.getEntities().stream()
.filter(entity -> {
String tagValue = entity.getTag(uuidTag);
if(tagValue != null) return entity.getTag(uuidTag).equals(this.uuid.toString());
return false;
})
.forEach(Entity::remove);
}
private Block getGhostBlock() { private Block getGhostBlock() {
Block returnBlock; Block returnBlock;
@ -170,12 +181,8 @@ public class Tetromino {
} }
private boolean isPartOfTetromino(Pos position) { private boolean isPartOfTetromino(Pos position) {
for(Pos blockPosition : getBlockPositions()) { return this.getBlockPositions().stream()
if(position.sameBlock(blockPosition)) { .anyMatch(pos -> pos.equals(position));
return true;
}
}
return false;
} }
private List<Pos> getBlockPositions() { private List<Pos> getBlockPositions() {