-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFallingPiece.java
More file actions
67 lines (53 loc) · 1.41 KB
/
Copy pathFallingPiece.java
File metadata and controls
67 lines (53 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import core.Entity;
import core.Relation;
public class FallingPiece extends Entity<Relation> {
private TetrominoType type = TetrominoType.I;
private int row = 0;
private int col = 3;
private int rotation = 0;
public FallingPiece(int id, String label) {
super(id, label);
}
public void reset(TetrominoType type, int row, int col) {
this.type = type;
this.row = row;
this.col = col;
this.rotation = 0;
}
public void moveBy(int dx, int dy) {
this.col += dx;
this.row += dy;
}
public void moveTo(int row, int col) {
this.row = row;
this.col = col;
}
public void setRotation(int rotation) {
this.rotation = Math.floorMod(rotation, 4);
}
public List<Point> cells() {
return cellsFor(row, col, rotation);
}
public List<Point> cellsFor(int row, int col, int rotation) {
List<Point> points = new ArrayList<>();
for (int[] cell : type.cells(rotation)) {
points.add(new Point(col + cell[1], row + cell[0]));
}
return points;
}
public TetrominoType getType() {
return type;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getRotation() {
return rotation;
}
}