-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiece.cpp
More file actions
239 lines (218 loc) · 6.44 KB
/
piece.cpp
File metadata and controls
239 lines (218 loc) · 6.44 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
/* Author: Brendon Kofink
* Wesley Goyette
* Aaron Sierra
* David Day
* Lucy Ray
* Assignment Title: Epicer Puzzle
* Assignment Description: A Puzzle game where the user can
* solve a puzzle by moving and snapping pieces into place.
* Due Date: 5/4/2023
* Date Created: 4/20/2023
* Date Last Modified: 4/20/2023
*/
#include "piece.h"
#include "picture.h"
// Intentional Shallow Copy.
Edge::Edge(Picture &pic, bool inverted) : Picture() {
this->inverted = inverted;
width = pic.width;
height = pic.height;
picData = pic.picData;
this->owner = false;
}
Edge::Edge() : Picture() {
width = 128;
height = 32;
picData = nullptr;
inverted = false;
}
Edge::~Edge() { owner = false; }
color Edge::getPixel(int x, int y) {
color col;
if (picData == nullptr) {
int abs = (x - height / 2);
if (y < abs + height / 2) {
return color(255, 0, 0);
}
if (y > -abs + width - height / 2) {
col = color(0, 255, 0);
return col;
}
if (x < height / 2 || y < height / 2 || y > width - height / 2) {
col = color(0, 0, 0);
} else {
col = color(255, 255, 255);
}
return col;
} else {
int abs = (x - height / 2);
if (abs < 0) {
abs = -abs;
}
if (y < abs + height / 2) {
return color(255, 0, 0);
}
if (y > -abs + width - height / 2) {
col = color(0, 255, 0);
return col;
}
if (y + x == height - 1) {
col = color(0, 0, 255);
return col;
}
if (y - x == width - height) {
col = color(0, 0, 255);
return col;
}
if (inverted) {
x = height - x;
y = width - y;
if (x >= height || x + y == width) {
return color(0, 0, 0);
}
}
col = picData[x][y];
}
if (inverted) {
col.R = 255 - col.R;
col.G = 255 - col.G;
col.B = 255 - col.B;
}
return col;
}
Edge& Edge::operator=(const Edge &other) {
if(this != &other) {
picData = other.picData;
width = other.width;
height = other.height;
inverted = other.inverted;
}
return *this;
}
Piece::Piece(Picture *image, Vec2 imagePos, Vec2 pos)
: image(image), imagePos(imagePos), pos(pos), topEdge(), bottomEdge(),
lEdge(), rEdge() {
orientation = NORMAL;
}
Piece::Piece(const Piece &other) : pos(other.pos), imagePos(other.imagePos),
image(other.image) {
moved = other.moved;
for(int i = 0; i< 4; i++) {
neighborArr[i] = other.neighborArr[i];
connected[i] = other.connected[i];
}
orientation = other.orientation;
topEdge = other.topEdge;
bottomEdge = other.bottomEdge;
lEdge = other.lEdge;
rEdge = other.rEdge;
}
void Piece::draw(Drawer &drawer) { draw(drawer, *image); }
void Piece::draw(Drawer &drawer, Picture &pic) {
Vec2 pictureDim = Vec2(128, 128);
Vec2 edgedim = Vec2(128, 32);
Vec2 unitVec = Vec2(1, 1);
drawer.drawMask(pic, topEdge, imagePos, pos);
drawer.drawMask(
pic, bottomEdge, imagePos + pictureDim.ycomp() - edgedim.ycomp(),
pos + pictureDim.ycomp() - edgedim.ycomp(), Orientation::FLIPPED);
drawer.drawMask(pic, lEdge, imagePos, pos, Orientation::LEFT);
drawer.drawMask(
pic, rEdge, imagePos + edgedim.xcomp() - edgedim.ycomp().flip(),
pos + edgedim.xcomp() - edgedim.ycomp().flip(), Orientation::RIGHT);
drawer.drawPart(pic, imagePos + edgedim.splaty(),
pictureDim - edgedim.splaty() * 2 + UNIT,
pos + edgedim.splaty(), Orientation::NORMAL);
}
void Piece::setNeighbor(int index, Piece *neighbor) {
this->neighborArr[index] = neighbor;
}
/*
* isClicked
* Description: This function checks to see if the piece has been clicked on
* by checking to see if the point falls within the bounds of
* the given piece.
* return: bool - true if the piece has been clicked on, false
* otherwise precondition: there exists a piece object post-condition: nothing
* is changed
*
*/
bool Piece::isClicked(point p) {
// store the x and y values of the point
int x = p.x, y = p.y;
// this checks x values
return x >= pos.x && x <= pos.x + 128 &&
// this checks y values
y >= pos.y && y <= pos.y + 128;
}
void Piece::setPos(const Vec2 &newPos) { pos = newPos; }
Vec2 Piece::getPos() const { return pos; }
bool Piece::isSnappable(Orientation orient) {
if (neighborArr[orient] == nullptr) {
return false;
}
Vec2 neighborPos = neighborArr[orient]->getPos();
float sqmag = (neighborPos - pos).sqMagnitude();
if (sqmag < 12000 && sqmag > 8000) {
if ((neighborPos - pos).normalized().dot(orientations[orient]) > 0.3) {
return true;
}
}
return false;
}
Vec2 Piece::snap(Orientation orient) {
Piece *neighbor = neighborArr[orient];
Vec2 neighborPos = neighbor->getPos();
Vec2 newPos = neighborPos - orientations[orient] * 96;
return newPos;
}
void Piece::connect(Orientation orient) {
setConnected(orient, true);
neighborArr[orient]->setConnected(Orientation((orient + 2) % 4), true);
}
void Piece::setConnected(Orientation orient, bool connected) {
this->connected[orient] = connected;
}
void Piece::move(Vec2 newPos, Drawer &drawer, Picture &bg) {
if (moved) {
return;
}
Vec2 dir = newPos - pos;
moved = true;
draw(drawer, bg);
pos = newPos;
for (int i = 0; i < 4; i++) {
if (neighborArr[i] != nullptr && connected[i]) {
neighborArr[i]->move(neighborArr[i]->getPos() + dir, drawer, bg);
}
}
}
void Piece::setMoved(bool moved) {
bool old = this->moved;
this->moved = moved;
if (old && !moved) {
for (int i = 0; i < 4; i++) {
if (neighborArr[i] != nullptr && connected[i]) {
neighborArr[i]->setMoved(false);
}
}
}
}
int Piece::ping(int count) {
if (moved) {
return count;
}
moved = true;
count++;
for (int i = 0; i < 4; i++) {
if (neighborArr[i] != nullptr && connected[i]) {
count = neighborArr[i]->ping(count);
}
}
return count;
}
int Piece::getConnected() {
int count = ping(0);
setMoved(false);
return count;
}