/
AlienScoutEntity.cpp
77 lines (64 loc) · 1.64 KB
/
AlienScoutEntity.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
#include "AlienScoutEntity.h"
AlienScoutEntity::AlienScoutEntity(Image* i, Rectangle* c, EntityFactory* f, int x, int y) :
SceneNode(ENTITY_ENEMY, x-ENTITY_WIDTH/2, y-ENTITY_HEIGHT/2, ENTITY_WIDTH, ENTITY_HEIGHT)
{
image = i;
camera = c;
factory = f;
cycle = 0;
startY = y;
active = false;
health = 10;
}
AlienScoutEntity::~AlienScoutEntity()
{
}
void AlienScoutEntity::update()
{
if(health <= 0)
{
remove();
return;
}
if(active)
{
cycle+=PI/20;
if(cycle > 2*PI)
cycle = 0;
setY(startY + sin(cycle)*ENTITY_HEIGHT);
setX(getX()-ENTITY_SPEED);
if(!overlaps(*camera))
remove();
std::list<SceneNode*>* nodes = getScene()->getNodes();
for(list<SceneNode*>::iterator it = nodes->begin(); it != nodes->end(); it++)
{
if((*it)->getID() == ENTITY_PLAYER && overlaps(**it))
{
PlayerEntity* player = (PlayerEntity*)(*it);
player->damage(5);
getScene()->addNode(factory->makeEntity(ENTITY_EXPLOSION, getCenterX(), getCenterY()));
remove();
}
}
}
else
{
if(overlaps(*camera))
active = true;
}
}
void AlienScoutEntity::draw(Rectangle* view, Graphics* g)
{
if(active)
{
if(image != NULL)
{
int frame = NUM_FRAMES/2 + sin(cycle)*NUM_FRAMES/2;
image->draw(getCenterX() - getWidth()/2 - view->getX(), getCenterY() - getHeight()/2 - view->getY(), frame, g);
}
}
}
void AlienScoutEntity::damage(int d)
{
health -= d;
}