Added Generator::getEnemySpawns.

This commit is contained in:
Felix Ableitner 2013-05-08 11:19:21 +02:00
parent aa6df65e97
commit 73bb918a01
4 changed files with 51 additions and 10 deletions

View file

@ -7,10 +7,14 @@
#include "Game.h"
#include <Thor/Vectors.hpp>
#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<Player>(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<Enemy>(new Enemy(mWorld, mPathfinder, position)));
}
}
/**

View file

@ -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;

View file

@ -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<sf::Vector2f>
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<sf::Vector2f, decltype(compare)> 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<sf::Vector2f>(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())

View file

@ -20,6 +20,7 @@ public:
explicit Generator(World& world, Pathfinder& pathfinder);
void generateTiles(const sf::IntRect& area);
sf::Vector2f getPlayerSpawn() const;
std::vector<sf::Vector2f> 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;