Reputation: 12718
I'm making Tetris in Java and am working on rotating a piece.
To start, I was just rotating the bar-shaped piece.
I feel the way I'm doing it now is not only buggy, but complete overkill. But I'm not sure how else to go about it.
Firstly, I have a keylistener that sets int[] rotatedCoords
equal to calcRotation("right")
... If rotate Left, the rotationsCounter+=1;
will be decremented instead.
if (keycode == KeyEvent.VK_D) {
int[] rotatedCoords = calcRotation("right");
rotationsCounter+=1;
clearCurrPosition();
rotate(rotatedCoords);
System.out.println(rotationsCounter);
if (rotationsCounter == 4) {
rotationsCounter = 0;
}
System.out.println(rotationsCounter);
}
calcRotation(String right or left) gets the current coordinates of all 4 Tiles in a Piece and sends them to int[] getRotation(String shape, String direction, int[] current X coords, int[] current Y Coords)
public int[] calcRotation(String direction) {
for (int i = 0; i < tile.length; i++) {
currPositionX[i] = tile[i].getX();
currPositionY[i] = tile[i].getY();
System.out.println(currPositionX[i] + ", " + currPositionY[i]);
}
return getRotation("Bar", direction, currPositionX, currPositionY);
}
then getRotation[] sets the new coordinates based on which rotation direction was chosen (right or left), which shape it is, and which rotation counter it's on (0 degrees, 90 degrees, 180 or 270...)
if (direction == "right") {
if (shape == "Bar") {
if (rotationsCounter == 0) {
currXs[0] += 1;
currYs[0] += -1;
currXs[1] += 0;
currYs[1] += 0;
currXs[2] += -1;
currYs[2] += 1;
currXs[3] += -2;
currYs[3] += 2;
rightRotate1 = new int[] {currXs[0], currYs[0], currXs[1], currYs[1], currXs[2], currYs[2], currXs[3], currYs[3]};
}
if (rotationsCounter == 1) {
... etc
Then the coordinates (pieceRotations
) would be set appropriately:
//handle on left rotations
if (direction == "right") {
if (shape == "Bar") {
if (rotationsCounter == 0) {
pieceRotations = rightRotate1;
}
if (rotationsCounter == 1) {
pieceRotations = rightRotate2;
}
if (rotationsCounter == 2) {
pieceRotations = rightRotate3;
}
if (rotationsCounter == 3) {
pieceRotations = rightRotate0;
}
}
}
if (direction == "left") {
if (shape == "Bar") {
if (rotationsCounter == 0) {
pieceRotations = rightRotate3;
}
if (rotationsCounter == 1) {
pieceRotations = rightRotate0;
}
if (rotationsCounter == 2) {
pieceRotations = rightRotate1;
}
if (rotationsCounter == 3) {
pieceRotations = rightRotate2;
}
}
}
return pieceRotations;
}
Then finally, rotate(rotatedCoords)
would be called with the correct coordinates to rotate all the tiles too...
public void rotate(int[] rotatedCoordinates) {
int counterX = 0, counterY = 1;
if (movePieceValid()) {
for (int i = 0; i < tile.length; i++) {
tile[i].setLocation(rotatedCoordinates[counterX], rotatedCoordinates[counterY]);
counterX+=2;
counterY+=2;
}
} else {
for (int i = 0; i < tile.length; i++) {
tile[i].setLocation(currPositionX[i], currPositionY[i]);
}
}
}
So, my current way of calculating the new coordinates based on current position of each shape for left
or right
is clearly overkill. Is there a general guide I can follow to greatly simplify that? I can't think of another way to get the positions for each shape?
Upvotes: 0
Views: 543
Reputation: 32333
There are only so many pieces. Try having this:
public abstract class Piece {
public abstract void rotate(Direction dir);
}
public class BarPiece extends Piece {
public void rotate(Direction dir) {
// implement
}
}
public class TPiece extends Piece {
// ...
}
public class LeftSPiece extends Piece {
// ...
}
It seems a bit dirty to special but doing math all the time will be slow, and since there are only so many possible pieces...
Upvotes: 4