Skip to content

Commit

Permalink
remove events on entity components
Browse files Browse the repository at this point in the history
malytomas committed Jan 23, 2025
1 parent 0b007c2 commit ff95939
Showing 4 changed files with 8 additions and 22 deletions.
3 changes: 0 additions & 3 deletions sources/include/cage-core/entities.h
Original file line number Diff line number Diff line change
@@ -67,9 +67,6 @@ namespace cage
CAGE_FORCE_INLINE uint32 count() const { return numeric_cast<uint32>(entities().size()); }

void destroy(); // destroy all entities with this component

EventDispatcher<bool(Entity *)> entityAdded;
EventDispatcher<bool(Entity *)> entityRemoved;
};

class CAGE_CORE_API Entity : private Immovable
2 changes: 0 additions & 2 deletions sources/libcore/entities.cpp
Original file line number Diff line number Diff line change
@@ -342,7 +342,6 @@ namespace cage
ComponentImpl *ci = (ComponentImpl *)component;
if (impl->comp(ci->definitionIndex) == nullptr)
return;
ci->entityRemoved.dispatch(this);
ci->componentEntities.erase(this);
ci->desVal(impl->comp(ci->definitionIndex));
impl->comp(ci->definitionIndex) = nullptr;
@@ -379,7 +378,6 @@ namespace cage
c = ci->newVal();
detail::memcpy(c, +ci->prototype, ci->typeSize);
ci->componentEntities.insert(this);
ci->entityAdded.dispatch(this);
}
return c;
}
9 changes: 7 additions & 2 deletions sources/libengine/gui/gui.cpp
Original file line number Diff line number Diff line change
@@ -32,8 +32,13 @@ namespace cage

memory = newMemoryAllocatorStream({});

ttRemovedListener.attach(entityMgr->component<GuiTooltipMarkerComponent>()->entityRemoved);
ttRemovedListener.bind([this](Entity *e) { return this->tooltipRemoved(e); });
ttRemovedListener.attach(entityMgr->entityRemoved);
ttRemovedListener.bind(
[this](Entity *e)
{
if (e->has<GuiTooltipMarkerComponent>())
return this->tooltipRemoved(e);
});
}

GuiImpl::~GuiImpl()
16 changes: 1 addition & 15 deletions sources/test-core/entities.cpp
Original file line number Diff line number Diff line change
@@ -164,19 +164,13 @@ namespace
addListener.bind([&](Entity *) { added++; });
removeListener.bind([&](Entity *) { removed++; });
}
} manCbs, posCbs, oriCbs;
} manCbs;

Holder<EntityManager> man = newEntityManager();
manCbs.addListener.attach(man->entityAdded);
manCbs.removeListener.attach(man->entityRemoved);

EntityComponent *pos = man->defineComponent(Vec3());
posCbs.addListener.attach(pos->entityAdded);
posCbs.removeListener.attach(pos->entityRemoved);

EntityComponent *ori = man->defineComponent(Quat());
oriCbs.addListener.attach(ori->entityAdded);
oriCbs.removeListener.attach(ori->entityRemoved);

for (uint32 i = 0; i < 100; i++)
{
@@ -189,19 +183,11 @@ namespace

CAGE_TEST(manCbs.added == 100);
CAGE_TEST(manCbs.removed == 0);
CAGE_TEST(posCbs.added == 34);
CAGE_TEST(posCbs.removed == 0);
CAGE_TEST(oriCbs.added == 20);
CAGE_TEST(oriCbs.removed == 0);

ori->destroy();

CAGE_TEST(manCbs.added == 100);
CAGE_TEST(manCbs.removed == 20);
CAGE_TEST(posCbs.added == 34);
CAGE_TEST(posCbs.removed == 7);
CAGE_TEST(oriCbs.added == 20);
CAGE_TEST(oriCbs.removed == 20);
}

void randomizedTests()

0 comments on commit ff95939

Please sign in to comment.