-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTetrisBlockProcess.pde
65 lines (51 loc) · 1.81 KB
/
TetrisBlockProcess.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
BlockPart[] getBlockParts(Block block) {
switch(block.type) {
case I: return getIBlockParts(block.direction);
case O: return getOBlockParts();
case T: return getTBlockParts(block.direction);
case S: return getSBlockParts(block.direction);
case Z: return getZBlockParts(block.direction);
case J: return getJBlockParts(block.direction);
case L: return getLBlockParts(block.direction);
}
return getIBlockParts(block.direction);
}
void makeBlockFall(Block block) {
block.yPos += 1;
for (int i = 0; i < BlockPartsCount; i++) {
block.parts[i].yPos += 1;
}
}
// We assume, only unsigned int is possible in block parts[i].xPos.
void arrangeNewBlock(Block block, int wellWidth) {
int leftXShift = 0;
int maxYShift = 0;
for (int i = 0; i < BlockPartsCount; i++) {
maxYShift = max (block.parts[i].yPos, maxYShift);
block.parts[i].xPos = block.parts[i].xPos + block.xPos;
block.parts[i].yPos = block.parts[i].yPos + block.yPos;
if (block.parts[i].xPos >= wellWidth)
leftXShift = max(leftXShift, block.parts[i].xPos + 1 - wellWidth);
}
for (int i = 0; i < BlockPartsCount; i++) {
block.parts[i].xPos = block.parts[i].xPos - leftXShift;
block.parts[i].yPos = block.parts[i].yPos - (1 + maxYShift);
}
}
Block createBlock(int type, int direction, int xPos, int yPos, int wellWidth) {
Block block = new Block(type, xPos, yPos, direction);
block.parts = getBlockParts(block);
arrangeNewBlock(block, wellWidth);
return block;
}
void moveBlockHorizontal(Block block, int distance) {
for (int i = 0; i < BlockPartsCount; i++) {
block.parts[i].xPos = block.parts[i].xPos + distance;
}
block.xPos += distance;
}
int rotateDirection(int prevDirection) {
if (prevDirection < DirectionsCount - 1)
return prevDirection + 1;
return 0;
}