-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawner.cpp
33 lines (27 loc) · 914 Bytes
/
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
#include <stdlib.h>
#include "enemy.hpp"
#include "spawner.hpp"
#define SPAWN_INTERVAL 1500
Spawner::Spawner(std::vector<Enemy> &e, const sf::Image &lvl) : enemies(e), spawn(lvl, sf::Color::Green) {
enemyTile.LoadFromFile("resources/images/enemy.png");
largeTile.LoadFromFile("resources/images/enemy_16x16.png");
}
void Spawner::update() {
if (spawnTime.GetElapsedTime() > SPAWN_INTERVAL) {
addEnemy();
spawnTime.Reset();
}
for (int i=0; i<enemies.size(); ++i) {
if (enemies[i].dead) {
enemies[i].dead = false; // respawn =]
enemies[i].pos = spawn.getPos();
}
}
}
void Spawner::addEnemy() {
vector2<float> pos = spawn.getPos();
// 25% chance of a big one spawning
if (rand()%4==0) enemies.push_back(Enemy(pos.x, pos.y, 10, largeTile));
else enemies.push_back(Enemy(pos.x, pos.y, 2, enemyTile));
}
void Spawner::reset() { spawnTime.Reset(); }