-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemyPool.cc
91 lines (78 loc) · 2.77 KB
/
EnemyPool.cc
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
#include "EnemyPool.hh"
using namespace std;
EnemyPool::EnemyPool() {
rand_pos_V.push_back(make_pair(330, 45)); //V
rand_pos_H.push_back(make_pair(30, 363)); //H
rand_pos_V.push_back(make_pair(426, 280)); //V
rand_pos_H.push_back(make_pair(204, 528)); //H
rand_pos_V.push_back(make_pair(360, 672)); //V
rand_pos_H.push_back(make_pair(1032, 381)); //H
rand_pos_V.push_back(make_pair(1555, 666)); //V
}
void EnemyPool::clear_enemies() {
pool.clear();
}
void EnemyPool::read(sf::Sprite enemySprite) {
// vector<Enemy> enemySprites;
srand(time(nullptr));
for (int i = 0; i < 3; i++) {
Enemy enemy;
enemy = Enemy(enemySprite, 100);
int random = rand() % 2 + 1;
if (random == 0) {
random = rand() % rand_pos_V.size();
if (rand_pos_V.size() == 0) {
random = rand() % rand_pos_H.size();
enemy.setPosition(rand_pos_H[i].first, rand_pos_H[i].second);
rand_pos_H.erase(rand_pos_H.begin() + i);
}
else {enemy.setPosition(rand_pos_V[i].first, rand_pos_V[i].second);
rand_pos_V.erase(rand_pos_V.begin() + i);}
}
else {
random = rand() % rand_pos_H.size();
if (rand_pos_H.size() == 0) {
random = rand() % rand_pos_V.size();
enemy.setPosition(rand_pos_V[i].first, rand_pos_V[i].second);
rand_pos_V.erase(rand_pos_V.begin() + i);
}
else {
enemy.setPosition(rand_pos_H[i].first, rand_pos_H[i].second);
rand_pos_H.erase(rand_pos_H.begin() + i);
}
}
pool.push_back(enemy);
}
}
// void EnemyPool::update(float delta) {
// for (auto& enemy : pool) {
// enemy.update(delta);
// }
// }
void EnemyPool::draw_enemy(sf::RenderWindow& window) {
for (auto& enemy : pool) enemy.draw(window);
}
int EnemyPool::get_nearest_enemy(pair <int, int> pos) {
int nearest_enemy;
double min = 50;
int best = -1;
for (int i = 0; i < pool.size(); i++) {
double distance = sqrt(pow(pool[i].getPos().first - pos.first, 2) + pow(pool[i].getPos().second - pos.second, 2));
if (distance < min ) {
min = distance;
nearest_enemy = i;
}
}
return nearest_enemy;
}
void EnemyPool::delete_enemy() {
for (int i = 0; i < pool.size(); i++) {
if (pool[i].getVida() <= 0) {
pool.erase(pool.begin() + i);
}
}
}
int EnemyPool::take_damage(int hit_points, int id) {
pool[id].take_damage(hit_points);
return pool[id].getVida();
}