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 <Thor/Graphics.hpp>
#include "abstract/Actor.h"
#include "sprite/Cover.h"
#include "sprite/Enemy.h"
@ -29,8 +31,9 @@ Game::Game(const Vector2i& resolution) :
sf::Style::Close | sf::Style::Titlebar),
mView(Vector2f(0, 0), Vector2f(resolution)),
//mFps("test"),
mTileManager(mWorld),
mPlayer(mWorld, mCollection, Vector2f(200.0f, 100.0f)),
mTileManager(mWorld),
mPathfinder(mWorld),
mPlayer(mWorld, mCollection, Vector2f(200.0f, 100.0f), mPathfinder),
mElapsed(0),
mQuit(false),
mPaused(false) {
@ -39,12 +42,10 @@ Game::Game(const Vector2i& resolution) :
mWorld.SetContactListener(this);
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)));
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 "Pathfinder.h"
#include "TileManager.h"
#include "sprite/Player.h"
#include "util/Collection.h"
@ -61,6 +62,7 @@ private:
Collection mCollection;
TileManager mTileManager;
Pathfinder mPathfinder;
Player mPlayer;
/// Milliseconds since the last tick.

View file

@ -13,19 +13,20 @@
#include "../items/Weapon.h"
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.
*/
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,
CATEGORY_ACTOR, MASK_ALL, true, false, true)),
Actor(100),
mWeapon(*this, collection, world, SIZE),
mDestination(Vector2i(position)),
mWeapon(*this, collection, world, SIZE),
mDirection(0),
mDirectInput(false) {
mPathfinder(pathfinder) {
}
/**
@ -54,10 +55,15 @@ Player::fire() {
*/
void
Player::move(const Vector2f& destination) {
mDestination = destination;
// Convert to relative destination.
setSpeed(mDestination - getPosition(), SPEED);
mDirectInput = false;
mPath = mPathfinder.getPath(*this, destination);
// Make sure we found a path.
if (mPath != std::vector<Vector2f>()) {
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;
}
setSpeed(dirVec, SPEED);
mDirectInput = true;
}
/**
@ -98,8 +103,19 @@ Player::setDirection(Direction direction, bool unset) {
void
Player::onThink(float elapsedTime) {
// Stop if we are close enough to destination.
if (!mDirectInput && (thor::length(mDestination - getPosition()) < 1.0f)) {
setSpeed(Vector2f(), 0);
if (!mPath.empty()) {
// 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.
setAngle(angle(mCrosshairPosition));
@ -111,6 +127,6 @@ Player::onThink(float elapsedTime) {
void
Player::onCollide(Physical& other, uint16 category) {
if (category != CATEGORY_PARTICLE) {
mDestination = getPosition();
mPath.clear();
}
}

View file

@ -11,12 +11,14 @@
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include "../Pathfinder.h"
#include "../abstract/Actor.h"
#include "../abstract/Sprite.h"
#include "../items/Weapon.h"
#include "../util/Vector.h"
class Actor;
class Pathfinder;
class Sprite;
class Weapon;
@ -38,7 +40,7 @@ public:
// Public functions.
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 fire();
@ -54,13 +56,16 @@ private:
private:
static const float SPEED;
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().
Vector2f mDestination; //< Absolute position of the movement destination.
Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor).
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_ */