centered hold and next tetrominoes
This commit is contained in:
parent
3c25f5f079
commit
f838317af0
src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game
@ -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.util.BatchUtil;
|
||||
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.block.Block;
|
||||
|
||||
@ -106,19 +104,6 @@ public class Playfield {
|
||||
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) {
|
||||
int xPosMissing = random.nextInt(1, 10);
|
||||
|
||||
|
45
src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java
45
src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java
@ -58,9 +58,9 @@ public class TetrisGame {
|
||||
this.tetrominoSpawnPosition = this.playfield.getTetrominoSpawnPosition();
|
||||
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++) {
|
||||
this.getNextTetromino();
|
||||
this.updateNextTetrominoes();
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,6 +97,16 @@ public class TetrisGame {
|
||||
this.tick();
|
||||
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() {
|
||||
@ -182,31 +192,36 @@ public class TetrisGame {
|
||||
if(this.holdTetromino == null) {
|
||||
newCurrentTetromino = this.nextTetrominoes.removeFirst();
|
||||
newCurrentTetromino.remove();
|
||||
this.getNextTetromino();
|
||||
this.updateNextTetrominoes();
|
||||
} else {
|
||||
newCurrentTetromino = this.holdTetromino;
|
||||
this.holdTetromino.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.setPosition(this.tetrominoSpawnPosition);
|
||||
this.currentTetromino.draw();
|
||||
if(!this.currentTetromino.moveDown()) loose();
|
||||
|
||||
this.holdTetromino.setPosition(this.holdPosition);
|
||||
this.holdTetromino.draw(false);
|
||||
double xChange;
|
||||
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;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private Tetromino getNextTetromino() {
|
||||
private void updateNextTetrominoes() {
|
||||
if(this.tetrominoBag.isEmpty()) {
|
||||
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);
|
||||
}
|
||||
@ -215,11 +230,14 @@ public class TetrisGame {
|
||||
Tetromino newTetromino = this.tetrominoBag.removeFirst();
|
||||
this.nextTetrominoes.add(newTetromino);
|
||||
this.nextTetrominoes.forEach(tetromino -> {
|
||||
tetromino.setPosition(this.nextPosition.sub(0, 4*this.nextTetrominoes.indexOf(tetromino), 0));
|
||||
tetromino.draw(false);
|
||||
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();
|
||||
});
|
||||
|
||||
return newTetromino;
|
||||
}
|
||||
|
||||
private void loose() {
|
||||
@ -251,9 +269,10 @@ public class TetrisGame {
|
||||
}
|
||||
|
||||
private void setActiveTetrominoDown() {
|
||||
this.currentTetromino.removeOwnEntities();
|
||||
this.currentTetromino = this.nextTetrominoes.removeFirst();
|
||||
this.currentTetromino.remove();
|
||||
this.getNextTetromino();
|
||||
this.updateNextTetrominoes();
|
||||
|
||||
int removedLines = this.playfield.removeFullLines();
|
||||
int combatLines = 0;
|
||||
|
@ -6,6 +6,7 @@ import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.entity.metadata.other.FallingBlockMeta;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.tag.Tag;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -15,11 +16,11 @@ import java.util.UUID;
|
||||
public class Tetromino {
|
||||
public final Shape shape;
|
||||
private final StatelessGame instance;
|
||||
private final Playfield playfield;
|
||||
private Pos position;
|
||||
private int[][] shapeArray;
|
||||
private final static EntityType ghostEntityType = EntityType.FALLING_BLOCK;
|
||||
private final UUID uuid;
|
||||
private final static Tag<String> uuidTag = Tag.String("uuid");
|
||||
|
||||
public enum Shape {
|
||||
I,
|
||||
@ -31,10 +32,9 @@ public class Tetromino {
|
||||
Z
|
||||
}
|
||||
|
||||
public Tetromino(StatelessGame instance, Shape shape, Playfield playfield) {
|
||||
public Tetromino(StatelessGame instance, Shape shape) {
|
||||
this.instance = instance;
|
||||
this.shape = shape;
|
||||
this.playfield = playfield;
|
||||
this.uuid = UUID.randomUUID();
|
||||
|
||||
switch (this.shape) {
|
||||
@ -82,7 +82,6 @@ public class Tetromino {
|
||||
|
||||
public void draw(boolean withGhost) {
|
||||
if(withGhost) {
|
||||
this.playfield.removeEntity(ghostEntityType);
|
||||
Pos ghostPos = this.position;
|
||||
while (!checkCollision(ghostPos.sub(0, 1, 0), this.shapeArray)) {
|
||||
ghostPos = ghostPos.sub(0, 1, 0);
|
||||
@ -94,6 +93,7 @@ public class Tetromino {
|
||||
// ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock());
|
||||
ghostBlock.setNoGravity(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(1, 0, 1));
|
||||
});
|
||||
@ -105,16 +105,17 @@ public class Tetromino {
|
||||
public void drawAsEntities() {
|
||||
getBlockPositions().forEach(pos -> {
|
||||
Entity ghostBlock = new Entity(ghostEntityType);
|
||||
((FallingBlockMeta) ghostBlock.getEntityMeta()).setBlock(this.getGhostBlock());
|
||||
((FallingBlockMeta) ghostBlock.getEntityMeta()).setBlock(this.getColoredBlock());
|
||||
// ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock());
|
||||
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.sub(positionChange).add(1, 0, 1));
|
||||
});
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
this.removeOwnEntities();
|
||||
this.getBlockPositions().forEach(pos -> this.instance.setBlock(pos, Block.AIR));
|
||||
}
|
||||
|
||||
@ -133,6 +134,16 @@ public class Tetromino {
|
||||
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() {
|
||||
Block returnBlock;
|
||||
@ -170,12 +181,8 @@ public class Tetromino {
|
||||
}
|
||||
|
||||
private boolean isPartOfTetromino(Pos position) {
|
||||
for(Pos blockPosition : getBlockPositions()) {
|
||||
if(position.sameBlock(blockPosition)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return this.getBlockPositions().stream()
|
||||
.anyMatch(pos -> pos.equals(position));
|
||||
}
|
||||
|
||||
private List<Pos> getBlockPositions() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user