From fd7173adeec064a953dae47c6bbec6493d987f66 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 21 Jun 2013 18:00:38 +0200 Subject: [PATCH] Changed Enemies to look into movement direction. --- source/abstract/Character.cpp | 28 +++++++++------------------- source/abstract/Character.h | 3 +++ source/abstract/Sprite.cpp | 4 ++++ source/sprites/Enemy.cpp | 4 +++- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index a15c77f..5d24615 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -17,6 +17,7 @@ #include "../Pathfinder.h" 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(). @@ -118,17 +119,7 @@ Character::releaseTrigger() { */ bool Character::setDestination(const sf::Vector2f& destination) { - if (destination == getPosition()) { - mPath.clear(); - return true; - } 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(); } @@ -138,16 +129,15 @@ Character::setDestination(const sf::Vector2f& destination) { */ void Character::move() { - sf::Vector2f distanceTraveled = mLastPosition - getPosition(); + if (mPath.empty()) + return; mLastPosition = getPosition(); - if (!mPath.empty()) { - // Point reached (during next time step). - if (thor::length(mPath.back() - getPosition()) < thor::length(distanceTraveled)) { - mPath.pop_back(); - (!mPath.empty()) - ? setSpeed(mPath.back() - getPosition(), mMovementSpeed) - : setSpeed(sf::Vector2f(), 0); - } + setSpeed(mPath.back() - getPosition(), mMovementSpeed); + setDirection(mPath.back() - getPosition()); + if (thor::length(mPath.back() - getPosition()) < POINT_REACHED_DISTANCE) { + mPath.pop_back(); + if (mPath.empty()) + setSpeed(sf::Vector2f(), 0); } } diff --git a/source/abstract/Character.h b/source/abstract/Character.h index c4af4b0..d9fa778 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -52,6 +52,9 @@ private: void move(); 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; World& mWorld; Pathfinder& mPathfinder; diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index b122828..7dc89b3 100755 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -133,6 +133,10 @@ Sprite::setSpeed(sf::Vector2f direction, float speed) { mSpeed = direction; } +/** + * Rotates sprite in the direction of the vector. Vector length must not be null, + * but is otherwise meaningless. + */ void Sprite::setDirection(const sf::Vector2f& direction) { if (direction != sf::Vector2f()) diff --git a/source/sprites/Enemy.cpp b/source/sprites/Enemy.cpp index e3653ae..39e5fc8 100644 --- a/source/sprites/Enemy.cpp +++ b/source/sprites/Enemy.cpp @@ -41,6 +41,8 @@ Enemy::onThink(int elapsed) { else if (!isMoving()) setDestination(target->getPosition()); } - else + else { releaseTrigger(); + setSpeed(sf::Vector2f(), 0); + } }