Changed Enemies to look into movement direction.

This commit is contained in:
Felix Ableitner 2013-06-21 18:00:38 +02:00
parent 51cf1d9c68
commit fd7173adee
4 changed files with 19 additions and 20 deletions

View file

@ -17,6 +17,7 @@
#include "../Pathfinder.h" #include "../Pathfinder.h"
const float Character::VISION_DISTANCE = 500.0f; const float Character::VISION_DISTANCE = 500.0f;
const float Character::POINT_REACHED_DISTANCE = 1.0f;
/** /**
* Saves pointer to this instance in static var for think(). * Saves pointer to this instance in static var for think().
@ -118,17 +119,7 @@ Character::releaseTrigger() {
*/ */
bool bool
Character::setDestination(const sf::Vector2f& destination) { Character::setDestination(const sf::Vector2f& destination) {
if (destination == getPosition()) {
mPath.clear();
return true;
}
mPath = mPathfinder.getPath(getPosition(), destination, getRadius()); mPath = mPathfinder.getPath(getPosition(), destination, getRadius());
if (!mPath.empty())
setSpeed(mPath.back() - getPosition(), mMovementSpeed);
else {
setSpeed(sf::Vector2f(), 0);
LOG_W("No path found to destination.");
}
return !mPath.empty(); return !mPath.empty();
} }
@ -138,16 +129,15 @@ Character::setDestination(const sf::Vector2f& destination) {
*/ */
void void
Character::move() { Character::move() {
sf::Vector2f distanceTraveled = mLastPosition - getPosition(); if (mPath.empty())
return;
mLastPosition = getPosition(); mLastPosition = getPosition();
if (!mPath.empty()) { setSpeed(mPath.back() - getPosition(), mMovementSpeed);
// Point reached (during next time step). setDirection(mPath.back() - getPosition());
if (thor::length(mPath.back() - getPosition()) < thor::length(distanceTraveled)) { if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) {
mPath.pop_back(); mPath.pop_back();
(!mPath.empty()) if (mPath.empty())
? setSpeed(mPath.back() - getPosition(), mMovementSpeed) setSpeed(sf::Vector2f(), 0);
: setSpeed(sf::Vector2f(), 0);
}
} }
} }

View file

@ -52,6 +52,9 @@ private:
void move(); void move();
private: private:
/// Distance to a path point where it will be considered as reached (to
/// avoid floating point equality check).
static const float POINT_REACHED_DISTANCE;
friend class World; friend class World;
World& mWorld; World& mWorld;
Pathfinder& mPathfinder; Pathfinder& mPathfinder;

View file

@ -133,6 +133,10 @@ Sprite::setSpeed(sf::Vector2f direction, float speed) {
mSpeed = direction; mSpeed = direction;
} }
/**
* Rotates sprite in the direction of the vector. Vector length must not be null,
* but is otherwise meaningless.
*/
void void
Sprite::setDirection(const sf::Vector2f& direction) { Sprite::setDirection(const sf::Vector2f& direction) {
if (direction != sf::Vector2f()) if (direction != sf::Vector2f())

View file

@ -41,6 +41,8 @@ Enemy::onThink(int elapsed) {
else if (!isMoving()) else if (!isMoving())
setDestination(target->getPosition()); setDestination(target->getPosition());
} }
else else {
releaseTrigger(); releaseTrigger();
setSpeed(sf::Vector2f(), 0);
}
} }