diff --git a/source/Game.cpp b/source/Game.cpp index 92aef4a..4546dd2 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -7,10 +7,14 @@ #include "Game.h" +#include + #include "generator/Generator.h" #include "sprites/Enemy.h" #include "sprites/Player.h" -#include "util/Yaml.h" +#include "util/Log.h" + +#include "sprites/Corpse.h" const int Game::FPS_GOAL = 60; @@ -26,10 +30,18 @@ Game::Game(sf::RenderWindow& window) : mWindow.setFramerateLimit(FPS_GOAL); mWindow.setKeyRepeatEnabled(true); - mGenerator.generateTiles(sf::IntRect(-32, -32, 64, 64)); + sf::IntRect area(-32, -32, 64, 64); + mGenerator.generateTiles(area); mPlayer = std::shared_ptr(new Player(mWorld, mPathfinder, - mGenerator.getPlayerSpawn())); - mWorld.insertCharacter(mPlayer); + mGenerator.getPlayerSpawn())); + mWorld.insertCharacter(mPlayer); + + auto enemyPositions = mGenerator.getEnemySpawns(area); + for (const auto& position : enemyPositions) { + LOG_D(position); + if (thor::length(mPlayer->getPosition() - position) > Character::VISION_DISTANCE) + mWorld.insertCharacter(std::shared_ptr(new Enemy(mWorld, mPathfinder, position))); + } } /** diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 02078fc..89ae6cd 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -27,6 +27,10 @@ public: void onDamage(int damage); +public: + /// Maximum distance where an enemy will be detected. + static const float VISION_DISTANCE; + protected: virtual void onThink(int elapsed); virtual void onDeath(); @@ -42,9 +46,6 @@ private: void move(); private: - /// Maximum distance where an enemy will be detected. - static const float VISION_DISTANCE; - friend class World; World& mWorld; Pathfinder& mPathfinder; diff --git a/source/generator/Generator.cpp b/source/generator/Generator.cpp index d39e27f..6924e34 100644 --- a/source/generator/Generator.cpp +++ b/source/generator/Generator.cpp @@ -28,6 +28,10 @@ uint8_t perm[512]; */ const int Generator::MARGIN = 10; +// Different layers in 3d noise so we don't use the same noise values. +const float Generator::LAYER_TILES = 0; +const float Generator::LAYER_ENEMIES = 1.0f; + /** * Generates new random seed. */ @@ -58,8 +62,8 @@ Generator::generateTiles(const sf::IntRect& area) { for (int x = area.left - MARGIN; x < area.left + area.width + MARGIN; x++) { for (int y = area.top - MARGIN; y < area.top + area.height + MARGIN; y++) { noise[x][y] = - (scaled_octave_noise_2d(2, 2, 0.05f, 0.5f, -0.5f, x, y) + - scaled_octave_noise_2d(2, 2, 0.5f, 0.15f, -0.15f, x, y) + (scaled_octave_noise_3d(2, 2, 0.05f, 0.5f, -0.5f, x, y, LAYER_TILES) + + scaled_octave_noise_3d(2, 2, 0.5f, 0.15f, -0.15f, x, y, LAYER_TILES) < -0.1f) ? type::WALL : type::FLOOR; @@ -86,6 +90,24 @@ Generator::generateTiles(const sf::IntRect& area) { mGenerated = filtered; } +std::vector +Generator::getEnemySpawns(const sf::IntRect& area) const { + auto compare = [](const sf::Vector2f& a, const sf::Vector2f& b) { + return a.x < b.x || (a.x == b.x && a.y < b.y); + }; + std::set ret(compare); + for (int x = area.left; x < area.left + area.width; x++) { + for (int y = area.top; y < area.top + area.height; y++) { + if (scaled_octave_noise_3d(2, 2, 0.5f, 10.0f, 0, x, y, LAYER_ENEMIES) + < 1.0f) { + ret.insert(sf::Vector2f(thor::componentwiseProduct( + findClosestFloor(sf::Vector2i(x, y)), Tile::TILE_SIZE))); + } + } + } + return std::vector(ret.begin(), ret.end()); +} + /** * Fills a rectangular area with the specified value. * @@ -209,7 +231,9 @@ Generator::findClosestFloor(const sf::Vector2i& position) const { const sf::Vector2i& current = open.begin()->first; open.erase(current); closed.insert(current); - if (mGenerated.at(current.x).at(current.y) == Tile::Type::FLOOR) + if (mGenerated.count(current.x) != 0 && + mGenerated.at(current.x).count(current.y) != 0 && + mGenerated.at(current.x).at(current.y) == Tile::Type::FLOOR) return current; else { if (closed.find(sf::Vector2i(current.x + 1, current.y)) == closed.end()) diff --git a/source/generator/Generator.h b/source/generator/Generator.h index bacd7f4..bba90e3 100644 --- a/source/generator/Generator.h +++ b/source/generator/Generator.h @@ -20,6 +20,7 @@ public: explicit Generator(World& world, Pathfinder& pathfinder); void generateTiles(const sf::IntRect& area); sf::Vector2f getPlayerSpawn() const; + std::vector getEnemySpawns(const sf::IntRect& area) const; private: typedef Tile::Type type; @@ -36,6 +37,9 @@ private: private: static const int MARGIN; + static const float LAYER_TILES; + static const float LAYER_ENEMIES; + World& mWorld; Pathfinder& mPathfinder; array mGenerated;