-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
80 lines (74 loc) · 2.35 KB
/
Enemy.cpp
File metadata and controls
80 lines (74 loc) · 2.35 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Enemy.hpp"
#include "Game.hpp"
namespace rpf {
Enemy::Enemy(ResourceHolder* rh, Game* g, int ix, int iy, int id) {
this->id = id;
this->rh = rh;
this->ix = ix;
this->iy = iy;
this->_game = g;
this->update();
spr.setScale(0.09, 0.09);
spr.setOrigin(0, 64);
spr.setPosition(ix * rh->tile_size, iy * rh->tile_size);
}
void Enemy::update() {
if (killed) {
anim_index++;
spr.setTexture(rh->enemy_die);
spr.move(0.5, -0.1);
return;
}
if (anim_index >= 2 * anim_speed)
anim_index = 0;
spr.setTexture(rh->enemy[anim_index++ / anim_speed]);
/*if (AI) {
update_current_tiles();
check_move_x();
spr.setTextureRect(getFaceRect(spr.getTexture()->getSize()));
}*/
}
void Enemy::dead() {
killed = true;
anim_index = 0;
}
void Enemy::check_move_x() {
x_speed = flip ? -unit_speed : unit_speed;
int dx = x_speed > 0 ? current_tile_x_right + 1 : current_tile_x_left - 1;
float ix = x_speed > 0 ? -(spr.getGlobalBounds().left + spr.getGlobalBounds().width) : rh->tile_size - spr.getGlobalBounds().left;
for (int i = current_tile_y_top; i <= current_tile_y_bottom; i++)
{
if (!_game->is_empty_block(dx, i))
{
float dist_to_obstacle = _game->get_cord_of_tile(dx, i).x + ix;
if (x_speed > 0 && x_speed > dist_to_obstacle) {
spr.move(dist_to_obstacle - 1, 0);
flip = !flip;
return;
}
else if (x_speed <= 0 && x_speed <= dist_to_obstacle) {
spr.move(dist_to_obstacle + 1, 0);
flip = !flip;
return;
}
}
}
if (!_game->is_empty_block(flip ? current_tile_x_left : current_tile_x_right, current_tile_y_bottom + 1))
spr.move(x_speed, 0);
else
flip = !flip;
}
void Enemy::update_current_tiles()
{
current_tile_x_left = (spr.getGlobalBounds().left - _game->getMap().getPosition().x) / (rh->tile_size);
current_tile_x_right = ((spr.getGlobalBounds().left + spr.getGlobalBounds().width - 4) - _game->getMap().getPosition().x) / (rh->tile_size);
current_tile_y_top = (spr.getGlobalBounds().top - _game->getMap().getPosition().y) / (rh->tile_size);
current_tile_y_bottom = ((spr.getGlobalBounds().top + spr.getGlobalBounds().height) - _game->getMap().getPosition().y) / (rh->tile_size);
}
sf::IntRect Enemy::getFaceRect(sf::Vector2u size) {
if (!flip)
return sf::IntRect(size.x, 0, -1 * size.x, size.y);
else
return sf::IntRect(0, 0, size.x, size.y);
}
}