Skip to content

Commit

Permalink
feat: add spawn_random_within_skybox function to spawn entities rando…
Browse files Browse the repository at this point in the history
…mly within the skybox
  • Loading branch information
MasterLaplace committed Nov 17, 2024
1 parent 9e4995c commit 603193f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,67 @@ void apply_movable(Registry &r, float deltaTime)
}
}

static float randomRange(float min, float max)
{
#ifdef _WIN32
return min + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (max - min)));
#else
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> dis(min, max);

return dis(gen);
#endif
}

void spawn_random_within_skybox(Registry &r)
{
if (!r.isRegistered<ECS::Components::_3D::Transform>() || !r.isRegistered<ECS::Components::Common::Tag>() ||
!r.isRegistered<ECS::Components::Common::Spawned>())
return;

auto &transforms = r.getComponents<ECS::Components::_3D::Transform>();
auto &tags = r.getComponents<ECS::Components::Common::Tag>();
auto &spawned = r.getComponents<ECS::Components::Common::Spawned>();

float maxRangeX = 0;
float maxRangeY = 0;
float maxRangeZ = 0;

for (Entity i(0); i < transforms.size() && i < tags.size(); ++i)
{
auto &transform = transforms[i];
auto &tag = tags[i];

if (transform.has_value() && tag.has_value())
{
if (tag->tag == "Skybox")
{
maxRangeX = transform->_scale.vec.x / 2;
maxRangeY = transform->_scale.vec.y / 2;
maxRangeZ = transform->_scale.vec.z / 2;
break;
}
}
}

for (Entity i(0); i < transforms.size() && i < tags.size(); ++i)
{
auto &transform = transforms[i];
auto &tag = tags[i];
auto &spawn = spawned[i];

if (transform.has_value() && tag.has_value() && !spawn.has_value())
{
if ((tag->tag == "Player" || tag->tag == "Enemy") && spawn->has_spawned == false)
{
transform->_position.vec.x = randomRange(-maxRangeX, maxRangeX);
transform->_position.vec.y = randomRange(-maxRangeY, maxRangeY);
transform->_position.vec.z = randomRange(-maxRangeZ, maxRangeZ);
spawn->has_spawned = true;
}
}
}
}

} // namespace Flakkari::Engine::ECS::Systems::_3D
8 changes: 8 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Systems/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define FLAKKARI_SYSTEMS_HPP_

#include "../Components/Components2D.hpp"
#include "../Components/Components3D.hpp"
#include "../Components/ComponentsCommon.hpp"
#include "../Registry.hpp"

Expand Down Expand Up @@ -47,6 +48,13 @@ namespace Flakkari::Engine::ECS::Systems::_3D {
*/
void apply_movable(Registry &r, float deltaTime);

/**
* @brief Spawns a random entity within a skybox.
*
* @param r The registry containing the entities to update.
*/
void spawn_random_within_skybox(Registry &r);

} // namespace Flakkari::Engine::ECS::Systems::_3D

#endif /* !FLAKKARI_SYSTEMS_HPP_ */
4 changes: 4 additions & 0 deletions Flakkari/Server/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ void Game::loadSystems(Engine::ECS::Registry &registry, const std::string &name)
else if (name == "apply_movable")
registry.add_system(
[this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::apply_movable(r, _deltaTime); });

else if (name == "spawn_random_within_skybox")
registry.add_system(
[this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::spawn_random_within_skybox(r); });
}

void Game::loadComponents(Engine::ECS::Registry &registry, const nl_component &components,
Expand Down

0 comments on commit 603193f

Please sign in to comment.