-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.cpp
More file actions
55 lines (48 loc) · 1.74 KB
/
enemy.cpp
File metadata and controls
55 lines (48 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
51
52
53
54
55
#include "enemy.hpp"
#include "components.hpp"
#include "defs.hpp"
Enemy::Enemy(int x, int y, Entity* p) {
n_xpos = xpos = x;
n_ypos = ypos = y;
//width = TILESHEET_SIZE * SCALING;
//height = TILESHEET_SIZE * SCALING;
width = 23;
height = 12;
player = p;
generateTag();
init();
addAnimations();
}
void Enemy::addAnimations() {
GraphicsComponent* gc = &getComponent<GraphicsComponent>(GRAPHICS_COMPONENT);
//gc->addAnimation(WALK, SDL_Rect { 15 * TILESHEET_SIZE, 0 * TILESHEET_SIZE, TILESHEET_SIZE, TILESHEET_SIZE }, 1, 10);
//gc->addAnimation(PERISH, SDL_Rect { 15 * TILESHEET_SIZE, 0 * TILESHEET_SIZE, TILESHEET_SIZE, TILESHEET_SIZE }, 1, 10);
}
void Enemy::init() {
addComponent<GraphicsComponent>(GRAPHICS_COMPONENT, SDL_Rect { 20 * TILESHEET_SIZE, 12 * TILESHEET_SIZE, TILESHEET_SIZE, TILESHEET_SIZE });
addComponent<PhysicsComponent>(PHYSICS_COMPONENT);
addComponent<ColliderComponent>(COLLIDER_COMPONENT, ENEMY);
addComponent<HealthComponent>(HEALTH_COMPONENT, 40, 40);
addAnimations();
initComponents();
}
void Enemy::update() {
GraphicsComponent* gc = &getComponent<GraphicsComponent>(GRAPHICS_COMPONENT);
if (getComponent<HealthComponent>(HEALTH_COMPONENT).dead) {
gc->setAnimation(PERISH);
if (gc->animation_complete) {
mark_remove = true;
}
} else {
if ((player->xpos - xpos) * (player->xpos - xpos) + (player->ypos - ypos) * (player->ypos - ypos) < 10000) {
PhysicsComponent *pc = &getComponent<PhysicsComponent>(PHYSICS_COMPONENT);
pc->xvel = player->xpos - xpos;
gc->setAnimation(WALK);
}
}
updateComponents();
}
void Enemy::draw() {
drawComponents();
}
Enemy::~Enemy() {}