2012-12-22 13:56:17 +00:00
|
|
|
/*
|
|
|
|
* World.cpp
|
|
|
|
*
|
|
|
|
* Created on: 29.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "World.h"
|
|
|
|
|
2012-12-22 15:48:48 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2013-04-29 14:49:16 +00:00
|
|
|
#include "sprites/Tile.h"
|
2013-04-28 16:11:39 +00:00
|
|
|
#include "util/Interval.h"
|
2013-05-01 17:07:47 +00:00
|
|
|
#include "util/Log.h"
|
2013-04-04 20:44:15 +00:00
|
|
|
|
2012-12-22 13:56:17 +00:00
|
|
|
/**
|
|
|
|
* Insert a drawable into the group. Drawables should only be handled with shared_ptr.
|
|
|
|
* An object can't be inserted more than once at the same level.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::insert(std::shared_ptr<Sprite> drawable) {
|
2013-03-03 20:55:15 +00:00
|
|
|
#ifndef NDEBUG
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite::Category cat = drawable->getCategory();
|
2012-12-22 13:56:17 +00:00
|
|
|
auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable);
|
2013-03-03 20:55:15 +00:00
|
|
|
assert(item == mDrawables[cat].end());
|
|
|
|
#endif
|
2013-07-14 19:56:44 +00:00
|
|
|
mDrawables[drawable->getCategory()].push_back(drawable);
|
2012-12-22 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
/**
|
|
|
|
* Inserts a character into the world. A character can only be inserted once.
|
|
|
|
* Also calls insert(character);
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::insertCharacter(std::shared_ptr<Character> character) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
|
|
|
|
assert(item == mCharacters.end());
|
|
|
|
#endif
|
2013-07-14 19:56:44 +00:00
|
|
|
mCharacters.push_back(character);
|
|
|
|
insert(character);
|
2013-03-29 16:59:35 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 08:52:50 +00:00
|
|
|
void
|
|
|
|
World::remove(std::shared_ptr<Sprite> drawable) {
|
|
|
|
Sprite::Category cat = drawable->getCategory();
|
|
|
|
auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable);
|
|
|
|
mDrawables[cat].erase(item);
|
|
|
|
}
|
|
|
|
|
2013-03-29 17:48:49 +00:00
|
|
|
/**
|
|
|
|
* Returns all characters that are within maxDistance from position.
|
|
|
|
*/
|
|
|
|
std::vector<std::shared_ptr<Character> >
|
2013-08-07 15:39:43 +00:00
|
|
|
World::getCharacters(const Vector2f& position, float maxDistance) const {
|
2013-03-29 17:48:49 +00:00
|
|
|
std::vector<std::shared_ptr<Character> > visible;
|
2013-07-21 08:52:50 +00:00
|
|
|
for (const auto& it : mCharacters) {
|
2013-04-05 14:23:09 +00:00
|
|
|
if (position == it->getPosition())
|
|
|
|
continue;
|
2013-03-29 17:48:49 +00:00
|
|
|
if (thor::squaredLength(position - it->getPosition()) <=
|
2013-04-04 21:13:08 +00:00
|
|
|
maxDistance * maxDistance)
|
2013-03-29 17:48:49 +00:00
|
|
|
visible.push_back(it);
|
|
|
|
}
|
|
|
|
return visible;
|
|
|
|
}
|
2013-04-27 12:04:23 +00:00
|
|
|
|
2013-03-03 20:55:15 +00:00
|
|
|
/**
|
|
|
|
* Checks for collisions and applies movement, also removes sprites if
|
|
|
|
* Sprite::getDelete returns true.
|
|
|
|
*
|
|
|
|
* This method can be improved by only testing each pair of sprites once,
|
|
|
|
* and using the result for both. Applying movement should be done in
|
|
|
|
* testCollision, always applying the part that causes no collision.
|
|
|
|
*/
|
2012-12-22 13:56:17 +00:00
|
|
|
void
|
|
|
|
World::step(int elapsed) {
|
2012-12-22 15:48:48 +00:00
|
|
|
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
2013-04-12 18:31:43 +00:00
|
|
|
for (auto it = v->second.begin(); it != v->second.end(); it++) {
|
2013-05-26 18:16:36 +00:00
|
|
|
if ((*it)->getDelete()) {
|
2013-04-28 12:08:16 +00:00
|
|
|
v->second.erase(it);
|
2013-04-12 18:31:43 +00:00
|
|
|
it--;
|
|
|
|
}
|
2013-05-26 18:16:36 +00:00
|
|
|
// Don't run collision tests if sprite is not moving.
|
2013-08-07 15:39:43 +00:00
|
|
|
else if ((*it)->getSpeed() != Vector2f())
|
2013-05-26 18:16:36 +00:00
|
|
|
applyMovement(*it, elapsed);
|
2013-05-01 17:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests spriteA for overlap with every other sprite (considering collision
|
|
|
|
* masks).
|
|
|
|
*/
|
2013-05-26 18:16:36 +00:00
|
|
|
void
|
|
|
|
World::applyMovement(std::shared_ptr<Sprite> sprite, int elapsed) {
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f offset = sprite->getSpeed() * (elapsed / 1000.0f);
|
2013-05-01 17:07:47 +00:00
|
|
|
for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) {
|
2013-07-21 08:52:50 +00:00
|
|
|
for (const auto& other : w->second) {
|
2013-05-26 18:16:36 +00:00
|
|
|
if (sprite == other)
|
2013-05-01 17:07:47 +00:00
|
|
|
continue;
|
|
|
|
// Ignore anything that is filtered by masks.
|
2013-05-26 18:16:36 +00:00
|
|
|
if (!sprite->collisionEnabled(other->getCategory()) ||
|
|
|
|
!other->collisionEnabled(sprite->getCategory()))
|
2013-05-01 17:07:47 +00:00
|
|
|
continue;
|
2013-05-26 18:16:36 +00:00
|
|
|
if (sprite->testCollision(other, offset,
|
|
|
|
other->getSpeed() * (elapsed / 1000.0f))) {
|
|
|
|
sprite->onCollide(other);
|
|
|
|
other->onCollide(sprite);
|
2013-03-03 20:28:33 +00:00
|
|
|
}
|
2012-12-22 15:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-26 18:16:36 +00:00
|
|
|
sprite->setPosition(sprite->getPosition() + offset);
|
2012-12-22 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
/**
|
2013-04-05 14:23:09 +00:00
|
|
|
* Calls Character::onThink for each character. Must be called
|
2013-04-28 12:08:16 +00:00
|
|
|
* before step so Characters get removed correctly.
|
2013-03-29 16:59:35 +00:00
|
|
|
*
|
|
|
|
* @param elapsed Time since last call.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::think(int elapsed) {
|
2013-04-05 14:54:51 +00:00
|
|
|
for (auto it = mCharacters.begin(); it != mCharacters.end(); ) {
|
2013-04-28 12:08:16 +00:00
|
|
|
if ((*it)->getDelete()) {
|
|
|
|
mCharacters.erase(it);
|
|
|
|
auto& d = mDrawables[Sprite::CATEGORY_ACTOR];
|
|
|
|
d.erase(std::find(d.begin(), d.end(), *it));
|
|
|
|
}
|
2013-04-05 14:54:51 +00:00
|
|
|
else {
|
|
|
|
(*it)->onThink(elapsed);
|
|
|
|
it++;
|
|
|
|
}
|
2013-03-29 16:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-22 13:56:17 +00:00
|
|
|
/**
|
|
|
|
* Draws all elements in the group.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
2013-04-27 19:29:05 +00:00
|
|
|
sf::FloatRect screen(target.getViewport(target.getView()));
|
|
|
|
screen.left += target.getView().getCenter().x - target.getView().getSize().x / 2;
|
|
|
|
screen.top += target.getView().getCenter().y - target.getView().getSize().y / 2;
|
2012-12-22 13:56:17 +00:00
|
|
|
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
2013-07-21 08:52:50 +00:00
|
|
|
for (const auto& item : v->second) {
|
2013-04-27 19:29:05 +00:00
|
|
|
if (item->isInside(screen))
|
|
|
|
target.draw(static_cast<sf::Drawable&>(*item), states);
|
2012-12-22 13:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-29 14:10:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Performs a raycast between two points to check if the path between them is
|
|
|
|
* clear of walls. Does not consider characters, bullets etc.
|
|
|
|
*
|
|
|
|
* @param lineStart First point of the line to test.
|
|
|
|
* @param lineEnd Second point of the line to test.
|
|
|
|
* @return True if the ray was not blocked.
|
|
|
|
*/
|
|
|
|
bool
|
2013-08-07 15:39:43 +00:00
|
|
|
World::raycast(const Vector2f& lineStart,
|
|
|
|
const Vector2f& lineEnd) const {
|
2013-04-29 14:10:22 +00:00
|
|
|
assert(lineStart != lineEnd);
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f lineCenter = lineStart + 0.5f * (lineEnd - lineStart);
|
2013-04-29 14:10:22 +00:00
|
|
|
for (const auto& it : mDrawables.at(Sprite::Category::CATEGORY_WORLD)) {
|
2013-07-10 21:25:53 +00:00
|
|
|
if (!it->collisionEnabled(Sprite::CATEGORY_ACTOR))
|
2013-04-29 14:10:22 +00:00
|
|
|
continue;
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f axis = it->getPosition() - lineCenter;
|
|
|
|
if (axis == Vector2f())
|
2013-04-29 14:10:22 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
axis = thor::unitVector(axis);
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f halfsize = it->getSize() / 2.0f;
|
2013-04-29 14:10:22 +00:00
|
|
|
float rectPosProjected = thor::dotProduct(axis, it->getPosition());
|
|
|
|
float lineStartProjected = thor::dotProduct(axis, lineStart);
|
|
|
|
float lineEndProjected = thor::dotProduct(axis, lineEnd);
|
|
|
|
// For corner projections, those on the same line with the rect
|
|
|
|
// center are equal by value, so we only need one on each axis
|
|
|
|
// and take the maximum.
|
|
|
|
float rectHalfWidthProjected = std::max(
|
|
|
|
abs(thor::dotProduct(axis, halfsize)),
|
|
|
|
abs(thor::dotProduct(axis,
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f(halfsize.x, -halfsize.y))));
|
2013-04-29 14:10:22 +00:00
|
|
|
Interval line = Interval::IntervalFromPoints(lineStartProjected,
|
|
|
|
lineEndProjected);
|
|
|
|
Interval rect = Interval::IntervalFromRadius(rectPosProjected,
|
|
|
|
rectHalfWidthProjected);
|
|
|
|
// Allow movement if sprites are moving apart.
|
|
|
|
if (line.getOverlap(rect).getLength() > 0.0f)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-21 08:52:50 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-18 11:13:49 +00:00
|
|
|
* Returns all sprites that are at most distance pixels away from position.
|
2013-07-21 08:52:50 +00:00
|
|
|
*/
|
2013-08-18 11:13:49 +00:00
|
|
|
std::vector<std::shared_ptr<Sprite> >
|
|
|
|
World::getNearbySprites(const Vector2f& position, float distance) const {
|
|
|
|
std::vector<std::shared_ptr<Sprite> > ret;
|
|
|
|
for (const auto& v : mDrawables)
|
|
|
|
for (const auto& d : v.second)
|
|
|
|
if (thor::squaredLength(d->getPosition() - position) <= distance * distance)
|
|
|
|
ret.push_back(d);
|
|
|
|
return ret;
|
2013-07-21 08:52:50 +00:00
|
|
|
}
|
2013-08-18 12:02:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the item closest to position, or null if it is further than
|
|
|
|
* Character::ITEM_PICKUP_MAX_DISTANCE away.
|
|
|
|
*/
|
|
|
|
std::shared_ptr<Item>
|
|
|
|
World::getClosestItem(const Vector2f& position) const {
|
|
|
|
float distance = std::numeric_limits<float>::max();
|
|
|
|
std::shared_ptr<Item> closest;
|
|
|
|
for (auto& s : getNearbySprites(position, Character::ITEM_PICKUP_MAX_DISTANCE)) {
|
|
|
|
std::shared_ptr<Item> converted = std::dynamic_pointer_cast<Item>(s);
|
|
|
|
if (converted.get() != nullptr &&
|
|
|
|
thor::squaredLength(position - converted->getPosition()) < distance * distance) {
|
|
|
|
closest = converted;
|
|
|
|
distance = thor::squaredLength(position - converted->getPosition());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (distance <= Character::ITEM_PICKUP_MAX_DISTANCE * Character::ITEM_PICKUP_MAX_DISTANCE)
|
|
|
|
? closest
|
|
|
|
: std::shared_ptr<Item>();
|
|
|
|
}
|