-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cpp
More file actions
343 lines (286 loc) · 7.98 KB
/
Block.cpp
File metadata and controls
343 lines (286 loc) · 7.98 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Authors: Christine Seng, Erick Martinez, Georgia Rushing, Graham Balas, Nolan Schirripa
* Assignment Title: Group Project (Block.cpp)
* Assignment Description: defines block class that can be hit for score and change color when hit
* Due Date: 12/09/2024
* Date Created: 10/25/2024
* Date Last Modified: 12/07/2024
*/
#include "Block.h"
#include "game.h"
//Constructors
Block::Block(): location(), life(1), blockColor(), shape("") {
updateBlockColor();
}
Block::Block(point loc, string s): location(loc), shape(s){
life = rand() % (Game::maxDifficulty - Game::minDifficulty + 1) + Game::minDifficulty;
if (life < Game::minDifficulty) {
life = Game::minDifficulty;
} else if (life > Game::maxDifficulty) {
life = Game::maxDifficulty;
}
updateBlockColor();
hb.setPoint(loc);
hb.setLength(85);
hb.setWidth(85);
}
//Member Functions
void Block::drawSquare(point loc, int size, color c, SDL_Plotter& win)
{
hb.setPoint(loc);
int length, width;
shape = "Square";
location.x = loc.x;
location.y = loc.y;
length = size, width = size;
// Calculate the top-left corner of the square based on the center
int startX = loc.x - length / 2;
int startY = loc.y - width / 2;
// Draw the square starting from the calculated top-left corner
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
win.plotPixel(startX + i, startY + j, c.R, c.G, c.B);
}
}
}
void Block::drawLine(point loc1, point loc2, color c, SDL_Plotter& g) const
{
int changeInX = loc2.x - loc1.x;
int changeInY = loc2.y - loc1.y;
int steps;
if (changeInX >= changeInY)
{
steps = changeInX;
}
else
{
steps = changeInY;
}
double xIncrease = changeInX / static_cast<double>(steps);
double yIncrease = changeInY / static_cast<double>(steps);
double x;
double y;
x = loc1.x;
y = loc1.y;
for (int i = 0; i <= steps; ++i)
{
g.plotPixel(round(x), round(y), c);
x += xIncrease;
y += yIncrease;
}
}
double Block::distance(point loc1, point loc2) const
{
double distance;
distance = sqrt(pow(loc1.x - loc2.x, 2.0) + pow(loc1.y - loc2.y, 2.0));
return distance;
}
void Block::drawTriangle(point centroid, int size, color c, SDL_Plotter& g)
{
hb.setPoint(centroid);
double height;
point topVertex, leftVertex, rightVertex;
point start, end;
shape = "Triangle";
location.x = centroid.x;
location.y = centroid.y;
height = (sqrt(3.0) / 2.0) * size;
topVertex.x = centroid.x;
topVertex.y = centroid.y - (2.0 / 3.0) * height;
leftVertex.x = centroid.x - (size / 2.0);
leftVertex.y = centroid.y + (1.0 / 3.0) * height;
rightVertex.x = centroid.x + (size / 2.0);
rightVertex.y = centroid.y + (1.0 / 3.0) * height;
for (int y = 0; y <= height; ++y)
{
int offset = (y / static_cast<double>(height)) * (size / 2.0);
// start and end points of the line to draw at this row
start.x = topVertex.x - offset;
start.y = topVertex.y + y;
end.x = topVertex.x + offset;
end.y = topVertex.y + y;
// Draw the line for this row
drawLine(start, end, c, g);
}
}
void Block::drawCircle(point loc, int size, color c, SDL_Plotter& win)
{
hb.setPoint(loc);
int x = 0;
int y = size;
int d = 3 - 2 * size;
auto plotHorizontalLine = [&](int xStart, int xEnd, int yCoord)
{
for (int x = xStart; x <= xEnd; x++)
{
win.plotPixel(x, yCoord, c);
}
};
while (x <= y)
{
// Draw horizontal spans to fill the circle
plotHorizontalLine(loc.x - x, loc.x + x, loc.y + y); // Upper span
plotHorizontalLine(loc.x - x, loc.x + x, loc.y - y); // Lower span
plotHorizontalLine(loc.x - y, loc.x + y, loc.y + x); // Left span
plotHorizontalLine(loc.x - y, loc.x + y, loc.y - x); // Right span
// Update decision parameter and iterate
if (d <= 0)
{
d = d + 4 * x + 6;
}
else
{
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
void Block::drawMirroredTriangle(point leftVertex, point rightVertex, point bottomVertex, color c, SDL_Plotter& g)
{
int sideLength;
int height;
int offset; //distance from center to edges
point start;
point end;
sideLength = distance(leftVertex, rightVertex);
height = (sqrt(3.0) / 2.0) * sideLength;
for (int y = 0; y <= height; ++y)
{
offset = ((height - y) / static_cast<double>(height)) * (sideLength / 2.0);
start.x = bottomVertex.x - offset;
start.y = bottomVertex.y + y;
end.x = bottomVertex.x + offset;
end.y = bottomVertex.y + y;
drawLine(start, end, c, g);
}
}
void Block::createLevel(point startLoc)
{
srand(time(0));
//Data-Abstraction
int size = 75;
int randLocOne, randLocTwo, randLocThree;
randLocOne = rand() % 281 + 39;
randLocTwo = rand() % 281 + 359;
randLocThree = rand() % 281 + 679;
color red = {255, 0, 0};
point loc1 = {randLocOne, startLoc.y + levelOffsetY};
point loc2 = {randLocTwo, startLoc.y + levelOffsetY};
point loc3 = {randLocThree, startLoc.y + levelOffsetY};
vector<string> shapes(3);
// Push existing levels upward
for (int i = 0; i < allActiveShapes.size(); ++i) {
allActiveShapes[i].location.y -= 100;
//make sure hitboxes update
allActiveShapes[i].hb.setPoint(allActiveShapes[i].location);
}
for (int i = 0; i < 3; ++i) {
int state = rand() % 10;
if (state >= 0 && state < 4) state = 1;
else if (state < 8) state = 2;
else if (state <= 9) state = 3;
if (state == 1) shapes[i] = "Circle";
if (state == 2) shapes[i] = "Square";
if (state == 3) shapes[i] = "Triangle";
}
allActiveShapes.emplace_back(loc1, shapes[0]); //emplace_back() is like push_back() but for pass by refrence
allActiveShapes.emplace_back(loc2, shapes[1]);
allActiveShapes.emplace_back(loc3, shapes[2]);
levelOffsetY += 100;
}
void Block::drawLevel(point startLoc, SDL_Plotter& g)
{
//Data-Abstraction
int size = 75;
//Draw random Shapes to the created Level
if (!allActiveShapes.empty())
{
for(int i = 0; i < allActiveShapes.size(); ++i)
{
if(allActiveShapes[i].shape == "Circle")
{
drawCircle(allActiveShapes[i].location, size / 2, allActiveShapes[i].getColor(), g);
}
if(allActiveShapes[i].shape == "Square")
{
drawSquare(allActiveShapes[i].location, size, allActiveShapes[i].getColor(), g);
}
if(allActiveShapes[i].shape == "Triangle")
{
drawTriangle(allActiveShapes[i].location, size, allActiveShapes[i].getColor(), g);
}
}
}
}
void Block::nextLevel()
{
currentLevel++;
}
void Block::decreaseLife() {
if (life > 0) {
--life;
updateBlockColor();
}
}
void Block::updateBlockColor() {
switch (life) {
case 8: {
blockColor = {255, 0, 0}; //red
break;
}
case 7: {
blockColor = {255, 128, 0}; //orange
break;
}
case 6: {
blockColor = {255, 240, 70}; //yellow
break;
}
case 5: {
blockColor = {102, 204, 0}; //Green
break;
}
case 4: {
blockColor = {51, 255, 51}; //lime green
break;
}
case 3: {
blockColor = {102, 255, 178}; //turquois
break;
}
case 2: {
blockColor = {102, 178, 255}; //sky blue
break;
}
case 1: {
blockColor = {255, 102, 255}; //violet
break;
}
case 0: {
blockColor = {255, 255, 255}; //white
break;
}
default: {
blockColor = {0, 0, 0}; //black for debugging
break;
}
}
}
void Block::setAllActiveShapesLife (int l, int index) {
allActiveShapes.at(index).setLife(l);
}
bool Block::gameOver()
{
bool flag = false;
for(int i = 0; i < allActiveShapes.size(); ++i)
{
if(allActiveShapes[i].location.y < 0)
{
flag = true;
}
}
return flag;
}