Integrated Pathfinder class into Game/Player.

This commit is contained in:
Felix Ableitner 2012-10-01 09:12:00 +02:00
parent 69889e5edc
commit 197ef6b398
4 changed files with 48 additions and 24 deletions

View file

@ -7,6 +7,8 @@
#include "Game.h" #include "Game.h"
#include <Thor/Graphics.hpp>
#include "abstract/Actor.h" #include "abstract/Actor.h"
#include "sprite/Cover.h" #include "sprite/Cover.h"
#include "sprite/Enemy.h" #include "sprite/Enemy.h"
@ -29,8 +31,9 @@ Game::Game(const Vector2i& resolution) :
sf::Style::Close | sf::Style::Titlebar), sf::Style::Close | sf::Style::Titlebar),
mView(Vector2f(0, 0), Vector2f(resolution)), mView(Vector2f(0, 0), Vector2f(resolution)),
//mFps("test"), //mFps("test"),
mTileManager(mWorld), mTileManager(mWorld),
mPlayer(mWorld, mCollection, Vector2f(200.0f, 100.0f)), mPathfinder(mWorld),
mPlayer(mWorld, mCollection, Vector2f(200.0f, 100.0f), mPathfinder),
mElapsed(0), mElapsed(0),
mQuit(false), mQuit(false),
mPaused(false) { mPaused(false) {
@ -39,12 +42,10 @@ Game::Game(const Vector2i& resolution) :
mWorld.SetContactListener(this); mWorld.SetContactListener(this);
mTileManager.generate(); mTileManager.generate();
for (int i = 0; i < 500; i += 50) {
mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(i, i), Vector2i(20, 20),
mWorld)));
}
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f), mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f),
mCollection))); mCollection)));
mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(300, 200), Vector2i(100, 150),
mWorld)));
} }
/** /**

View file

@ -15,6 +15,7 @@
#include <Box2D/Box2D.h> #include <Box2D/Box2D.h>
#include "Pathfinder.h"
#include "TileManager.h" #include "TileManager.h"
#include "sprite/Player.h" #include "sprite/Player.h"
#include "util/Collection.h" #include "util/Collection.h"
@ -61,6 +62,7 @@ private:
Collection mCollection; Collection mCollection;
TileManager mTileManager; TileManager mTileManager;
Pathfinder mPathfinder;
Player mPlayer; Player mPlayer;
/// Milliseconds since the last tick. /// Milliseconds since the last tick.

View file

@ -13,19 +13,20 @@
#include "../items/Weapon.h" #include "../items/Weapon.h"
const float Player::SPEED = 100.0f; const float Player::SPEED = 100.0f;
const Vector2i Player::SIZE = Vector2i(50, 50); const Vector2i Player::SIZE = Vector2i(50, 50);
const float Player::POINT_REACHED_DISTANCE = 1.0f;
/** /**
* Initializes Sprite. * Initializes Sprite.
*/ */
Player::Player(b2World& world, Collection& collection, const Vector2f& position) : Player::Player(b2World& world, Collection& collection, const Vector2f& position,
Pathfinder& pathfinder) :
Sprite("player.png", PhysicalData(position, SIZE, world, Sprite("player.png", PhysicalData(position, SIZE, world,
CATEGORY_ACTOR, MASK_ALL, true, false, true)), CATEGORY_ACTOR, MASK_ALL, true, false, true)),
Actor(100), Actor(100),
mWeapon(*this, collection, world, SIZE), mWeapon(*this, collection, world, SIZE),
mDestination(Vector2i(position)),
mDirection(0), mDirection(0),
mDirectInput(false) { mPathfinder(pathfinder) {
} }
/** /**
@ -54,10 +55,15 @@ Player::fire() {
*/ */
void void
Player::move(const Vector2f& destination) { Player::move(const Vector2f& destination) {
mDestination = destination; mPath = mPathfinder.getPath(*this, destination);
// Convert to relative destination. // Make sure we found a path.
setSpeed(mDestination - getPosition(), SPEED); if (mPath != std::vector<Vector2f>()) {
mDirectInput = false; setSpeed(*mPath.end() - getPosition(), SPEED);
}
// Otherwise stop (in case this was called during movement).
else {
setSpeed(Vector2f(), 0);
}
} }
/** /**
@ -89,7 +95,6 @@ Player::setDirection(Direction direction, bool unset) {
dirVec.y += - 1.0f; dirVec.y += - 1.0f;
} }
setSpeed(dirVec, SPEED); setSpeed(dirVec, SPEED);
mDirectInput = true;
} }
/** /**
@ -98,8 +103,19 @@ Player::setDirection(Direction direction, bool unset) {
void void
Player::onThink(float elapsedTime) { Player::onThink(float elapsedTime) {
// Stop if we are close enough to destination. // Stop if we are close enough to destination.
if (!mDirectInput && (thor::length(mDestination - getPosition()) < 1.0f)) { if (!mPath.empty()) {
setSpeed(Vector2f(), 0); // Reached a point.
if (thor::length(*mPath.end() - getPosition()) < POINT_REACHED_DISTANCE) {
mPath.pop_back();
if (!mPath.empty()) {
// Move to next.
setSpeed(*mPath.end() - getPosition(), SPEED);
}
else {
// Reached destination.
setSpeed(Vector2f(), 0);
}
}
} }
// Look towards crosshair. // Look towards crosshair.
setAngle(angle(mCrosshairPosition)); setAngle(angle(mCrosshairPosition));
@ -111,6 +127,6 @@ Player::onThink(float elapsedTime) {
void void
Player::onCollide(Physical& other, uint16 category) { Player::onCollide(Physical& other, uint16 category) {
if (category != CATEGORY_PARTICLE) { if (category != CATEGORY_PARTICLE) {
mDestination = getPosition(); mPath.clear();
} }
} }

View file

@ -11,12 +11,14 @@
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "../Pathfinder.h"
#include "../abstract/Actor.h" #include "../abstract/Actor.h"
#include "../abstract/Sprite.h" #include "../abstract/Sprite.h"
#include "../items/Weapon.h" #include "../items/Weapon.h"
#include "../util/Vector.h" #include "../util/Vector.h"
class Actor; class Actor;
class Pathfinder;
class Sprite; class Sprite;
class Weapon; class Weapon;
@ -38,7 +40,7 @@ public:
// Public functions. // Public functions.
public: public:
Player(b2World& world, Collection& collection, const Vector2f& position); Player(b2World& world, Collection& collection, const Vector2f& position, Pathfinder& pathfinder);
void setCrosshairPosition(const Vector2f& position); void setCrosshairPosition(const Vector2f& position);
void fire(); void fire();
@ -54,13 +56,16 @@ private:
private: private:
static const float SPEED; static const float SPEED;
static const Vector2i SIZE; static const Vector2i SIZE;
/// The distance to a point where it is considered reached.
static const float POINT_REACHED_DISTANCE;
Weapon mWeapon; //< Weapon object used for Player::fire(). Weapon mWeapon; //< Weapon object used for Player::fire().
Vector2f mDestination; //< Absolute position of the movement destination.
Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor). Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor).
uint8 mDirection; //< Current movement direction for direct control. uint8 mDirection; //< Current movement direction for direct control.
bool mDirectInput; //< True when using keyboard input (directions), false when using
// destination point. Pathfinder& mPathfinder;
std::vector<Vector2f> mPath;
}; };
#endif /* DG_PLAYER_H_ */ #endif /* DG_PLAYER_H_ */