Skip to content

Commit

Permalink
ECS: Add getEntitiesByComponents method to Registry
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
guillaumeAbel committed Oct 2, 2023
1 parent b6a6f37 commit 48231fb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 35 deletions.
16 changes: 8 additions & 8 deletions src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ namespace Systems {
std::size_t /*unused*/)
{
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPosition =
Registry::components<Types::Position> arrPos =
registry.getComponents<Types::Position>();
std::vector<std::size_t> playerId =
registry.getComponents<Types::Player>().getExistingsId();
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::Player), typeid(Types::Position)});

for (std::size_t id : playerId) {
for (auto id : ids) {
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_RIGHT)) {
checkAnimRect(id);
arrPosition[id].x += 1;
arrPos[id].x += 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_LEFT)) {
checkAnimRect(id);
arrPosition[id].x -= 1;
arrPos[id].x -= 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_UP)) {
checkAnimRect(id);
arrPosition[id].y -= 1;
arrPos[id].y -= 1;
}
if (Raylib::isKeyDown(Raylib::KeyboardKey::KB_DOWN)) {
checkAnimRect(id);
arrPosition[id].y += 1;
arrPos[id].y += 1;
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions src/Client/Systems/Graphic/SpriteSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ namespace Systems {
Registry &registry = Registry::getInstance();
Registry::components<Types::AnimRect> arrAnimRect =
registry.getComponents<Types::AnimRect>();
std::vector<std::size_t> ids = arrAnimRect.getExistingsId();
Registry::components<Types::Rect> arrRect =
registry.getComponents<Types::Rect>();
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::AnimRect), typeid(Types::Rect)});

for (auto id : ids) {
Types::AnimRect &animRect = arrAnimRect[id];
if (arrRect.exist(id)) {
arrRect[id] =
getCurrentList(animRect)[animRect.currentRectInList];
}
arrRect[id] = getCurrentList(animRect)[animRect.currentRectInList];
if (animRect.currentRectList != Types::RectListType::DEFAULTRECT) {
animRect.currentRectInList++;
if (animRect.currentRectInList
Expand All @@ -56,13 +54,10 @@ namespace Systems {
registry.getComponents<Types::Position>();
Registry::components<Types::RectangleShape> arrRect =
registry.getComponents<Types::RectangleShape>();
std::vector<std::size_t> rectShapeIndexes = arrRect.getExistingsId();

std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::RectangleShape), typeid(Types::Position)});

for (auto id : rectShapeIndexes) {
if (!arrPosition.exist(id)) {
continue;
}
for (auto id : ids) {
Types::Position &position = arrPosition[id];
Types::RectangleShape &rectangle = arrRect[id];

Expand Down
38 changes: 38 additions & 0 deletions src/ECS/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "Registry.hpp"
#include <iostream>
#include <string>

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
Expand Down Expand Up @@ -41,6 +42,43 @@ void Registry::clear()
_entitiesNb = 0;
}

static std::vector<std::size_t>
match(std::vector<std::size_t> fst, std::vector<std::size_t> scd)
{
std::vector<std::size_t> res;

for (auto it = fst.begin(); it != fst.end(); it++) {
for (auto scdIt = scd.begin(); scdIt != scd.end(); scdIt++) {
if (*it == *scdIt) {
res.push_back(*it);
}
}
}
return res;
}

std::vector<std::size_t> Registry::getExistings(std::type_index type)
{
auto funcIt = _getExistingsId.find(type);
if (funcIt == _getExistingsId.end()) {
return {};
}
return (funcIt->second)(*this);
}

std::vector<std::size_t>
Registry::getEntitiesByComponents(std::vector<std::type_index> types)
{
auto it = types.begin();
std::vector<std::size_t> res = getExistings(*it);
it++;

for (; it != types.end(); it++) {
res = match(res, getExistings(*it));
}
return res;
}

void Registry::setToBackLayers(std::size_t id, BackLayers layer)
{
removeFromDefaultLayer(id);
Expand Down
18 changes: 18 additions & 0 deletions src/ECS/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <any>
#include <functional>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -50,6 +51,9 @@ class Registry {

void clear();

std::vector<std::size_t>
getEntitiesByComponents(std::vector<std::type_index>);

void
setToBackLayers(std::size_t id, BackLayers layer = BackLayers::BACK);

Expand Down Expand Up @@ -77,6 +81,8 @@ class Registry {

void removeFromDefaultLayer(std::size_t id);

std::vector<std::size_t> getExistings(std::type_index type);

template <typename Component>
void checkAddSparseArray()
{
Expand All @@ -86,6 +92,8 @@ class Registry {
&Registry::addComponentPlace<Component>);
_removeComponentFunctions.push_back(
&Registry::removeComponent<Component>);
_getExistingsId[typeid(Component)] =
&Registry::getExistingsId<Component>;
components<Component> componentArray = castReturn<Component>();
for (std::size_t i = 0; i < _entitiesNb; i++) {
componentArray.add();
Expand All @@ -105,6 +113,12 @@ class Registry {
castReturn<Component>().erase(id);
}

template <typename Component>
std::vector<std::size_t> getExistingsId()
{
return castReturn<Component>().getExistingsId();
}

template <class Component>
components<Component> castReturn()
{
Expand All @@ -120,6 +134,10 @@ class Registry {
_addComponentPlaceFunctions;
std::vector<std::function<void(Registry &, std::size_t)>>
_removeComponentFunctions;
std::unordered_map<
std::type_index,
std::function<std::vector<std::size_t>(Registry &)>>
_getExistingsId;
std::unordered_map<std::type_index, std::any> _data;

std::size_t _entitiesNb;
Expand Down
29 changes: 13 additions & 16 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ namespace Systems {
registry.getComponents<Types::Position>();
Registry::components<Types::CollisionRect> arrCollisionRect =
registry.getComponents<Types::CollisionRect>();

std::vector<std::size_t> playerIdx =
registry.getComponents<Types::Player>().getExistingsId();
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::Player),
typeid(Types::Position),
typeid(Types::CollisionRect)});

const float maxPercent = 100.0F;
for (std::size_t id : playerIdx) {
for (std::size_t id : ids) {
if (arrPosition[id].x < 0) {
arrPosition[id].x = 0;
}
Expand Down Expand Up @@ -85,19 +86,16 @@ namespace Systems {

void entitiesCollision(std::size_t /*unused*/, std::size_t /*unused*/)
{
Registry &registry = Registry::getInstance();
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
registry.getComponents<Types::Position>();
Registry::components<Types::CollisionRect> arrCollisionRect =
Registry::getInstance().getComponents<Types::CollisionRect>();

std::vector<std::size_t> ids = arrPosition.getExistingsId();
auto itIds = ids.begin();
registry.getComponents<Types::CollisionRect>();
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::CollisionRect), typeid(Types::Position)});

while (itIds != ids.end()) {
if (arrCollisionRect.exist(*itIds)) {
checkCollisionEntity(itIds, ids, arrPosition, arrCollisionRect);
}
itIds++;
for (auto itIds = ids.begin(); itIds != ids.end(); itIds++) {
checkCollisionEntity(itIds, ids, arrPosition, arrCollisionRect);
}
}

Expand Down Expand Up @@ -142,8 +140,6 @@ namespace Systems {
Registry::getInstance().getComponents<Types::Player>().insertBack({});

id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Position>().insertBack(
{playerData, playerData});
Registry::getInstance().getComponents<Raylib::Sprite>().insertBack(
{playerPath, playerWidth, playerHeight, id});
Registry::getInstance().getComponents<Types::Rect>().insertBack(
Expand All @@ -153,6 +149,7 @@ namespace Systems {
.insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Health>().insertBack(
{playerHealth});
Registry::getInstance().getComponents<Types::Player>().insertBack({});

id = Registry::getInstance().addEntity();
Registry::getInstance().getComponents<Types::Position>().insertBack(
Expand Down

0 comments on commit 48231fb

Please sign in to comment.