-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cpp
226 lines (193 loc) · 5.15 KB
/
Snake.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
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
#include "Snake.h"
Snake::Snake(sf::Vector2f position, sf::Vector2f velocity, sf::Color color,
Board* board, sf::Keyboard::Key leftKey, sf::Keyboard::Key rightKey) {
this->position = position;
this->velocity = velocity;
this->color = color;
this->board = board;
this->leftKey = leftKey;
this->rightKey = rightKey;
size = 3;
speed = 75;
rotateAngle = 3;
delay = 5;
nextGap = randFloat(delay);
gapLength = size / speed * 10;
lineTimerEnd = false;
startTimerEnd = false;
noLine = true;
alive = true;
//lines.push_back(Line(size, color));
//lines[lines.size() - 1].addPoint(position);
startCircle.setFillColor(color);
startCircle.setOrigin(size, size);
startCircle.setRadius(size);
startCircle.setPosition(position);
endCircle.setFillColor(color);
endCircle.setOrigin(size, size);
endCircle.setRadius(size);
endCircle.setPosition(position);
board->addSnake(this);
}
void Snake::control(float dt) {
if (sf::Keyboard::isKeyPressed(rightKey)) {
velocity = rotateVector(velocity, rotateAngle*dt);
}
if (sf::Keyboard::isKeyPressed(leftKey)) {
velocity = rotateVector(velocity, -rotateAngle*dt);
}
if (!checkSelfCollision()) {
position += velocity * speed * dt;
}
endCircle.setPosition(position);
}
void Snake::draw(sf::RenderWindow &window) {
window.draw(startCircle);
window.draw(endCircle);
for (Line *line : lines) {
line->draw(window);
}
}
sf::Vector2f Snake::getLastPoint() {
return lines[lines.size() - 1]->getLine()[lines[lines.size() - 1]->getLine().size() - 1];
}
void Snake::update(float dt) {
if (!alive) {
return;
}
startCollision();
control(dt);
if (checkSelfCollision() || checkWallCollision() || (!noLine && board->checkEnemyCollision(this))) {
alive = false;
}
board->checkPowerUpCollision(this);
if (lineTimer.getElapsedTime().asSeconds() < nextGap && !noLine && dist(getLastPoint(),position) > 3 && alive) {
lines[lines.size() - 1]->addPoint(position);
}
else if (lineTimer.getElapsedTime().asSeconds() >= nextGap && !lineTimerEnd && !noLine && alive) {
lineTimerEnd = true;
lines[lines.size() - 1]->addPoint(position + velocity * size);
}
else if (lineTimer.getElapsedTime().asSeconds() > nextGap + gapLength && !noLine && alive) {
lines.push_back(new Line(size, color));
lines[lines.size() - 1]->addPoint(position - velocity * size);
lines[lines.size() - 1]->addPoint(position);
lineTimer.restart();
nextGap = randFloat(delay);
lineTimerEnd = false;
}
for (int i = 0; i < effects.size(); i++) {
if (effects[i]->isTimeUp()) {
PowerUp* eff = effects[i];
switch (eff->getPowerUpType()) {
case PowerUpType::DECREASE_MY_SIZE:
setSize(size * 2);
break;
case PowerUpType::INCREASE_MY_SIZE:
setSize(size / 2);
break;
case PowerUpType::REVERSE_MY_KEYS:
reverseKeys();
break;
case PowerUpType::DECREASE_MY_SPEED:
setSpeed(speed * 2);
break;
case PowerUpType::INCREASE_MY_SPEED:
setSpeed(speed / 2);
break;
case PowerUpType::MAKE_ME_POINTY:
break;
case PowerUpType::MAKE_ME_DOT:
lineOn();
break;
case PowerUpType::MAKE_WALL_OFF_FOR_ME:
break;
}
effects.erase(effects.begin() + i);
delete eff;
}
}
}
void Snake::startCollision() {
if (startTimer.getElapsedTime().asSeconds() < 2) {
lineOff();
startCircle.setPosition(position);
}
else if (!startTimerEnd && startTimer.getElapsedTime().asSeconds() > 2) {
lineOn();
startTimerEnd = true;
}
}
void Snake::lineOn() {
noLine = false;
lines.push_back(new Line(size, color));
lines[lines.size() - 1]->addPoint(position);
}
void Snake::lineOff() {
noLine = true;
}
bool Snake::checkSelfCollision() {
if (noLine || lineTimerEnd) {
return false;
}
for (int i = 0; i < lines.size(); i++) {
for (int j = 1; j < lines[i]->getLine().size() - 1; j++) {
if (dist(position, lines[i]->getLine()[j]) < size + lines[i]->getThicc() && !(i == lines.size() - 1 && lines[i]->getSize() - j < 10*size)) {
return true;
}
}
}
return false;
}
bool Snake::checkWallCollision() {
if (position.x - size < board->getOffset().x || position.y - size < board->getOffset().y || position.x + size > board->getSize().x || position.y + size > board->getSize().y) {
return true;
}
return false;
}
sf::Vector2f Snake::getPosition() {
return position;
}
float Snake::getSize() {
return size;
}
std::vector<Line*>* Snake::getLines() {
return &lines;
}
void Snake::setSize(float size) {
float diff = size / this->size;
this->size = size;
endCircle.setRadius(size);
endCircle.setOrigin(size, size);
gapLength *= diff;
lines[lines.size() - 1]->setThicc(size);
}
void Snake::addEffect(PowerUp* effect) {
this->effects.push_back(effect);
effect->startTimer();
}
void Snake::reverseKeys() {
sf::Keyboard::Key temp = rightKey;
rightKey = leftKey;
leftKey = temp;
}
void Snake::setSpeed(float speed) {
float diff = speed / this->speed;
this->speed = speed;
rotateAngle *= diff;
gapLength /= diff;
}
float Snake::getSpeed() {
return speed;
}
void Snake::earseSnake() {
for (int i = 0; i < lines.size(); i++)
{
lines[i]->clear();
delete lines[i];
}
lines.resize(0);
lines.push_back(new Line(size, color));
lines[lines.size() - 1]->addPoint(position);
startCircle.setPosition(position);
}