Class Tetromino
- java.lang.Object
-
- hu.klenium.tetris.logic.tetromino.Tetromino
-
public class Tetromino extends java.lang.Object
A shape in Tetris game, which can be controlled by the player.
A tetromino is a geometric shape composed of four squares, connected orthogonally. The tetromino is part of one board. It can be moved left, right, or down, and can be rotated to right by the player. These operations may fail if the board is not empty at some point. Tetrominoes can't overlap previously fallen tetromino parts located on the board. When the tetromino can't move down any more, it's added to the board's table, and a new Tetromino is created.
-
-
Field Summary
Fields Modifier and Type Field Description private Board
board
The board which this tetromino belongs to.private TetrominoData[]
parts
Data of the tetromino in each rotation.private Point
position
Coordinates of this tetromino.private int
rotation
Current rotation of the tetromino.private int
type
The type determines the shape of the tetromino.
-
Method Summary
Modifier and Type Method Description private boolean
canPushBy(Point delta)
Checks that the thetromino can be pushed away by the given values or not.TetrominoData
getCurrentData()
Gives access to the tetromino's data.Point
getPosition()
Tells the tetromino's position over the board's grid.int
getType()
Tells this tetromino's type.boolean
moveDown()
Tries to move the tetromino down by one.boolean
moveLeft()
Tries to move the tetromino left by one.boolean
moveRight()
Tries to move the tetromino right by one.boolean
moveToInitialPos()
Tries to set the tetromino's initial position after it is created.boolean
rotateRight()
Tries to rotate the tetromino by 90 degrees to right.private boolean
tryPush(Point delta)
Check if moving is possible, and if it is, updates the tetromino's state.
-
-
-
Field Detail
-
board
private final Board board
The board which this tetromino belongs to.
-
type
private final int type
The type determines the shape of the tetromino. Each type has a different shape, they has different views.
This is the index ofTetrominoDataFactory.rawData
.
-
parts
private final TetrominoData[] parts
Data of the tetromino in each rotation. The data contains the tetromino's cells in that rotation, and the bounding box's size.
The index is the rotation. It's length can be less than 4 if two or more rotations have the same data.
-
position
private Point position
Coordinates of this tetromino. It points to the top-left side of the current bounding box (stored inparts
).
-
rotation
private int rotation
Current rotation of the tetromino.
Its value can be bettween 0-3. It's used to indexparts
.
-
-
Constructor Detail
-
Tetromino
public Tetromino(int type, Board board)
Initializes a new falling tetromino.- Parameters:
type
- Type of this tetromino.board
- The game's board which this tetromino belongs to.
-
-
Method Detail
-
getCurrentData
public TetrominoData getCurrentData()
Gives access to the tetromino's data. It contaions each parts, and their bounding box.- Returns:
- Parts in the current rotation.
- See Also:
parts
-
getPosition
public Point getPosition()
Tells the tetromino's position over the board's grid.- Returns:
- The top-left coordinate of the tetromino's bounding box.
- See Also:
position
-
getType
public int getType()
Tells this tetromino's type. Used to display the falling tetromino, and create board cells.- Returns:
- The tetromino's type.
- See Also:
type
-
moveToInitialPos
public boolean moveToInitialPos()
Tries to set the tetromino's initial position after it is created. This position is the board's top-center point.- Returns:
- True if the tetromino could be moved to the specificed position, false otherwise (it also causes game over).
-
rotateRight
public boolean rotateRight()
Tries to rotate the tetromino by 90 degrees to right. If it isn't possible, it'll try to move the tetromino away horizontally, so that if the tetromino is near the board's edge, the player still can perform the rotation easily.- Returns:
- True if the rotating was successful, false otherwise.
-
moveRight
public boolean moveRight()
Tries to move the tetromino right by one.- Returns:
- True if the moving was successful, false otherwise.
-
moveLeft
public boolean moveLeft()
Tries to move the tetromino left by one.- Returns:
- True if the moving was successful, false otherwise.
-
moveDown
public boolean moveDown()
Tries to move the tetromino down by one.- Returns:
- True if the moving was successful, false otherwise.
-
tryPush
private boolean tryPush(Point delta)
Check if moving is possible, and if it is, updates the tetromino's state.- Parameters:
delta
- Difference bettween the current and the target position.- Returns:
- True if the moving was successful, false otherwise.
-
canPushBy
private boolean canPushBy(Point delta)
Checks that the thetromino can be pushed away by the given values or not.- Parameters:
delta
- Difference bettween the current and the target position.- Returns:
- True if moving to position current+delta if possible, false otherwise.
-
-