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

View File

@ -53,7 +53,7 @@ private:
static const float DEFAULT_SPEED; static const float DEFAULT_SPEED;
static const std::string KEY_WEAPON; static const std::string KEY_WEAPON;
static const std::string DEFAULT_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 const float POINT_REACHED_DISTANCE;
static std::vector<Character*> mCharacterInstances; static std::vector<Character*> mCharacterInstances;