Skip to content

Commit 603193f

Browse files
committed
feat: add spawn_random_within_skybox function to spawn entities randomly within the skybox
1 parent 9e4995c commit 603193f

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Flakkari/Engine/EntityComponentSystem/Systems/Systems.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,67 @@ void apply_movable(Registry &r, float deltaTime)
100100
}
101101
}
102102

103+
static float randomRange(float min, float max)
104+
{
105+
#ifdef _WIN32
106+
return min + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (max - min)));
107+
#else
108+
std::random_device rd;
109+
std::mt19937 gen(rd());
110+
std::uniform_real_distribution<float> dis(min, max);
111+
112+
return dis(gen);
113+
#endif
114+
}
115+
116+
void spawn_random_within_skybox(Registry &r)
117+
{
118+
if (!r.isRegistered<ECS::Components::_3D::Transform>() || !r.isRegistered<ECS::Components::Common::Tag>() ||
119+
!r.isRegistered<ECS::Components::Common::Spawned>())
120+
return;
121+
122+
auto &transforms = r.getComponents<ECS::Components::_3D::Transform>();
123+
auto &tags = r.getComponents<ECS::Components::Common::Tag>();
124+
auto &spawned = r.getComponents<ECS::Components::Common::Spawned>();
125+
126+
float maxRangeX = 0;
127+
float maxRangeY = 0;
128+
float maxRangeZ = 0;
129+
130+
for (Entity i(0); i < transforms.size() && i < tags.size(); ++i)
131+
{
132+
auto &transform = transforms[i];
133+
auto &tag = tags[i];
134+
135+
if (transform.has_value() && tag.has_value())
136+
{
137+
if (tag->tag == "Skybox")
138+
{
139+
maxRangeX = transform->_scale.vec.x / 2;
140+
maxRangeY = transform->_scale.vec.y / 2;
141+
maxRangeZ = transform->_scale.vec.z / 2;
142+
break;
143+
}
144+
}
145+
}
146+
147+
for (Entity i(0); i < transforms.size() && i < tags.size(); ++i)
148+
{
149+
auto &transform = transforms[i];
150+
auto &tag = tags[i];
151+
auto &spawn = spawned[i];
152+
153+
if (transform.has_value() && tag.has_value() && !spawn.has_value())
154+
{
155+
if ((tag->tag == "Player" || tag->tag == "Enemy") && spawn->has_spawned == false)
156+
{
157+
transform->_position.vec.x = randomRange(-maxRangeX, maxRangeX);
158+
transform->_position.vec.y = randomRange(-maxRangeY, maxRangeY);
159+
transform->_position.vec.z = randomRange(-maxRangeZ, maxRangeZ);
160+
spawn->has_spawned = true;
161+
}
162+
}
163+
}
164+
}
165+
103166
} // namespace Flakkari::Engine::ECS::Systems::_3D

Flakkari/Engine/EntityComponentSystem/Systems/Systems.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define FLAKKARI_SYSTEMS_HPP_
1818

1919
#include "../Components/Components2D.hpp"
20+
#include "../Components/Components3D.hpp"
2021
#include "../Components/ComponentsCommon.hpp"
2122
#include "../Registry.hpp"
2223

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

51+
/**
52+
* @brief Spawns a random entity within a skybox.
53+
*
54+
* @param r The registry containing the entities to update.
55+
*/
56+
void spawn_random_within_skybox(Registry &r);
57+
5058
} // namespace Flakkari::Engine::ECS::Systems::_3D
5159

5260
#endif /* !FLAKKARI_SYSTEMS_HPP_ */

Flakkari/Server/Game/Game.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ void Game::loadSystems(Engine::ECS::Registry &registry, const std::string &name)
7272
else if (name == "apply_movable")
7373
registry.add_system(
7474
[this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::apply_movable(r, _deltaTime); });
75+
76+
else if (name == "spawn_random_within_skybox")
77+
registry.add_system(
78+
[this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::spawn_random_within_skybox(r); });
7579
}
7680

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

0 commit comments

Comments
 (0)