Shortened and improved path following code.

This commit is contained in:
Felix Ableitner 2013-03-25 16:58:13 +01:00
parent ff65f6ceb6
commit eb2bcf1d6d
2 changed files with 13 additions and 21 deletions

View File

@ -127,14 +127,14 @@ Character::releaseTrigger() {
*/
bool
Character::setDestination(const sf::Vector2f& destination) {
//mPath = mPathfinder.getPath(*this, destination);
// Make sure we found a path.
if (mPath.empty()) {
LOG_I("No path found to destination.");
} else {
mStartPathfinding = true;
mPath = mWorld.getPath(getPosition(), destination, getRadius());
if (!mPath.empty()) {
setSpeed(mPath.back() - getPosition(), mMovementSpeed);
}
return mStartPathfinding;
else {
LOG_W("No path found to destination.");
}
return !mPath.empty();
}
/**
@ -144,20 +144,12 @@ Character::setDestination(const sf::Vector2f& destination) {
void
Character::move() {
if (!mPath.empty()) {
if (mStartPathfinding) {
setSpeed(*mPath.end() - getPosition(), mMovementSpeed);
}
if (thor::length(*mPath.end() - getPosition()) < POINT_REACHED_DISTANCE) {
// Reached a path node.
if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) {
mPath.pop_back();
if (!mPath.empty()) {
// Move to next path node.
setSpeed(*mPath.end() - getPosition(), mMovementSpeed);
}
else {
LOG_I("Reached destination.");
setSpeed(sf::Vector2f(), 0);
}
(!mPath.empty())
? setSpeed(mPath.back() - getPosition(), mMovementSpeed)
: setSpeed(sf::Vector2f(), 0);
}
}
}

View File

@ -53,7 +53,7 @@ private:
static const float DEFAULT_SPEED;
static const std::string KEY_WEAPON;
static const std::string DEFAULT_WEAPON;
/// The distance to a point where it is considered reached.
/// The distance to a point where it is considered reached (in pixels).
static const float POINT_REACHED_DISTANCE;
static std::vector<Character*> mCharacterInstances;