Fixed enemies spawning near player start by moving enemy insertion from Generator to Game.
This commit is contained in:
parent
737ab2b1ad
commit
dcf69c8e07
4 changed files with 34 additions and 16 deletions
23
src/Game.cpp
23
src/Game.cpp
|
@ -66,12 +66,30 @@ Game::initPlayer() {
|
||||||
Weapon::WeaponType::PISTOL, Weapon::WeaponType::KNIFE,
|
Weapon::WeaponType::PISTOL, Weapon::WeaponType::KNIFE,
|
||||||
Gadget::GadgetType::NONE, Gadget::GadgetType::NONE
|
Gadget::GadgetType::NONE, Gadget::GadgetType::NONE
|
||||||
};
|
};
|
||||||
mGenerator.generateCurrentAreaIfNeeded(Vector2f(), playerItems);
|
auto enemySpawns = mGenerator.generateCurrentAreaIfNeeded(Vector2f());
|
||||||
mPlayer = std::shared_ptr<Player>(new Player(mWorld, mPathfinder,
|
mPlayer = std::shared_ptr<Player>(new Player(mWorld, mPathfinder,
|
||||||
mGenerator.getPlayerSpawn(), playerItems));
|
mGenerator.getPlayerSpawn(), playerItems));
|
||||||
mWorld.insertCharacter(mPlayer);
|
mWorld.insertCharacter(mPlayer);
|
||||||
|
insertEnemies(enemySpawns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts enemies at the given positions, if they are more than
|
||||||
|
* Character::VISION_DISTANCE away from the player's postion.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
Game::insertEnemies(const std::vector<Vector2f>& positions) {
|
||||||
|
for (const auto& spawn : positions) {
|
||||||
|
if (thor::length(spawn - mPlayer->getPosition()) >
|
||||||
|
Character::VISION_DISTANCE)
|
||||||
|
mWorld.insertCharacter(std::make_shared<Enemy>(mWorld,
|
||||||
|
mPathfinder, spawn, mPlayer->getEquippedItems()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the lights held by the player and sets light system parameters.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
Game::initLight() {
|
Game::initLight() {
|
||||||
Yaml config("light.yaml");
|
Yaml config("light.yaml");
|
||||||
|
@ -132,7 +150,8 @@ Game::loop() {
|
||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
||||||
mGenerator.generateCurrentAreaIfNeeded(mPlayer->getPosition(), mPlayer->getEquippedItems());
|
auto enemySpawns = mGenerator.generateCurrentAreaIfNeeded(mPlayer->getPosition());
|
||||||
|
insertEnemies(enemySpawns);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ private:
|
||||||
void updateGui();
|
void updateGui();
|
||||||
void initPlayer();
|
void initPlayer();
|
||||||
void initLight();
|
void initLight();
|
||||||
|
void insertEnemies(const std::vector<Vector2f>& positions);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const int FPS_GOAL = 60;
|
static const int FPS_GOAL = 60;
|
||||||
|
|
|
@ -39,15 +39,17 @@ Generator::Generator(World& world, Pathfinder& pathfinder,
|
||||||
/**
|
/**
|
||||||
* Generates tiles near player position (maximum distance is determined by
|
* Generates tiles near player position (maximum distance is determined by
|
||||||
* GENERATE_AREA_SIZE and GENERATE_AREA_RANGE).
|
* GENERATE_AREA_SIZE and GENERATE_AREA_RANGE).
|
||||||
|
*
|
||||||
|
* @return Potential spawn points for enemies. Guaranteed to be on floor tiles.
|
||||||
*/
|
*/
|
||||||
void
|
std::vector<Vector2f>
|
||||||
Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition,
|
Generator::generateCurrentAreaIfNeeded(const Vector2f& position) {
|
||||||
const Character::EquippedItems& playerItems) {
|
std::vector<Vector2f> enemySpawns;
|
||||||
std::map<Vector2i, float> open;
|
std::map<Vector2i, float> open;
|
||||||
std::set<Vector2i> closed;
|
std::set<Vector2i> closed;
|
||||||
|
|
||||||
Vector2i start((int) floor(playerPosition.x / Tile::TILE_SIZE.x),
|
Vector2i start((int) floor(position.x / Tile::TILE_SIZE.x),
|
||||||
(int) floor(playerPosition.y / Tile::TILE_SIZE.y));
|
(int) floor(position.y / Tile::TILE_SIZE.y));
|
||||||
start /= mAreaSize;
|
start /= mAreaSize;
|
||||||
auto makePair = [&start](const Vector2i& point) {
|
auto makePair = [&start](const Vector2i& point) {
|
||||||
return std::make_pair(point, thor::length(Vector2f(point - start)));
|
return std::make_pair(point, thor::length(Vector2f(point - start)));
|
||||||
|
@ -65,12 +67,8 @@ Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition,
|
||||||
Vector2i(mAreaSize, mAreaSize) / 2,
|
Vector2i(mAreaSize, mAreaSize) / 2,
|
||||||
Vector2i(mAreaSize, mAreaSize));
|
Vector2i(mAreaSize, mAreaSize));
|
||||||
generateTiles(area);
|
generateTiles(area);
|
||||||
for (const auto& spawn : getEnemySpawns(area)) {
|
auto spawnTemp = getEnemySpawns(area);
|
||||||
float distance = thor::length(spawn - playerPosition);
|
enemySpawns.insert(enemySpawns.end(), spawnTemp.begin(), spawnTemp.end());
|
||||||
if (distance > Character::VISION_DISTANCE)
|
|
||||||
mWorld.insertCharacter(std::shared_ptr<Enemy>(new Enemy(
|
|
||||||
mWorld, mPathfinder, spawn, playerItems)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (mGenerated[current.x][current.y] && distance <= mMaxRange) {
|
if (mGenerated[current.x][current.y] && distance <= mMaxRange) {
|
||||||
if (closed.find(Vector2i(current.x + 1, current.y)) == closed.end())
|
if (closed.find(Vector2i(current.x + 1, current.y)) == closed.end())
|
||||||
|
@ -83,6 +81,7 @@ Generator::generateCurrentAreaIfNeeded(const Vector2f& playerPosition,
|
||||||
open.insert(makePair(Vector2i(current.x, current.y - 1)));
|
open.insert(makePair(Vector2i(current.x, current.y - 1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return enemySpawns;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -28,10 +28,8 @@ class Generator : public sf::Drawable {
|
||||||
public:
|
public:
|
||||||
explicit Generator(World& world, Pathfinder& pathfinder,
|
explicit Generator(World& world, Pathfinder& pathfinder,
|
||||||
ltbl::LightSystem& lightSystem, const Yaml& config);
|
ltbl::LightSystem& lightSystem, const Yaml& config);
|
||||||
void generateCurrentAreaIfNeeded(const Vector2f& position,
|
std::vector<Vector2f> generateCurrentAreaIfNeeded(const Vector2f& position);
|
||||||
const Character::EquippedItems& playerItems);
|
|
||||||
Vector2f getPlayerSpawn() const;
|
Vector2f getPlayerSpawn() const;
|
||||||
std::vector<Vector2f> getEnemySpawns(const sf::IntRect& area);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::map<int, std::map<int, Tile::Type> > array;
|
typedef std::map<int, std::map<int, Tile::Type> > array;
|
||||||
|
@ -43,6 +41,7 @@ private:
|
||||||
std::vector<Vector2i> createMinimalSpanningTree(
|
std::vector<Vector2i> createMinimalSpanningTree(
|
||||||
const Vector2i& start, const float limit);
|
const Vector2i& start, const float limit);
|
||||||
void connectRooms(const Vector2i& start);
|
void connectRooms(const Vector2i& start);
|
||||||
|
std::vector<Vector2f> getEnemySpawns(const sf::IntRect& area);
|
||||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Reference in a new issue