-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpawner.cpp
90 lines (79 loc) · 1.73 KB
/
Spawner.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
#include "Spawner.h"
#include <vector>
void Spawner::act(Map *map)
{
if (isReady())
spawn(map);
_curCooldown++;
map->setHasActed(_coords);
}
Vec2i Spawner::findEmptyCell(Map *map)
{
for (int i = -1; i < 1; ++i)
{
for (int j = -1; j < 1; ++j)
{
if (map->isValidCell(_coords + Vec2i(i, j)))
{
if (map->isEmptyCell(_coords + Vec2i(i, j)))
return _coords + Vec2i(i, j);
}
}
}
return Vec2i(0, 0);
}
void Spawner::collide(Map *map, Magic *target)
{
map->clearCell(target->coordinates());
}
void Graveyard::spawn(Map *map)
{
Vec2i coords = findEmptyCell(map);
if (coords == Vec2i(0, 0))
return;
Zombie *zombie = new Zombie(coords, ZOMBIE_HP, ZOMBIE_SYMB, ZOMBIE_DAMAGE);
map->replace(coords, zombie);
_curCooldown = 0;
}
void DragonNest::spawn(Map *map)
{
Vec2i coords = findEmptyCell(map);
if (coords == Vec2i(0, 0))
return;
Dragon *dragon = new Dragon(coords, DRAGON_HP, DRAGON_SYMB, DRAGON_DAMAGE);
map->replace(coords, dragon);
_curCooldown = 0;
}
void MedkitSpawner::act(Map *map)
{
checkSpawned(map);
_isRefreshing = false;
if (_spawned.size() < 5)
_isRefreshing = true;
if (_isRefreshing)
_curCooldown++;
if (isReady())
spawn(map);
}
void MedkitSpawner::checkSpawned(Map *map)
{
std::vector<std::vector<Vec2i>::iterator> toBeDeleted;
for (auto it = _spawned.begin(); it != _spawned.end(); ++it)
{
if (map->getActor(*it)->symbol() != MEDKIT_SYMB)
toBeDeleted.push_back(it);
}
for (auto item : toBeDeleted)
_spawned.erase(item);
}
void MedkitSpawner::spawn(Map *map)
{
Vec2i pos = Vec2i(rand() % WIDTH, rand() % HEIGHT);
if (map->isEmptyCell(pos))
{
Medkit *medkit = new Medkit(pos, MEDKIT_SYMB);
map->replace(pos, medkit);
_spawned.push_back(pos);
}
_curCooldown = 0;
}