2012-12-22 13:56:17 +00:00
|
|
|
/*
|
|
|
|
* World.h
|
|
|
|
*
|
|
|
|
* Created on: 29.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DG_WORLD_H_
|
|
|
|
#define DG_WORLD_H_
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
#include "abstract/Character.h"
|
2013-03-29 17:34:51 +00:00
|
|
|
#include "abstract/Sprite.h"
|
2012-12-22 13:56:17 +00:00
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
class Character;
|
2012-12-22 13:56:17 +00:00
|
|
|
class Sprite;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of sprites, which can be put into different layers.
|
|
|
|
*
|
|
|
|
* Uses Sprite instead of sf::Drawable to also manage deleting objects.
|
|
|
|
* Render order is determined by Physical::Category (higher number on top).
|
|
|
|
*/
|
|
|
|
class World : public sf::Drawable {
|
|
|
|
public:
|
|
|
|
void insert(std::shared_ptr<Sprite> drawable);
|
|
|
|
void remove(std::shared_ptr<Sprite> drawable);
|
2013-03-29 16:59:35 +00:00
|
|
|
void insertCharacter(std::shared_ptr<Character> character);
|
|
|
|
void removeCharacter(std::shared_ptr<Character> character);
|
2012-12-22 13:56:17 +00:00
|
|
|
void step(int elapsed);
|
2013-03-29 16:59:35 +00:00
|
|
|
void think(int elapsed);
|
2013-04-27 12:04:23 +00:00
|
|
|
void insertArea(const sf::IntRect& rect);
|
|
|
|
void generatePortals();
|
2013-03-27 13:38:34 +00:00
|
|
|
std::vector<sf::Vector2f> getPath(const sf::Vector2f& start,
|
2013-03-27 15:09:01 +00:00
|
|
|
const sf::Vector2f& end, float radius) const;
|
2013-03-29 17:48:49 +00:00
|
|
|
std::vector<std::shared_ptr<Character> >
|
|
|
|
getCharacters(const sf::Vector2f& position, float maxDistance) const;
|
2012-12-22 13:56:17 +00:00
|
|
|
|
2013-03-27 13:38:34 +00:00
|
|
|
private:
|
|
|
|
struct Area;
|
|
|
|
/**
|
|
|
|
* Edges
|
|
|
|
*
|
|
|
|
* Redundant data as portals are saved twice.
|
|
|
|
*/
|
|
|
|
struct Portal {
|
2013-04-27 12:04:23 +00:00
|
|
|
Portal() = default;
|
|
|
|
Portal(const sf::Vector2i& start, const sf::Vector2i& end);
|
|
|
|
bool operator==(const Portal& p) {
|
|
|
|
return start == p.start && end == p.end && area == p.area;
|
|
|
|
}
|
|
|
|
sf::Vector2i start;
|
|
|
|
sf::Vector2i end;
|
2013-03-27 13:38:34 +00:00
|
|
|
Area* area;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nodes
|
|
|
|
*/
|
|
|
|
struct Area {
|
2013-04-27 12:04:23 +00:00
|
|
|
sf::IntRect area;
|
|
|
|
sf::Vector2i center;
|
2013-03-27 13:38:34 +00:00
|
|
|
std::vector<Portal> portals;
|
|
|
|
};
|
|
|
|
|
2013-03-03 20:55:15 +00:00
|
|
|
private:
|
2012-12-22 13:56:17 +00:00
|
|
|
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
2013-03-04 23:36:22 +00:00
|
|
|
bool testCollision(std::shared_ptr<Sprite> spriteA, std::shared_ptr<Sprite> spriteB,
|
|
|
|
int elapsed) const;
|
2013-03-27 13:38:34 +00:00
|
|
|
Area* getArea(const sf::Vector2f& point) const;
|
|
|
|
float heuristic_cost_estimate(Area* start, Area* end) const;
|
|
|
|
std::vector<Portal*> astarArea(Area* start, Area* end) const;
|
2012-12-22 13:56:17 +00:00
|
|
|
|
|
|
|
private:
|
2013-04-05 14:58:37 +00:00
|
|
|
static const float WALL_DISTANCE_MULTIPLIER;
|
2012-12-23 14:50:49 +00:00
|
|
|
std::map<Sprite::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
|
2013-03-27 13:38:34 +00:00
|
|
|
std::vector<Area> mAreas; //< This has to be a vector as objects are compared by address.
|
2013-03-29 16:59:35 +00:00
|
|
|
std::vector<std::shared_ptr<Character> > mCharacters;
|
2012-12-22 13:56:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* DG_WORLD_H_ */
|