-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.cpp
46 lines (40 loc) · 1.23 KB
/
enemy.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
#include <boost/lexical_cast.hpp>
#include "enemy.hpp"
#include "levelset.hpp"
#include "player.hpp"
#include "utils.hpp"
#include "globals.hpp"
Enemy::Enemy(const std::vector<std::string>& args)
{
if (args.size() != 8)
throw std::runtime_error("Invalid enemy definition");
horiz = args[0] == "henemy";
text = GetTexture(args[1]);
x = boost::lexical_cast<int>(args[2]);
y = boost::lexical_cast<int>(args[3]);
width = boost::lexical_cast<int>(args[4]);
height = boost::lexical_cast<int>(args[5]);
min = boost::lexical_cast<int>(args[6]);
max = boost::lexical_cast<int>(args[7]);
}
void Enemy::Simul(double dt)
{
double& c = horiz ? x : y;
c += (posdir ? dt : -dt) * 300;
if (c <= min) { posdir = true; c = min; }
if (c >= max) { posdir = false; c = max; }
}
void Enemy::Render()
{
SDL_Rect r = { int(x)*SCREEN_MUL, int(y)*SCREEN_MUL,
width*SCREEN_MUL, height*SCREEN_MUL };
Color c = level.Color();
SDL_SetTextureColorMod(text.get(), c.r, c.g, c.b);
SDL_RenderCopy(renderer, text.get(), nullptr, &r);
}
void Enemy::Interact(Player& p)
{
p.Kill();
}
static EntityFactoryImpl<Enemy> henemy_fact("henemy");
static EntityFactoryImpl<Enemy> venemy_fact("venemy");