summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/se/liu/gusso230/tetris/Board.java29
-rw-r--r--src/se/liu/gusso230/tetris/Point.java12
-rw-r--r--src/se/liu/gusso230/tetris/Poly.java9
-rw-r--r--src/se/liu/gusso230/tetris/TetrisComponent.java19
4 files changed, 66 insertions, 3 deletions
diff --git a/src/se/liu/gusso230/tetris/Board.java b/src/se/liu/gusso230/tetris/Board.java
index da5606c..d176dee 100644
--- a/src/se/liu/gusso230/tetris/Board.java
+++ b/src/se/liu/gusso230/tetris/Board.java
@@ -70,6 +70,9 @@ public class Board {
private boolean hasFallingCollision() {
return !falling.onlyCoversEmpty(fallingX, fallingY, this);
}
+ private boolean fallingIsOutside() {
+ return !falling.isInside(fallingX, fallingY, 0, width - 1, 0, height - 1);
+ }
public void move(Direction dir) {
if (falling == null) {
@@ -85,14 +88,36 @@ public class Board {
break;
}
fallingX += dx;
- if (!falling.isInside(fallingX, fallingY, 0, width - 1, 0, height - 1)
- || hasFallingCollision())
+ if (fallingIsOutside() || hasFallingCollision())
{
fallingX -= dx;
}
notifyListeners();
}
+ public void rotate(Direction dir) {
+ if (falling == null) {
+ return;
+ }
+
+ if (dir == Direction.RIGHT) {
+ falling.rotateClockwise();
+ } else {
+ falling.rotateCounterClockwise();
+ }
+
+ // reverse rotation if collision
+ if (fallingIsOutside() || hasFallingCollision()) {
+ if (dir == Direction.RIGHT) {
+ falling.rotateCounterClockwise();
+ } else {
+ falling.rotateClockwise();
+ }
+ }
+
+ notifyListeners();
+ }
+
private void placeAt(Poly poly, int x, int y) {
for (Point pt : poly.getPoints()) {
squares[y + pt.getY()][x + pt.getX()] = poly.getSquareType();
diff --git a/src/se/liu/gusso230/tetris/Point.java b/src/se/liu/gusso230/tetris/Point.java
index 577ef74..139f155 100644
--- a/src/se/liu/gusso230/tetris/Point.java
+++ b/src/se/liu/gusso230/tetris/Point.java
@@ -27,4 +27,16 @@ public class Point {
x = boundingBoxSize - y;
y = prevX; // This looks suspicious but it's intended. (x, y) => (s - y, x)
}
+
+ /**
+ * Rotates this point one step counter clockwise.
+ *
+ * @param boundingBoxSize the size of the bounding box this point should be rotated with respect to.
+ */
+ public void rotateCounterClockwise(int boundingBoxSize) {
+ //TODO better logic
+ rotateClockwise(boundingBoxSize);
+ rotateClockwise(boundingBoxSize);
+ rotateClockwise(boundingBoxSize);
+ }
}
diff --git a/src/se/liu/gusso230/tetris/Poly.java b/src/se/liu/gusso230/tetris/Poly.java
index 734b25e..efd9b7e 100644
--- a/src/se/liu/gusso230/tetris/Poly.java
+++ b/src/se/liu/gusso230/tetris/Poly.java
@@ -79,6 +79,15 @@ public class Poly {
}
}
+ /**
+ * Rotates all the poly's points one step counter clockwise.
+ */
+ public void rotateCounterClockwise() {
+ for (Point point : points) {
+ point.rotateCounterClockwise(boundingBoxSize);
+ }
+ }
+
public boolean onlyCoversEmpty(int posX, int posY, Board board) {
for (Point point : points) {
if (board.getSquareAt(posX + point.getX(), posY + point.getY(), false) != SquareType.EMPTY) {
diff --git a/src/se/liu/gusso230/tetris/TetrisComponent.java b/src/se/liu/gusso230/tetris/TetrisComponent.java
index 2be91a3..e9c14f0 100644
--- a/src/se/liu/gusso230/tetris/TetrisComponent.java
+++ b/src/se/liu/gusso230/tetris/TetrisComponent.java
@@ -23,16 +23,33 @@ public class TetrisComponent extends JComponent implements BoardListener {
}
}
+ private class RotateAction extends AbstractAction {
+ private Direction dir;
+ private RotateAction(Direction dir) {
+ this.dir = dir;
+ }
+
+ @Override public void actionPerformed(final ActionEvent actionEvent) {
+ board.rotate(dir);
+ }
+ }
+
public TetrisComponent(final Board board) {
this.board = board;
this.getInputMap().put(KeyStroke.getKeyStroke('a'), "moveLeft");
- this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "moveLeft");
this.getInputMap().put(KeyStroke.getKeyStroke('d'), "moveRight");
+ this.getInputMap().put(KeyStroke.getKeyStroke('q'), "rotateLeft");
+ this.getInputMap().put(KeyStroke.getKeyStroke('e'), "rotateRight");
+ this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "moveLeft");
this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "moveRight");
+ this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "rotateLeft");
+ this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "rotateRight");
this.getActionMap().put("moveLeft", new MoveAction(Direction.LEFT));
this.getActionMap().put("moveRight", new MoveAction(Direction.RIGHT));
+ this.getActionMap().put("rotateLeft", new RotateAction(Direction.LEFT));
+ this.getActionMap().put("rotateRight", new RotateAction(Direction.RIGHT));
}
@Override protected void paintComponent(final Graphics g) {