-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
179 lines (167 loc) · 5.12 KB
/
Board.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
#include "Snake.h"
#include <iostream>
Board::Board(int width,int height) {
size = sf::Vector2f(width, height);
offset = sf::Vector2f(5, 5);
background.setOutlineColor(sf::Color::White);
background.setSize(size - 2.f * offset);
background.setOutlineThickness(offset.x);
background.setFillColor(sf::Color::Transparent);
background.setPosition(offset);
delay = 7.f;
nextPowerUp = 3.f + randFloat(delay);
generateThree = false;
}
sf::Vector2f Board::getSize() {
return size - offset;
}
sf::Vector2f Board::getOffset() {
return offset;
}
void Board::setColor(sf::Color color) {
background.setOutlineColor(color);
}
void Board::updatePowerUp() {
if (timer.getElapsedTime().asSeconds() > 3) {
powerUps.push_back(new PowerUp(sf::Vector2f( 50 + randFloat(size.x - 100), 50 + randFloat(size.y - 100)), PowerUpType(14 + rand() % 3)));
nextPowerUp = 5 + randFloat(delay);
timer.restart();
}
if (generateThree && generateThreeTimer.getElapsedTime().asMilliseconds() > 300 * generateThreeCount) {
generateThreeCount++;
powerUps.push_back(new PowerUp(sf::Vector2f(50 + randFloat(size.x - 100), 50 + randFloat(size.y - 100)), PowerUpType(rand() % 18)));
if (generateThree > 4) {
generateThree = false;
}
}
}
void Board::draw(sf::RenderWindow &window) {
window.draw(background);
for (PowerUp* pu : powerUps) {
pu->draw(window);
}
}
void Board::checkPowerUpCollision(Snake *s) {
for (int i = 0; i < powerUps.size(); i++) {
if (dist(s->getPosition(), powerUps[i]->getPosition()) < powerUps[i]->getRadius() + s->getSize()) {
PowerUp* pu = powerUps[i];
switch (pu->getPowerUpType()) {
case PowerUpType::DECREASE_MY_SIZE:
s->setSize(s->getSize() / 2);
s->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::DECREASE_MY_SIZE));
break;
case PowerUpType::DECREASE_ENEMY_SIZE:
for (int j = 0; j < snakes.size(); j++) {
if (snakes[j] == s) {
continue;
}
snakes[j]->setSize(snakes[j]->getSize() / 2);
snakes[j]->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::DECREASE_MY_SIZE));
}
break;
case PowerUpType::INCREASE_MY_SIZE:
s->setSize(s->getSize() * 2);
s->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::INCREASE_MY_SIZE));
break;
case PowerUpType::INCREASE_ENEMY_SIZE:
for (int j = 0; j < snakes.size(); j++) {
if (snakes[j] == s) {
continue;
}
snakes[j]->setSize(snakes[j]->getSize() * 2);
snakes[j]->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::INCREASE_MY_SIZE));
}
break;
case PowerUpType::REVERSE_MY_KEYS:
s->reverseKeys();
s->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::REVERSE_MY_KEYS));
break;
case PowerUpType::REVERSE_ENEMY_KEYS:
for (int j = 0; j < snakes.size(); j++) {
if (snakes[j] == s) {
continue;
}
snakes[j]->reverseKeys();
snakes[j]->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::REVERSE_MY_KEYS));
}
break;
case PowerUpType::DECREASE_MY_SPEED:
s->setSpeed(s->getSpeed() / 2);
s->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::DECREASE_MY_SPEED));
break;
case PowerUpType::DECREASE_ENEMY_SPEED:
for (int j = 0; j < snakes.size(); j++) {
if (snakes[j] == s) {
continue;
}
snakes[j]->setSpeed(snakes[j]->getSpeed() / 2);
snakes[j]->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::DECREASE_MY_SPEED));
}
break;
case PowerUpType::INCREASE_MY_SPEED:
s->setSpeed(s->getSpeed() * 2);
s->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::INCREASE_MY_SPEED));
break;
case PowerUpType::INCREASE_ENEMY_SPEED:
for (int j = 0; j < snakes.size(); j++) {
if (snakes[j] == s) {
continue;
}
snakes[j]->setSpeed(snakes[j]->getSpeed() * 2);
snakes[j]->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::INCREASE_ENEMY_SPEED));
}
break;
case PowerUpType::MAKE_ME_POINTY:
break;
case PowerUpType::MAKE_ENEMY_POINTY:
break;
case PowerUpType::MAKE_ME_DOT:
s->lineOff();
s->addEffect(new PowerUp(sf::Vector2f(0, 0), PowerUpType::MAKE_ME_DOT));
break;
case PowerUpType::MAKE_WALL_OFF:
break;
case PowerUpType::GENERTE_THREE_POWERUPS:
generateThree = true;
generateThreeCount = 1;
generateThreeTimer.restart();
break;
case PowerUpType::CLEAN_BOARD:
for (int j = 0; j < snakes.size(); j++) {
if (snakes[j] == s) {
continue;
}
snakes[j]->earseSnake();
}
break;
case PowerUpType::CLEAN_MY_LINE:
s->earseSnake();
break;
case PowerUpType::MAKE_WALL_OFF_FOR_ME:
break;
}
powerUps.erase(powerUps.begin() + i);
delete pu;
}
}
}
void Board::addSnake(Snake* s) {
snakes.push_back(s);
}
bool Board::checkEnemyCollision(Snake* s) {
sf::Vector2f position = s->getPosition();
for (int i = 0; i < snakes.size(); i++) {
if (s == snakes[i]) {
continue;
}
for (int j = 0; j < snakes[i]->getLines()->size(); j++) {
Line* line = (*snakes[i]->getLines())[j];
for (int k = 0; k < line->getLine().size(); k++) {
if (dist(position, line->getLine()[k]) <= s->getSize() + line->getThicc()) {
return true;
}
}
}
}
return false;
}