/
TPiece.pde
106 lines (88 loc) · 3.07 KB
/
TPiece.pde
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class TPiece extends Piece {
/*
# 2 441144 (purple)
### 103
*/
public TPiece(float x, float y) {
super.setX(x);
super.setY(y);
for (int i=0; i<4; i++) {
blocks[i] = new Block(x + offsetX[i], y + offsetY[i], blockSize, "#441144");
}
update();
// 45 deg pieces don't line up right
// TODO: figure out math to get rid of this
super.round(blocks[0]);
super.round(blocks[1]);
super.round(blocks[2]);
super.round(blocks[3]);
}
public boolean rotateCollideX(float wallStart, float wallWidth) {
// update test coords for coll test
testUpdate();
return(super.rotateCollideX(wallStart, wallWidth));
}
public boolean rotateCollideY(float roomStart, float roomWidth) {
// are we near the bottom?
if (super.pivotPoint.getY() < roomWidth / 2) {
if (super.pivotPoint.getY() + blockSize * 3 < roomWidth) {
// we are nowhere near the bottom
return false;
} else {
testUpdate();
return(super.rotateCollideYHit(roomStart, roomWidth));
}
}
// default return
return false;
}
public boolean rotateCollide(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
//println("rotateCollide in LPiece");
// are we near the grid, if not, don't waste cycles
if (super.nearGrid(deadGrid, playField, gridSizeX, gridSizeY)) {
testUpdate();
// call super method to check for hit
//println("rotateCollide in LPiece");
return(super.rotateCollideHit(deadGrid, gridSizeX, gridSizeY, testOffsetX, testOffsetY, playField));
}
return false;
}
/* Call this whenever moving or rotating */
public void update() {
offsetX[0] = 0;
offsetX[1] = sin(rotation + radians(270)) * blockSize;
offsetX[2] = sin(rotation + radians(0)) * blockSize;
offsetX[3] = sin(rotation + radians(90)) * blockSize;
offsetY[0] = 0;
offsetY[1] = cos(rotation + radians(90)) * blockSize;
offsetY[2] = cos(rotation + radians(180)) * blockSize;
offsetY[3] = cos(rotation + radians(270)) * blockSize;
for (int i=0; i<4; i++) {
blocks[i].setX(super.pivotPoint.getX() + offsetX[i]);
blocks[i].setY(super.pivotPoint.getY() + offsetY[i]);
}
// rounding to line up with grid
// TODO: better way?
super.round(blocks[0]);
super.round(blocks[1]);
super.round(blocks[2]);
super.round(blocks[3]);
}
// this method is called to test collision without updating position, it's like look-ahead
public void testUpdate() {
testRotation = rotation + radians(90.0f);
testOffsetX[0] = 0;
testOffsetX[1] = sin(testRotation + radians(270)) * blockSize;
testOffsetX[2] = sin(testRotation + radians(0)) * blockSize;
testOffsetX[3] = sin(testRotation + radians(90)) * blockSize;
testOffsetY[0] = 0;
testOffsetY[1] = cos(testRotation + radians(90)) * blockSize;
testOffsetY[2] = cos(testRotation + radians(180)) * blockSize;
testOffsetY[3] = cos(testRotation + radians(270)) * blockSize;
// round our test offsets to blocksize
for (int i=0; i<4; i++) {
testOffsetX[i] = Math.round(testOffsetX[i] / blockSize) * blockSize;
testOffsetY[i] = Math.round(testOffsetY[i] / blockSize) * blockSize;
}
}
}