Fixed Character path finding to not skip points.
This commit is contained in:
parent
d585a82ada
commit
adfb49b493
|
@ -20,7 +20,6 @@ const std::string Character::KEY_HEALTH = "health";
|
||||||
const int Character::DEFAULT_HEALTH = 100;
|
const int Character::DEFAULT_HEALTH = 100;
|
||||||
const std::string Character::KEY_SPEED = "speed";
|
const std::string Character::KEY_SPEED = "speed";
|
||||||
const float Character::DEFAULT_SPEED = 100;
|
const float Character::DEFAULT_SPEED = 100;
|
||||||
const float Character::POINT_REACHED_DISTANCE = 1.0f;
|
|
||||||
const std::string Character::KEY_WEAPON = "weapon";
|
const std::string Character::KEY_WEAPON = "weapon";
|
||||||
const std::string Character::DEFAULT_WEAPON = "weapon.yaml";
|
const std::string Character::DEFAULT_WEAPON = "weapon.yaml";
|
||||||
const float Character::VISION_DISTANCE = 500.0f;
|
const float Character::VISION_DISTANCE = 500.0f;
|
||||||
|
@ -37,7 +36,8 @@ Character::Character(World& world, TileManager& tileManager, const Data& data,
|
||||||
mCurrentHealth(mMaxHealth),
|
mCurrentHealth(mMaxHealth),
|
||||||
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
||||||
mWeapon(new Weapon(world, *this,
|
mWeapon(new Weapon(world, *this,
|
||||||
Yaml(config.get(KEY_WEAPON, DEFAULT_WEAPON)))) {
|
Yaml(config.get(KEY_WEAPON, DEFAULT_WEAPON)))),
|
||||||
|
mLastPosition(getPosition()){
|
||||||
}
|
}
|
||||||
|
|
||||||
Character::~Character() {
|
Character::~Character() {
|
||||||
|
@ -121,8 +121,10 @@ Character::setDestination(const sf::Vector2f& destination) {
|
||||||
mPath = mWorld.getPath(getPosition(), destination, getRadius());
|
mPath = mWorld.getPath(getPosition(), destination, getRadius());
|
||||||
if (!mPath.empty())
|
if (!mPath.empty())
|
||||||
setSpeed(mPath.back() - getPosition(), mMovementSpeed);
|
setSpeed(mPath.back() - getPosition(), mMovementSpeed);
|
||||||
else
|
else {
|
||||||
|
setSpeed(sf::Vector2f(), 0);
|
||||||
LOG_W("No path found to destination.");
|
LOG_W("No path found to destination.");
|
||||||
|
}
|
||||||
return !mPath.empty();
|
return !mPath.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,8 +134,11 @@ Character::setDestination(const sf::Vector2f& destination) {
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Character::move() {
|
Character::move() {
|
||||||
|
sf::Vector2f distanceTraveled = mLastPosition - getPosition();
|
||||||
|
mLastPosition = getPosition();
|
||||||
if (!mPath.empty()) {
|
if (!mPath.empty()) {
|
||||||
if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) {
|
// Point reached (during next time step).
|
||||||
|
if (thor::length(mPath.back() - getPosition()) < thor::length(distanceTraveled)) {
|
||||||
mPath.pop_back();
|
mPath.pop_back();
|
||||||
(!mPath.empty())
|
(!mPath.empty())
|
||||||
? setSpeed(mPath.back() - getPosition(), mMovementSpeed)
|
? setSpeed(mPath.back() - getPosition(), mMovementSpeed)
|
||||||
|
|
|
@ -47,8 +47,6 @@ 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 (in pixels).
|
|
||||||
static const float POINT_REACHED_DISTANCE;
|
|
||||||
/// Maximum distance where an enemy will be detected.
|
/// Maximum distance where an enemy will be detected.
|
||||||
static const float VISION_DISTANCE;
|
static const float VISION_DISTANCE;
|
||||||
|
|
||||||
|
@ -61,6 +59,7 @@ private:
|
||||||
const float mMovementSpeed;
|
const float mMovementSpeed;
|
||||||
std::unique_ptr<Weapon> mWeapon;
|
std::unique_ptr<Weapon> mWeapon;
|
||||||
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
|
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
|
||||||
|
sf::Vector2f mLastPosition;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* DG_ACTOR_H_ */
|
#endif /* DG_ACTOR_H_ */
|
||||||
|
|
Reference in New Issue