-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmartSprite.cpp
More file actions
53 lines (45 loc) · 1.42 KB
/
smartSprite.cpp
File metadata and controls
53 lines (45 loc) · 1.42 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
#include <cmath>
#include "viewport.h"
#include "smartSprite.h"
float distance(float x1, float y1, float x2, float y2) {
float x = x1-x2;
float y = y1-y2;
return hypot(x, y);
}
SmartSprite::SmartSprite(const std::string& name,
const Vector2f& pos, const Vector2f& vel, Drawable* p) :
Sprite(name, pos, vel ),
io(IOManager::getInstance()),
enemy(p),
safeDistance(Gamedata::getInstance().getXmlInt("safeDistance")),
currentMode(NORMAL)
{ }
void SmartSprite::goLeft() {
if (X() > 0) velocityX( -abs(velocityX()) );
}
void SmartSprite::goRight() { velocityX( fabs(velocityX()) ); }
void SmartSprite::goUp() { velocityY( -fabs(velocityY()) ); }
void SmartSprite::goDown() { velocityY( fabs(velocityY()) ); }
void SmartSprite::draw() const {
Sprite::draw();
}
void SmartSprite::update(Uint32 ticks) {
Sprite::update(ticks);
float x= X()+getFrame()->getWidth()/2;
float y= Y()+getFrame()->getHeight()/2;
float ex= enemy->X()+enemy->getFrame()->getWidth()/2;
float ey= enemy->Y()+enemy->getFrame()->getHeight()/2;
float distanceToEnemy = ::distance( x, y, ex, ey );
if ( currentMode == NORMAL ) {
if(distanceToEnemy < safeDistance) currentMode = EVADE;
}
else if ( currentMode == EVADE ) {
if(distanceToEnemy > safeDistance) currentMode=NORMAL;
else {
if ( x < ex ) goLeft();
if ( x > ex ) goRight();
if ( y < ey ) goUp();
if ( y > ey ) goDown();
}
}
}