-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cpp
118 lines (99 loc) · 3.18 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "Enemy.hpp"
#include "Prop.hpp"
#include "Weapon.hpp"
#include "Object.hpp"
#include "ScenePlayable.hpp"
#include "DungeonDoor.hpp"
Enemy::Enemy(ScenePlayable* scene, Map* map, sf::Vector2f pos) : _scene(scene), _map(map) {
_speed = sf::Vector2f(0,0);
_hp = 5;
_dead = false;
_damage = 0;
_elapsedWalking = 0;
_elapsedAnimation = 0;
_currentAnimation = 0;
_hitedTimer = 0;
_moving = false;
_attacking = false;
_sprite.setPosition(pos);
_collisionMask = collisionMapMask::ground;
}
Enemy::~Enemy() {}
void Enemy::update(float deltaTime) {
_hitedTimer -= deltaTime;
if(_moving) {
_elapsedAnimation -= deltaTime;
if (_elapsedAnimation < 0) {
_elapsedAnimation += _elapsedWalking;
_currentAnimation = (_currentAnimation+1)%_description[_action*4+_dir].size();
}
}
_sprite.setTextureRect(_description[_action*4+_dir][_currentAnimation%_description[_action*4+_dir].size()]);
if (_attacking) {
_elapsedAttack -= deltaTime;
if (_elapsedAttack <= 0) {
_action = linkActions::move;
_attacking = false;
}
}
else if (_moving) {
sf::Vector2f movement, initial;
initial = _sprite.getPosition();
movement.x = (_dir == directions::left ? -_speed.x : (_dir == directions::right ? _speed.x : 0));
movement.y = (_dir == directions::up ? -_speed.y : (_dir == directions::down ? _speed.y : 0));
cmove(_map->getMaxMovement(initial,movement*deltaTime,_walkBounds, _collisionMask, false));
_moving = false;
}
}
void Enemy::draw(sf::RenderTarget* window) {
if (_hitedTimer > 0) {
Resources::cInvert.setParameter("deltaTime", _hitedTimer);
window->draw(_sprite,&Resources::cInvert);
}
else window->draw(_sprite);
}
void Enemy::setMap(Map* map) {
_map = map;
}
void Enemy::getHit(float much, sf::Vector2f) {
if (_hitedTimer > 0) return;
Resources::cInvert.setParameter("Time", 1);
_hitedTimer = 1; // One second of invulneravility;
_hp -= much;
_dead = _hp <= 0;
if (_dead) drop();
}
void Enemy::drop() {
_scene->addObject(new Object(objectType(std::rand()%int(objectType::objectsQtty)),
sf::Vector2f(_sprite.getPosition().x+_bounds.left+_bounds.width/2,
_sprite.getPosition().y+_bounds.top+_bounds.height/2), _scene));
}
bool Enemy::isAlive() {
return !_dead;
}
float Enemy::getDamage() {
return _damage;
}
sf::IntRect Enemy::getWalkBounds() {
return _walkBounds;
}
sf::IntRect Enemy::getGlobalWalkBounds() {
return sf::IntRect(_walkBounds.left+_sprite.getPosition().x, _walkBounds.top + _sprite.getPosition().y, _walkBounds.width, _walkBounds.height);
}
void Enemy::intersectsWith(Collisionable* c) {
Weapon* weapon = dynamic_cast<Weapon*>(c);
if (weapon != nullptr) {
getHit(weapon->getDamage(),sf::Vector2f(0,0));
return;
}
Prop* prop = dynamic_cast<Prop*>(c);
if (prop != nullptr) {
resetMove();
return;
}
DungeonDoor* door = dynamic_cast<DungeonDoor*>(c);
if (door != nullptr) {
if (!door->isOpened()) resetMove();
return;
}
}