-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cpp
72 lines (61 loc) · 1.83 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
#include "Enemy.hpp"
#include "Game.hpp"
Enemy::Enemy(int nextPositionIndex){
_nextPositionIndex = nextPositionIndex;
reloadDistanceToEnd();
}
bool Enemy::damage(double n){
return (_life-=n)<=0;
}
void Enemy::killed(){}
void Enemy::reloadDistanceToEnd(){
if(_nextPositionIndex<0 || _nextPositionIndex>=Game::enemiesRoute.size()){
_distanceToEnd = -1;
}else{
double d = 0;
Vec2d p = _position;
for(int i=_nextPositionIndex; i<Game::enemiesRoute.size(); i++)
d += p.distance(Game::enemiesRoute[i]);
_distanceToEnd = d;
}
}
bool Enemy::tick(){
if(Game::enemiesRoute.size()<=_nextPositionIndex
|| _nextPositionIndex<0 || _distanceToEnd<=_velocity)
return true;
Vec2d p = Game::enemiesRoute[_nextPositionIndex];
double toMove = _velocity;
_distanceToEnd -= toMove;
while(toMove>0.0){
double d = _position.distance(p);
if(d<=_velocity){
_position = p;
_nextPositionIndex += 1;
/*if(Game::enemiesRoute.size()<=_nextPositionIndex){
_nextPositionIndex = -1;
return true;
}*/
toMove -= d;
p = Game::enemiesRoute[_nextPositionIndex];
}else{
Vec2d t = p-_position;
t.normalize();
t *= _velocity;
_position += t;
toMove = 0.0;
}
}
return false;
}
int Enemy::setNextPositionIndex(int nextPositionIndex){
_nextPositionIndex = nextPositionIndex;
reloadDistanceToEnd();
}
double Enemy::getDistanceToNextPosition() const{
if(_nextPositionIndex<0 || Game::enemiesRoute.size()<=_nextPositionIndex)
return -1;
return _position.distance(Game::enemiesRoute[_nextPositionIndex]);
}
double Enemy::getDistanceToEnd() const{
return _distanceToEnd;
}