Removed collision checks for non-moving sprites.

This commit is contained in:
Felix Ableitner 2013-04-12 20:31:43 +02:00
parent 525e33fbc2
commit 83b523ca70
4 changed files with 17 additions and 5 deletions

View file

@ -27,7 +27,7 @@ Game::Game(sf::RenderWindow& window) :
mWindow.setKeyRepeatEnabled(true); mWindow.setKeyRepeatEnabled(true);
Generator generator; Generator generator;
generator.generateTiles(mTileManager, sf::IntRect(-10, -10, 20, 20)); generator.generateTiles(mTileManager, sf::IntRect(-20, -20, 40, 40));
mPlayer = std::shared_ptr<Player>(new Player(mWorld, mTileManager, mPlayer = std::shared_ptr<Player>(new Player(mWorld, mTileManager,
sf::Vector2f(0.0f, 0.0f), Yaml("player.yaml"))); sf::Vector2f(0.0f, 0.0f), Yaml("player.yaml")));
mWorld.insertCharacter(mPlayer); mWorld.insertCharacter(mPlayer);

View file

@ -262,11 +262,14 @@ World::heuristic_cost_estimate(Area* start, Area* end) const {
void void
World::step(int elapsed) { World::step(int elapsed) {
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
for (auto it = v->second.begin(); it != v->second.end(); ) { for (auto it = v->second.begin(); it != v->second.end(); it++) {
auto spriteA = *it; auto spriteA = *it;
if (spriteA->getDelete()) if (spriteA->getDelete()) {
remove(spriteA); remove(spriteA);
else { it--;
}
// Apply movement for movable sprites.
else if ((*it)->isMovable()) {
sf::Vector2f speed = spriteA->getSpeed(); sf::Vector2f speed = spriteA->getSpeed();
speed *= elapsed / 1000.0f; speed *= elapsed / 1000.0f;
bool overlap = false; bool overlap = false;
@ -286,7 +289,6 @@ World::step(int elapsed) {
} }
if (!overlap) if (!overlap)
spriteA->setPosition(spriteA->getPosition() + speed); spriteA->setPosition(spriteA->getPosition() + speed);
it++;
} }
} }
} }

View file

@ -153,6 +153,15 @@ Sprite::collisionEnabled(Category category) const {
return (category & mMask) != 0; return (category & mMask) != 0;
} }
/**
* Returns true if this sprite is able to move, currently decided by shape
* (circle can move, rectangle can not).
*/
bool
Sprite::isMovable() const {
return mShape.type == Shape::Type::CIRCLE;
}
/** /**
* Called when a collision with another Sprite occured. Override this method * Called when a collision with another Sprite occured. Override this method
* to manage collision events. * to manage collision events.

View file

@ -67,6 +67,7 @@ public:
sf::Vector2f getSize() const; sf::Vector2f getSize() const;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
bool collisionEnabled(Category category) const; bool collisionEnabled(Category category) const;
bool isMovable() const;
virtual void onCollide(std::shared_ptr<Sprite> other); virtual void onCollide(std::shared_ptr<Sprite> other);