Changed Generator to use enum instead of bool.

This commit is contained in:
Felix Ableitner 2013-04-28 23:18:17 +02:00
parent 5be4a65653
commit 39cfb89f5e
3 changed files with 34 additions and 33 deletions

View File

@ -33,8 +33,6 @@ Generator::Generator() {
/**
* Fill TileManager with procedurally generated tiles.
*
* True means wall, false means floor.
*
* @param tm TileManager instance to set tiles in.
* @param area Size and position of area to generate tiles for. Must be
* power of two.
@ -46,16 +44,20 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder,
assert(area.width && !(area.width & (area.width - 1)));
assert(area.height && !(area.height & (area.height - 1)));
std::vector<std::vector<bool> >
noise(area.width, std::vector<bool>(area.height));
std::vector<std::vector<bool> >
filtered(area.width, std::vector<bool>(area.height, false));
std::vector<std::vector<TileManager::Type> >
noise(area.width, std::vector<TileManager::Type>(area.height));
std::vector<std::vector<TileManager::Type> >
filtered(area.width, std::vector<TileManager::Type>(
area.height, TileManager::Type::FLOOR));
for (int x = area.left; x < area.left + area.width; x++) {
for (int y = area.top; y < area.top + area.height; y++) {
noise[x-area.left][y-area.top] =
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)
< -0.1f;
(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)
< -0.1f)
? TileManager::Type::WALL
: TileManager::Type::FLOOR;
}
}
for (int x = 0; x < (int) noise.size(); x++) {
@ -67,11 +69,8 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder,
}
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),
filtered[x-area.left][y-area.top]);
}
}
generateAreas(pathfinder, filtered, area,
@ -87,8 +86,8 @@ Generator::generateTiles(TileManager& tm, Pathfinder& pathfinder,
* @param value The value to set.
*/
void
Generator::fill(std::vector<std::vector<bool> >& image,
const sf::IntRect& area, bool value) {
Generator::fill(std::vector<std::vector<TileManager::Type> >& image,
const sf::IntRect& area, TileManager::Type value) {
for (int x = area.left;
x < area.left + area.width && x < (int) image.size(); x++) {
for (int y = area.top;
@ -99,17 +98,17 @@ Generator::fill(std::vector<std::vector<bool> >& image,
}
/**
* Returns the number of walls (fields with value true) in the area in tiles.
* Returns the number of walls in the area in tiles.
*
* @param area The area to count in.
* @param tiles Array of tile values (walls).
* @param tiles Array of tile values.
*/
int Generator::countWalls(const sf::IntRect& area,
std::vector<std::vector<bool> >& tiles) {
std::vector<std::vector<TileManager::Type> >& tiles) {
int count = 0;
for (int x = area.left; x < area.left + area.width; x++) {
for (int y = area.top; y < area.top + area.height; y++)
count += (int) tiles[x][y];
count += (int) (tiles[x][y] == TileManager::Type::WALL);
}
return count;
}
@ -128,8 +127,8 @@ int Generator::countWalls(const sf::IntRect& area,
* tiles is not walls (tilecount >= longside * shortside - subtract).
*/
void
Generator::filterWalls(std::vector<std::vector<bool> >& in,
std::vector<std::vector<bool> >& out,
Generator::filterWalls(std::vector<std::vector<TileManager::Type> >& in,
std::vector<std::vector<TileManager::Type> >& out,
int x, int y, int longside, int shortside, int subtract) {
// Skip if we would go out of range.
if ((x + longside >= (int) in.size()) ||
@ -139,11 +138,11 @@ Generator::filterWalls(std::vector<std::vector<bool> >& in,
// Filter in horizontal direction.
if (countWalls(sf::IntRect(x, y, longside, shortside), in) >=
shortside * longside - subtract)
fill(out, sf::IntRect(x, y, longside, shortside), true);
fill(out, sf::IntRect(x, y, longside, shortside), TileManager::Type::WALL);
// Filter in vertical direction.
if (countWalls(sf::IntRect(x, y, shortside, longside), in) >=
shortside * longside - subtract)
fill(out, sf::IntRect(x, y, shortside, longside), true);
fill(out, sf::IntRect(x, y, shortside, longside), TileManager::Type::WALL);
}
/**
@ -151,12 +150,13 @@ Generator::filterWalls(std::vector<std::vector<bool> >& in,
* into four and continues recursively.
*
* @param tm World to insert areas into.
* @param tiles Array of tile values (walls).
* @param tiles Array of tile values.
* @param area The area to generate areas for.
* @param offset Offset of tiles[0][0] from World coordinate (0, 0).
*/
void
Generator::generateAreas(Pathfinder& pathfinder, std::vector<std::vector<bool> >& tiles,
Generator::generateAreas(Pathfinder& pathfinder,
std::vector<std::vector<TileManager::Type> >& 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,

View File

@ -10,8 +10,9 @@
#include <SFML/Graphics.hpp>
#include "../sprites/TileManager.h"
class Pathfinder;
class TileManager;
class Generator {
public:
@ -22,15 +23,15 @@ public:
sf::Vector2f getPlayerSpawn() const;
private:
static void fill(std::vector<std::vector<bool> >& image,
const sf::IntRect& area, bool value);
static void filterWalls(std::vector<std::vector<bool> >& in,
std::vector<std::vector<bool> >& out,
static void fill(std::vector<std::vector<TileManager::Type> >& image,
const sf::IntRect& area, TileManager::Type value);
static void filterWalls(std::vector<std::vector<TileManager::Type> >& in,
std::vector<std::vector<TileManager::Type> >& out,
int x, int y, int longside, int shortside, int subtract);
static int countWalls(const sf::IntRect& area,
std::vector<std::vector<bool> >& tiles);
std::vector<std::vector<TileManager::Type> >& tiles);
static void generateAreas(Pathfinder& pathfinder,
std::vector<std::vector<bool> >& tiles,
std::vector<std::vector<TileManager::Type> >& tiles,
const sf::IntRect& area, const sf::Vector2f& offset);
};

View File

@ -14,7 +14,7 @@ class World;
class TileManager {
public:
enum class Type {
enum class Type : char {
FLOOR,
WALL
};