docs: add javadocs for nearly everything
This commit is contained in:
@@ -15,25 +15,40 @@ public class Space {
|
||||
private int score;
|
||||
public int level;
|
||||
|
||||
/**
|
||||
* Space saves the current state of the game map
|
||||
*/
|
||||
public Space() {
|
||||
gameMap = new Color[14][8];
|
||||
gameMap = new Color[20][12];
|
||||
nextShape = generateNextShape();
|
||||
score = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn the queued tetromino piece and generate the next one
|
||||
*/
|
||||
public void spawnTetromino() {
|
||||
currentTetromino = new Tetromino(nextShape);
|
||||
nextShape = generateNextShape();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the currently controlled tetromino piece
|
||||
*/
|
||||
public Tetromino getCurrentTetromino() {
|
||||
return currentTetromino;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a randomly selected tetromino shape
|
||||
*/
|
||||
public TetrominoShape generateNextShape() {
|
||||
return TetrominoShape.values()[random.nextInt(TetrominoShape.values().length)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the queued tetromino shape
|
||||
*/
|
||||
public TetrominoShape getNextShape() {
|
||||
return nextShape;
|
||||
}
|
||||
@@ -46,6 +61,11 @@ public class Space {
|
||||
return gameMap.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the current state of the space to an array and includes the falling tetromino.
|
||||
*
|
||||
* @return a representation of the space with the falling tetromino baked in
|
||||
*/
|
||||
public Color[][] getGameMapWithTetromino() {
|
||||
Color[][] tempGameMap = new Color[gameMap.length][gameMap[0].length];
|
||||
for (int y = 0; y < tempGameMap.length; y++) {
|
||||
@@ -61,6 +81,11 @@ public class Space {
|
||||
return tempGameMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the current state of the space to an array.
|
||||
*
|
||||
* @return a representation of the space without the falling tetromino baked in
|
||||
*/
|
||||
public Color[][] getGameMap() {
|
||||
return gameMap;
|
||||
}
|
||||
@@ -69,6 +94,11 @@ public class Space {
|
||||
return score;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle line changes.
|
||||
* Once a line is fully completed, it is removed from the space, the lines above are moved down and the score is increased.
|
||||
* More lines completed at once increase the combo and therefore the score.
|
||||
*/
|
||||
public void onLinesChanged(Tetromino tetromino, int... lines) {
|
||||
int combo = 0;
|
||||
Set<Integer> completedLines = new TreeSet<>();
|
||||
|
||||
@@ -12,12 +12,20 @@ public class Tetromino {
|
||||
private Vec2i centerPos;
|
||||
private int fallLength = 0;
|
||||
|
||||
/**
|
||||
* This class handles the falling tetromino.
|
||||
* @param shape the tetromino's shape
|
||||
*/
|
||||
public Tetromino(TetrominoShape shape) {
|
||||
this.shape = shape;
|
||||
this.collision = shape.boundary;
|
||||
this.centerPos = Vec2i.of(Tetris.getSpace().getMapWidth()/2-1, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the tetromino vertically.
|
||||
* Upon hitting solid ground, the piece is released and {@link Space#onLinesChanged(Tetromino, int...)} is triggered.
|
||||
*/
|
||||
public boolean fall() {
|
||||
Vec2i newPos = centerPos.offset(Vec2i.of(0, 1));
|
||||
if (collidesVertically(newPos)) {
|
||||
@@ -37,6 +45,10 @@ public class Tetromino {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the tetromino horizontally.
|
||||
* Accounts for collision and may prevent movement.
|
||||
*/
|
||||
public void move(int xOffset) {
|
||||
Vec2i newPos = centerPos.offset(Vec2i.of(xOffset, 0));
|
||||
if (collidesHorizontally(newPos, xOffset)) {
|
||||
@@ -45,6 +57,11 @@ public class Tetromino {
|
||||
centerPos = newPos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks, whether the piece would overlap with a block that's already present on the map or would be out-of-bounds.
|
||||
* @param newPos the new position
|
||||
* @return true if collision is detected, false otherwise
|
||||
*/
|
||||
private boolean collidesVertically(Vec2i newPos) {
|
||||
if (Tetris.getSpace() == null) return false;
|
||||
|
||||
@@ -62,6 +79,12 @@ public class Tetromino {
|
||||
return collides;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks, whether the piece would overlap with a block that's already present on the map or would be out-of-bounds.
|
||||
* @param newPos the new position
|
||||
* @param xOffset the direction of movement (-1 == left; 1 == right)
|
||||
* @return true if collision is detected, false otherwise
|
||||
*/
|
||||
private boolean collidesHorizontally(Vec2i newPos, int xOffset) {
|
||||
if (Tetris.getSpace() == null) return false;
|
||||
|
||||
@@ -78,6 +101,10 @@ public class Tetromino {
|
||||
return collides;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate the tetromino 90 degrees clockwise.
|
||||
* Fails if the piece would collide vertically or horizontally.
|
||||
*/
|
||||
public void rotate() {
|
||||
int M = collision.length;
|
||||
int N = collision[0].length;
|
||||
@@ -98,6 +125,10 @@ public class Tetromino {
|
||||
else this.centerPos = centerPos.offset(Vec2i.of(offset, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the tetromino piece to its world-view position.
|
||||
* @return an array representing the line
|
||||
*/
|
||||
public Color[] getLine(int line) {
|
||||
Color[] l = new Color[Tetris.getSpace().getMapWidth()];
|
||||
for (int i = 0; i < l.length; i++) {
|
||||
@@ -111,6 +142,9 @@ public class Tetromino {
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the tetromino down until it collides.
|
||||
*/
|
||||
public void fallToBottom() {
|
||||
while (true) {
|
||||
if (!fall()) break;
|
||||
|
||||
@@ -5,39 +5,38 @@ import java.awt.Color;
|
||||
public enum TetrominoShape {
|
||||
SQUARE(new int[][]{
|
||||
{1, 1},
|
||||
{1, 2}
|
||||
{1, 1}
|
||||
}, Color.YELLOW),
|
||||
LINE(new int[][]{
|
||||
{1},
|
||||
{2},
|
||||
{1},
|
||||
{1},
|
||||
{1}
|
||||
}, Color.BLUE),
|
||||
T(new int[][]{
|
||||
{0, 1, 0},
|
||||
{1, 2, 1}
|
||||
{1, 1, 1}
|
||||
}, Color.RED),
|
||||
L_LEFT(new int[][]{
|
||||
{0, 1},
|
||||
{0, 2},
|
||||
{0, 1},
|
||||
{1, 1}
|
||||
}, Color.MAGENTA),
|
||||
L_RIGHT(new int[][]{
|
||||
{1, 0},
|
||||
{2, 0},
|
||||
{1, 0},
|
||||
{1, 1}
|
||||
}, Color.GREEN),
|
||||
ZAP_LEFT(new int[][]{
|
||||
{0, 1},
|
||||
{1, 2},
|
||||
{1, 1},
|
||||
{1, 0}
|
||||
}, Color.CYAN),
|
||||
ZAP_RIGHT(new int[][]{
|
||||
{1, 0},
|
||||
{2, 1},
|
||||
{1, 1},
|
||||
{0, 1}
|
||||
}, Color.ORANGE);
|
||||
;
|
||||
|
||||
final int[][] boundary;
|
||||
final Color color;
|
||||
|
||||
Reference in New Issue
Block a user