-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGameBoardData.cpp
More file actions
190 lines (165 loc) · 5.91 KB
/
GameBoardData.cpp
File metadata and controls
190 lines (165 loc) · 5.91 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "GameBoardData.h"
CGameBoardData::CGameBoardData() : bottomID{16}, seed{31337} { reset(); }
CGameBoardData::~CGameBoardData() {}
uint32_t CGameBoardData::getSeed() { return seed; }
void CGameBoardData::setSeed(uint32_t s) { seed = s; }
void CGameBoardData::newLine() {
bottomID = normalizeY(bottomID + 1);
genLine(bottomID);
}
void CGameBoardData::genOneLine(int y) {
int x{2};
boardBuffer[y][0] = generator.rand(1, 5);
boardBuffer[y][1] = generator.rand(1, 5);
while (x < 6) {
auto next_value = generator.rand(1, 5);
if (boardBuffer[y][x - 2] != boardBuffer[y][x - 1] ||
boardBuffer[y][x - 1] != next_value) {
boardBuffer[y][x] = next_value;
x++;
}
}
}
void CGameBoardData::genLine(int y) {
int x{2};
for (int i = 0; i < 2; i++) {
int next_value{generator.rand(1, 5)};
while (boardBuffer[normalizeY(y - 2)][i] == boardBuffer[normalizeY(y - 1)][i] &&
boardBuffer[normalizeY(y - 1)][i] == next_value)
next_value = generator.rand(1, 5);
boardBuffer[y][i] = next_value;
fpBuffer[y][i] = 0.0;
}
while (x < 6) {
int next_value{generator.rand(1, 5)};
if ((boardBuffer[y][x - 2] != boardBuffer[y][x - 1] ||
boardBuffer[y][x - 1] != next_value) &&
(boardBuffer[normalizeY(y - 2)][x] != boardBuffer[normalizeY(y - 1)][x] ||
boardBuffer[normalizeY(y - 1)][x] != next_value)) {
boardBuffer[y][x] = next_value;
fpBuffer[y][x] = 0.0;
x++;
}
}
}
void CGameBoardData::genBegin() {
generator.setSeed(seed);
genOneLine(13);
genOneLine(14);
genLine(15);
genLine(16);
}
void CGameBoardData::reset() {
for (int y = 0; y < 17; y++) {
for (int x = 0; x < 6; x++) {
boardBuffer[y][x] = 0.0;
fpBuffer[y][x] = 0.0;
}
}
bottomID = 16;
genBegin();
}
int CGameBoardData::normalizeY(int y) {
while (y >= 17)
y -= 17;
while (y < 0)
y += 17;
return y;
}
bool CGameBoardData::isLineEmpty(int y) {
int v = 0;
for (int x = 0; x < 6; x++)
v += boardBuffer[y][x];
return v == 0;
}
void CGameBoardData::clearTop() {
int y{normalizeY(bottomID + 1)};
if (isLineEmpty(y)) {
while (isLineEmpty(y))
y = normalizeY(y + 1);
}
for (int x = 0; x < 6; x++)
boardBuffer[y][x] = 0x00;
}
uint8_t CGameBoardData::getBlockID(const glm::ivec2 &block) {
int correctY{bottomID + 1};
return boardBuffer[normalizeY(correctY + block.y)][block.x];
}
BOX_COLOR CGameBoardData::getBlockColor(const glm::ivec2 &block) {
auto id{getBlockID(block)};
return (id >= BOX_COLOR::BOX_FALLING)
? (BOX_COLOR)(id - BOX_COLOR::BOX_FALLING - 1)
: (BOX_COLOR)(id - 1);
}
void CGameBoardData::set(const glm::ivec2 &block, uint8_t value) {
int correctY{bottomID + 1};
boardBuffer[normalizeY(correctY + block.y)][block.x] = value;
}
bool CGameBoardData::isBlockFalling(const glm::ivec2 &block) {
return (getBlockID(block) >= BOX_COLOR::BOX_FALLING) ? true : false;
}
bool CGameBoardData::isSetToRemove(const glm::ivec2 &block) {
int correctY = bottomID + 1;
return (fpBuffer[normalizeY(correctY + block.y)][block.x] > 0.0);
}
void CGameBoardData::setToRemove(const glm::ivec2 &block) {
int correctY = bottomID + 1;
fpBuffer[normalizeY(correctY + block.y)][block.x] = 1.0;
}
double CGameBoardData::getBlink(const glm::ivec2 &block) {
int correctY = bottomID + 1;
return fpBuffer[normalizeY(correctY + block.y)][block.x];
}
double CGameBoardData::getFallOffset(const glm::ivec2 &block) {
int correctY = bottomID + 1;
return fpBuffer[normalizeY(correctY + block.y)][block.x];
}
void CGameBoardData::updateBlink(double dtime) {
int correctY{bottomID + 1};
for (int y = 0; y < 17; y++) {
for (int x = 0; x < 6; x++) {
if (isBlockFalling({x, y}))
continue;
if (fpBuffer[normalizeY(correctY + y)][x] > 0.0) {
fpBuffer[normalizeY(correctY + y)][x] -= dtime;
if (fpBuffer[normalizeY(correctY + y)][x] <= 0.0) {
boardBuffer[normalizeY(correctY + y)][x] = 0;
fpBuffer[normalizeY(correctY + y)][x] = 0.0;
}
}
}
}
}
void CGameBoardData::updateGravity(double dtime) {
const double fallSpeed{64.0};
int correctY{bottomID + 1};
for (int y = 13; y > 0; y--) {
for (int x = 0; x < 6; x++) {
auto underBlock{normalizeY(correctY + y)};
auto currentBlock{normalizeY(correctY + y - 1)};
if (isSetToRemove({x, y - 1}))
continue;
if ((boardBuffer[underBlock][x] == 0 ||
boardBuffer[underBlock][x] >= BOX_COLOR::BOX_FALLING) &&
boardBuffer[currentBlock][x] != 0 && y < 13) {
if (boardBuffer[currentBlock][x] < BOX_COLOR::BOX_FALLING) {
boardBuffer[currentBlock][x] += BOX_COLOR::BOX_FALLING;
fpBuffer[currentBlock][x] = 0.0;
} else {
fpBuffer[currentBlock][x] -= fallSpeed * dtime;
if (fpBuffer[currentBlock][x] <= -2.0 &&
boardBuffer[underBlock][x] == 0) {
boardBuffer[underBlock][x] = boardBuffer[currentBlock][x];
boardBuffer[currentBlock][x] = 0;
fpBuffer[underBlock][x] = fpBuffer[currentBlock][x] + 2.0;
fpBuffer[currentBlock][x] = 0.0;
}
}
} else if (boardBuffer[underBlock][x] != 0 &&
boardBuffer[currentBlock][x] >= BOX_COLOR::BOX_FALLING) {
boardBuffer[currentBlock][x] -= BOX_COLOR::BOX_FALLING;
fpBuffer[currentBlock][x] = 0.0;
}
}
}
}