-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy_building.cpp
More file actions
50 lines (43 loc) · 1.74 KB
/
enemy_building.cpp
File metadata and controls
50 lines (43 loc) · 1.74 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
#include "enemy_building.h"
#include "game_map.h"
#include "enemy.h"
#include "map_cell.h"
const Position& EnemyBuilding::getPosition() const { return position; }
EnemyBuilding::EnemyBuilding(int x, int y, int level, int interval, int enemyHealth, int enemyDamage)
: position(x, y),
level(level),
count(0),
interval(interval),
spawnedEnemyHealth(enemyHealth),
spawnedEnemyDamage(enemyDamage) {}
void EnemyBuilding::spawn(std::vector<Enemy*>& enemies, GameMap& map){
count++;
if(count >= interval){
int SPAWN_DIRECTIONS[TOTAL_SPAWN_DIRECTIONS][2] = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for(int i = TOTAL_SPAWN_DIRECTIONS - 1; i > 0; i--) {
int j = rand() % (i + 1);
std::swap(SPAWN_DIRECTIONS[i][0], SPAWN_DIRECTIONS[j][0]);
std::swap(SPAWN_DIRECTIONS[i][1], SPAWN_DIRECTIONS[j][1]);
}
for(int i = 0; i < TOTAL_SPAWN_DIRECTIONS; i++){
int new_x = position.getX() + SPAWN_DIRECTIONS[i][0];
int new_y = position.getY() + SPAWN_DIRECTIONS[i][1];
Position spawn_pos(new_x, new_y);
if (map.isPositionValid(spawn_pos)) {
MapCell& cell = map.getCell(spawn_pos);
if (cell.getType() == MapCell::Type::EMPTY && !cell.isUsed()) {
cell.setUsed(true);
Enemy* enemy = new Enemy(new_x, new_y, level, spawnedEnemyHealth, spawnedEnemyDamage);
enemies.push_back(enemy);
cell.setEntity(enemy);
count = 0;
break;
}
}
}
}
}