-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag.cpp
More file actions
134 lines (124 loc) · 3.48 KB
/
flag.cpp
File metadata and controls
134 lines (124 loc) · 3.48 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
/*
* Author: Nolan Schirripa, Christine Seng, Erick Martinez, Georgia Rushing, Graham Balas
* Assignment Title: Group Project (flag.cpp)
* Assignment Description: defines flag class that can detect if ball has hit something
* Due Date: 12/09/2024
* Date Created: 10/25/2024
* Date Last Modified: 12/07/2024
*/
#include "flag.h"
Flag::Flag(){
x = 0;
y = 0;
point topArray[5];
point bottomArray[5];
point leftArray[5];
point rightArray[5];
point topRight;
point bottomRight;
point bottomLeft;
point topLeft;
flagSpace = 3;
}
void Flag::update(int newX, int newY, int ballSize, force& newF){
//update force
f = newF;
point p;
//update edges
for (int i = 0; i < 5; ++i){
p.x = newX + (flagSpace * (i - 2));
p.y = newY - ballSize - 3;
topArray[i] = p;
p.x = newX + (flagSpace * (i - 2));
p.y = newY + ballSize + 3;
bottomArray[i] = p;
p.x = newX + ballSize + 3;
p.y = newY + (flagSpace * (i - 2));
rightArray[i] = p;
p.x = newX - ballSize - 3;
p.y = newY + (flagSpace * (i - 2));
leftArray[i] = p;
}
//update corners
//update top right
p.x = newX + ballSize + 2;
p.y = newY - ballSize - 2;
topRight = p;
//bottom right
p.x = newX + ballSize + 2;
p.y = newY + ballSize + 2;
bottomRight = p;
//bottom left
p.x = newX - ballSize - 2;
p.y = newY + ballSize + 2;
bottomLeft = p;
//top left
p.x = newX - ballSize - 2;
p.y = newY - ballSize - 2;
topLeft = p;
}
int Flag::isHit(SDL_Plotter& g){
int sideNum = -1; //if no sides or corners hit return -1
if (f.getDirection() >= 0 && f.getDirection() <= M_PI / 2){ //check bottom, right, bottom-right
if(rowIsHit(bottomArray, g)){
sideNum = 2;
}
else if (rowIsHit(rightArray, g)){
sideNum = 1;
}
else if (pointIsHit(bottomRight, g)){//check corner
sideNum = 5;
}
}
else if (f.getDirection() >= M_PI / 2 && f.getDirection() <= M_PI){ //check right, top, top-right
if (rowIsHit(rightArray, g)){
sideNum = 1;
}
else if (rowIsHit(topArray, g)){
sideNum = 0;
}
else if (pointIsHit(topRight, g)){
sideNum = 4;
}
}
else if (f.getDirection() >= M_PI && f.getDirection() <= 3 * M_PI / 2){ //check top, left, top-left
if (rowIsHit(topArray, g)){
sideNum = 0;
}
else if (rowIsHit(leftArray, g)){
sideNum = 3;
}
else if (pointIsHit(topLeft, g)){
sideNum = 7;
}
}
else if (f.getDirection() >= 3 * M_PI / 2 && f.getDirection() <= 2 * M_PI){ //check left, bottom, bottom-left
if (rowIsHit(bottomArray, g)){
sideNum = 2;
}
else if (rowIsHit(leftArray, g)){
sideNum = 3;
}
else if (pointIsHit(bottomLeft, g)){
sideNum = 6;
}
}
return sideNum;
}
//checks if row of flags is hit
bool Flag::rowIsHit(point pointArray[], SDL_Plotter& g){
bool hit = false;
for (int i = 0; i < 5; ++i){
if (pointIsHit(pointArray[i], g)){
hit = true;
}
}
return hit;
}
bool Flag::pointIsHit(point p, SDL_Plotter& g){
bool result = false;
if (g.getColor(p.x, p.y) != flagWhite && g.getColor(p.x, p.y) != flagBlack && g.getColor(p.x, p.y) != flagEmpty){
result = true;
}
return result;
}