-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piece.cpp
114 lines (99 loc) · 1.8 KB
/
Piece.cpp
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
107
108
109
110
111
112
113
114
#include "Piece.h"
#include "pieces.h"
Piece::Piece()
{
m_piece = 0;
m_rotation = ROTATION_0;
}
Piece::Piece(int piece, int rotation)
{
m_piece = piece;
m_rotation = rotation;
}
void
Piece::rotate()
{
m_rotation++;
if (m_rotation > ROTATION_270) {
m_rotation = ROTATION_0;
}
}
void
Piece::resetBlocksEnum()
{
m_blockEnumX = 0;
m_blockEnumY = 0;
}
/**
* Finds next piece's block and return its position
*/
bool
Piece::iterateBlocksEnum(int *x, int *y)
{
bool hasResult = false;
while (
!hasResult
&& m_blockEnumX < PIECE_SIZE
&& m_blockEnumY < PIECE_SIZE
) {
hasResult = checkHasBlock(m_blockEnumX, m_blockEnumY);
if (hasResult) {
*x = m_blockEnumX;
*y = m_blockEnumY;
}
m_blockEnumY++;
if (m_blockEnumY >= PIECE_SIZE) {
m_blockEnumY = 0;
m_blockEnumX++;
}
}
return hasResult;
}
bool
Piece::checkHasBlock(int x, int y)
{
if (m_rotation == ROTATION_0) {
return (pieceBlocks[m_piece][y][x] > 0);
} else if (m_rotation == ROTATION_90) {
return (pieceBlocks[m_piece][PIECE_SIZE-x-1][y] > 0);
} else if (m_rotation == ROTATION_180) {
return (pieceBlocks[m_piece][PIECE_SIZE-y-1][PIECE_SIZE-x-1] > 0);
} else {
return (pieceBlocks[m_piece][x][PIECE_SIZE-y-1] > 0);
}
}
Uint8
Piece::getColorRed()
{
return pieceColor[m_piece][0];
}
Uint8
Piece::getColorGreen()
{
return pieceColor[m_piece][1];
}
Uint8
Piece::getColorBlue()
{
return pieceColor[m_piece][2];
}
void
Piece::setPiece(int piece)
{
m_piece = piece;
}
int
Piece::getPiece()
{
return m_piece;
}
void
Piece::setRotation(int rotation)
{
m_rotation = rotation;
}
int
Piece::getRotation()
{
return m_rotation;
}