diff --git a/source/Game.cpp b/source/Game.cpp index 0d4aac3..7967046 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -12,8 +12,6 @@ #include "sprites/Player.h" #include "util/Yaml.h" -#include "util/Log.h" - const int Game::FPS_GOAL = 60; /** @@ -29,7 +27,8 @@ Game::Game(sf::RenderWindow& window) : mWindow.setKeyRepeatEnabled(true); Generator generator; - generator.generateTiles(mTileManager, sf::IntRect(-16, -16, 32, 32)); + generator.generateTiles(mTileManager, mWorld, + sf::IntRect(-16, -16, 32, 32)); mPlayer = std::shared_ptr(new Player(mWorld, mTileManager, sf::Vector2f(0.0f, 0.0f), Yaml("player.yaml"))); mWorld.insertCharacter(mPlayer); @@ -161,8 +160,8 @@ Game::mouseDown(const sf::Event& event) { mPlayer->pullTrigger(); break; case sf::Mouse::Right: - mPlayer->setDestination(convertCoordinates(event.mouseMove.x, - event.mouseMove.y)); + mPlayer->setDestination(convertCoordinates(event.mouseButton.x, + event.mouseButton.y)); default: break; } diff --git a/source/World.cpp b/source/World.cpp index 50c35da..c9db57c 100755 --- a/source/World.cpp +++ b/source/World.cpp @@ -7,12 +7,14 @@ #include "World.h" +#include #include #include #include #include "util/Interval.h" +#include "sprites/TileManager.h" const float World::WALL_DISTANCE_MULTIPLIER = 1.5f; /** @@ -69,66 +71,19 @@ World::removeCharacter(std::shared_ptr character) { remove(character); } -/** - * Generate path finding base data. - * - * Hardcoded as heuristic may be unnecessary with proper map generation. - * - * @warning Must not be run while getPath() is running (raw pointers). - */ -void -World::generateAreas() { - Area a; - - a.area = sf::FloatRect(50, 50, 900, 300); - a.center = sf::Vector2f(500, 200); - mAreas.push_back(a); - a.area = sf::FloatRect(450, 350, 450, 100); - a.center = sf::Vector2f(675, 400); - mAreas.push_back(a); - a.area = sf::FloatRect(50, 450, 900, 500); - a.center = sf::Vector2f(500, 700); - mAreas.push_back(a); - - Portal p1; - Portal p2; - std::vector vp; - - p1.start = sf::Vector2f(450, 350); - p1.end = sf::Vector2f(950, 350); - p1.area = &mAreas[1]; - vp.push_back(p1); - mAreas[0].portals = vp; - - vp.clear(); - p2.start = sf::Vector2f(450, 450); - p2.end = sf::Vector2f(950, 450); - p2.area = &mAreas[1]; - vp.push_back(p2); - mAreas[2].portals = vp; - - vp.clear(); - p1.area = &mAreas[0]; - vp.push_back(p1); - p2.area = &mAreas[2]; - vp.push_back(p2); - mAreas[1].portals = vp; -} - /** * Runs the A* path finding algorithm with areas as nodes and portals as edges. * * @warning Areas and portals must not be changed while this is running. * * @param start The area to start the path finding from. Must not be null. - * @param end The goal to reach. May be null. + * @param end The goal to reach. Must not be null. * @return Path in reverse order (start being the last item and end the first). */ std::vector World::astarArea(Area* start, Area* end) const { assert(start); - if (!end) - return std::vector(); + assert(end); std::unordered_set closed; std::unordered_map openAreasEstimatedCost; @@ -190,30 +145,32 @@ World::astarArea(Area* start, Area* end) const { std::vector World::getPath(const sf::Vector2f& start, const sf::Vector2f& end, float radius) const { + if (!getArea(end)) + return std::vector(); std::vector portals = astarArea(getArea(start), getArea(end)); std::vector path; path.push_back(end); for (auto p : portals) { // Find the point on the line of the portal closest to the previous point. - sf::Vector2f startToEnd = p->end - p->start; - float percentage = thor::dotProduct(startToEnd, path.back() - p->start) / + sf::Vector2f startToEnd = sf::Vector2f(p->end - p->start); + float percentage = thor::dotProduct(startToEnd, path.back() - sf::Vector2f(p->start)) / thor::squaredLength(startToEnd); sf::Vector2f point; if (percentage < 0 || percentage > 1.0f) { - if (thor::squaredLength(p->start - path.back()) < - thor::squaredLength(p->end - path.back())) { + if (thor::squaredLength(sf::Vector2f(p->start) - path.back()) < + thor::squaredLength(sf::Vector2f(p->end) - path.back())) { thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius); - point = p->start + startToEnd; + point = sf::Vector2f(p->start) + startToEnd; } else { thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius); - point = p->end - startToEnd; + point = sf::Vector2f(p->end) - startToEnd; } } else - point = p->start + startToEnd * percentage; + point = sf::Vector2f(p->start) + startToEnd * percentage; // Take two points on a line orthogonal to the portal. thor::setLength(startToEnd, radius); @@ -243,12 +200,22 @@ std::vector > } return visible; } + +/** + * Initializes start and end of an area, sets area to null. + */ +World::Portal::Portal(const sf::Vector2i& start, const sf::Vector2i& end) : + start(start), + end(end), + area(nullptr) { +} + /** * Returns the linear distance between two areas (using their center). */ float World::heuristic_cost_estimate(Area* start, Area* end) const { - return thor::length(end->center - start->center); + return thor::length(sf::Vector2f(end->center - start->center)); } /** @@ -296,7 +263,7 @@ World::step(int elapsed) { /** * Calls Character::onThink for each character. Must be called - * before step (due to character removal). + * before step (so Characters get removed asap). * * @param elapsed Time since last call. */ @@ -312,6 +279,90 @@ World::think(int elapsed) { } } +/** + * Inserts an area used for path finding. + * + * @parm rect Rectangle the area covers. + */ +void +World::insertArea(const sf::IntRect& rect) { + Area a; + // Not sure why the offset of -50 is required, but with it, areas align + // with tiles perfectly. + a.area = sf::IntRect(rect.left * TileManager::TILE_SIZE.x - 50, + rect.top * TileManager::TILE_SIZE.y - 50, + rect.width * TileManager::TILE_SIZE.x, + rect.height * TileManager::TILE_SIZE.y); + a.center = sf::Vector2i(a.area.left + a.area.width / 2, + a.area.top + a.area.height / 2); + mAreas.push_back(a); +} + +/** + * Generates portals that connect areas. Needs to be run after insertArea for + * path finding to work. + * + * Could be improved by only checking nearby areas. + */ +void +World::generatePortals() { + for (Area& it : mAreas) { + // We currently recreate portals for all existing areas, so we have + // to clear in case this was already generated. + it.portals.clear(); + for (Area& other : mAreas) { + if (&it == &other) + continue; + Portal portal; + portal.area = &other; + if (it.area.left + it.area.width == other.area.left) { + Interval overlap = Interval::IntervalFromPoints(it.area.top, + it.area.top + it.area.height) + .getOverlap(Interval::IntervalFromPoints(other.area.top, + other.area.top + other.area.height)); + if (overlap.getLength() > 0) { + portal.start = sf::Vector2i(other.area.left, overlap.start); + portal.end = sf::Vector2i(other.area.left, overlap.end); + it.portals.push_back(portal); + } + } + if (other.area.left + other.area.width == it.area.left) { + Interval overlap = Interval::IntervalFromPoints(it.area.top, + it.area.top + it.area.height) + .getOverlap(Interval::IntervalFromPoints(other.area.top, + other.area.top + other.area.height)); + if (overlap.getLength() > 0) { + portal.start = sf::Vector2i(it.area.left, overlap.start); + portal.end = sf::Vector2i(it.area.left, overlap.end); + it.portals.push_back(portal); + } + } + else if (it.area.top + it.area.height == other.area.top) { + Interval overlap = Interval::IntervalFromPoints(it.area.left, + it.area.left + it.area.width) + .getOverlap(Interval::IntervalFromPoints(other.area.left, + other.area.left + other.area.width)); + if (overlap.getLength() > 0) { + portal.start = sf::Vector2i(overlap.start, other.area.top); + portal.end = sf::Vector2i(overlap.end, other.area.top); + it.portals.push_back(portal); + } + } + else if (other.area.top + other.area.height == it.area.top) { + Interval overlap = Interval::IntervalFromPoints(it.area.left, + it.area.left + it.area.width) + .getOverlap(Interval::IntervalFromPoints(other.area.left, + other.area.left + other.area.width)); + if (overlap.getLength() > 0) { + portal.start = sf::Vector2i(overlap.start, it.area.top); + portal.end = sf::Vector2i(overlap.end, it.area.top); + it.portals.push_back(portal); + } + } + } + } +} + /** * Tests for collisions using Seperating Axis Theorem (SAT). * @@ -426,7 +477,7 @@ World::testCollision(std::shared_ptr spriteA, World::Area* World::getArea(const sf::Vector2f& point) const { for (auto area = mAreas.begin(); area != mAreas.end(); area++) { - if (area->area.contains(point)) + if (sf::FloatRect(area->area).contains(point)) // Make the return value non-const for convenience. return &const_cast(*area); } diff --git a/source/World.h b/source/World.h index 3e3b321..860979f 100755 --- a/source/World.h +++ b/source/World.h @@ -28,7 +28,8 @@ public: void removeCharacter(std::shared_ptr character); void step(int elapsed); void think(int elapsed); - void generateAreas(); + void insertArea(const sf::IntRect& rect); + void generatePortals(); std::vector getPath(const sf::Vector2f& start, const sf::Vector2f& end, float radius) const; std::vector > @@ -42,8 +43,13 @@ private: * Redundant data as portals are saved twice. */ struct Portal { - sf::Vector2f start; - sf::Vector2f end; + 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; Area* area; }; @@ -51,8 +57,8 @@ private: * Nodes */ struct Area { - sf::FloatRect area; - sf::Vector2f center; + sf::IntRect area; + sf::Vector2i center; std::vector portals; }; diff --git a/source/generator/Generator.cpp b/source/generator/Generator.cpp index 8b48390..03e82e3 100644 --- a/source/generator/Generator.cpp +++ b/source/generator/Generator.cpp @@ -13,6 +13,7 @@ #include "simplexnoise.h" #include "../sprites/TileManager.h" #include "../util/Log.h" +#include "../World.h" /// For usage with simplexnoise.h uint8_t perm[512]; @@ -39,7 +40,8 @@ Generator::Generator() { * power of two. */ void -Generator::generateTiles(TileManager& tm, const sf::IntRect& area) const { +Generator::generateTiles(TileManager& tm, World& world, + const sf::IntRect& area) const { // Check if width and height are power of two. assert(area.width && !(area.width & (area.width - 1))); assert(area.height && !(area.height & (area.height - 1))); @@ -66,10 +68,14 @@ Generator::generateTiles(TileManager& tm, const sf::IntRect& area) const { for (int x = area.left; x < area.left + area.width; x++) { for (int y = area.top; y < area.top + area.height; y++) { (filtered[x-area.left][y-area.top]) - ? tm.insertTile(TileManager::TilePosition(x, y), TileManager::Type::WALL) - : tm.insertTile(TileManager::TilePosition(x, y), TileManager::Type::FLOOR); + ? tm.insertTile(TileManager::TilePosition(x, y), + TileManager::Type::WALL) + : tm.insertTile(TileManager::TilePosition(x, y), + TileManager::Type::FLOOR); } } + generateAreas(world, filtered, area, sf::Vector2f(area.left, area.top)); + world.generatePortals(); } /** @@ -138,3 +144,38 @@ Generator::filterWalls(std::vector >& in, shortside * longside - subtract) fill(out, sf::IntRect(x, y, shortside, longside), true); } + +/** + * Inserts tile if all values within area are the same, otherwise divides area + * into four and continues recursively. + * + * @param tm World to insert areas into. + * @param tiles Array of tile values (walls). + * @param area The area to generate areas for. + * @param offset Offset of tiles[0][0] from World coordinate (0, 0). + */ +void +Generator::generateAreas(World& world, std::vector >& tiles, + const sf::IntRect& area, const sf::Vector2f& offset) { + assert(area.width > 0 && area.height > 0); + int count = countWalls(sf::IntRect(area.left - offset.y, area.top - offset.x, + area.width, area.height), tiles); + if (count == 0) { + world.insertArea(sf::IntRect(area)); + } + else if (count == area.width * area.height) { + return; + } + else { + int halfWidth = area.width / 2.0f; + int halfHeight = area.height / 2.0f; + generateAreas(world, tiles, sf::IntRect(area.left, + area.top, halfWidth, halfHeight), offset); + generateAreas(world, tiles, sf::IntRect(area.left + halfWidth, + area.top, halfWidth, halfHeight), offset); + generateAreas(world, tiles, sf::IntRect(area.left, + area.top + halfHeight, halfWidth, halfHeight), offset); + generateAreas(world, tiles, sf::IntRect(area.left + halfWidth, + area.top + halfHeight, halfWidth, halfHeight), offset); + } +} diff --git a/source/generator/Generator.h b/source/generator/Generator.h index 495dc76..e5e347d 100644 --- a/source/generator/Generator.h +++ b/source/generator/Generator.h @@ -11,11 +11,13 @@ #include class TileManager; +class World; class Generator { public: explicit Generator(); - void generateTiles(TileManager& tm, const sf::IntRect& area) const; + void generateTiles(TileManager& tm, World& world, + const sf::IntRect& area) const; //void generateCharacters(World& world, const sf::IntRect& area) const; sf::Vector2f getPlayerSpawn() const; @@ -27,6 +29,9 @@ private: int x, int y, int longside, int shortside, int subtract); static int countWalls(const sf::IntRect& area, std::vector >& tiles); + static void generateAreas(World& world, + std::vector >& tiles, + const sf::IntRect& area, const sf::Vector2f& offset); }; #endif /* DG_GENERATOR_H_ */ diff --git a/source/util/Interval.h b/source/util/Interval.h index 22add5b..193713d 100644 --- a/source/util/Interval.h +++ b/source/util/Interval.h @@ -16,12 +16,12 @@ public: bool isInside(float point) const; float getLength(); -private: - Interval(float start, float end); - -private: +public: float start; float end; + +private: + Interval(float start, float end); }; #endif /* DG_INTERVAL_H_ */