Skip to content

Commit 2791553

Browse files
committed
Systems being notified when component is added or removed
1 parent d016762 commit 2791553

21 files changed

Lines changed: 208 additions & 140 deletions

engine/core/Scene.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,13 @@ bool Scene::isEntityCreated(Entity entity){
252252
}
253253

254254
void Scene::destroyEntity(Entity entity){
255-
255+
Signature signature = entityManager.getSignature(entity);
256256
for (auto const& pair : systems){
257-
pair.second->entityDestroyed(entity);
257+
for (ComponentId componentId = 0; componentId < signature.size(); ++componentId) {
258+
if (signature.test(componentId)) {
259+
pair.second->onComponentRemoved(entity, componentId);
260+
}
261+
}
258262
}
259263

260264
componentManager.entityDestroyed(entity);

engine/core/Scene.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,20 @@ namespace Supernova{
164164
auto signature = entityManager.getSignature(entity);
165165
signature.set(componentManager.getComponentId<T>(), true);
166166
entityManager.setSignature(entity, signature);
167+
168+
ComponentId componentId = componentManager.getComponentId<T>();
169+
for (auto const& pair : systems){
170+
pair.second->onComponentAdded(entity, componentId);
171+
}
167172
}
168173

169174
template<typename T>
170175
void removeComponent(Entity entity){
176+
ComponentId componentId = componentManager.getComponentId<T>();
177+
for (auto const& pair : systems){
178+
pair.second->onComponentRemoved(entity, componentId);
179+
}
180+
171181
componentManager.removeComponent<T>(entity);
172182
auto signature = entityManager.getSignature(entity);
173183
signature.set(componentManager.getComponentId<T>(), false);

engine/core/component/TextComponent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace Supernova{
3131

3232
std::shared_ptr<STBText> stbtext = NULL;
3333

34-
bool needReload = false;
34+
bool needReloadAtlas = false;
3535
bool needUpdateText = true;
3636
};
3737

engine/core/ecs/SubSystem.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// (c) 2024 Eduardo Doria.
2+
// (c) 2025 Eduardo Doria.
33
//
44

55
#ifndef SUBSYSTEM_H
@@ -8,6 +8,7 @@
88
#include "Export.h"
99
#include "Entity.h"
1010
#include "Signature.h"
11+
#include "ComponentManager.h"
1112
#include <set>
1213

1314
namespace Supernova{
@@ -34,7 +35,8 @@ namespace Supernova{
3435

3536
virtual void update(double dt) = 0;
3637

37-
virtual void entityDestroyed(Entity entity) = 0;
38+
virtual void onComponentAdded(Entity entity, ComponentId componentId) {}
39+
virtual void onComponentRemoved(Entity entity, ComponentId componentId) {}
3840

3941
};
4042

engine/core/object/ui/Button.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ using namespace Supernova;
1111

1212
Button::Button(Scene* scene): Image(scene){
1313
addComponent<ButtonComponent>({});
14-
15-
ButtonComponent& btcomp = getComponent<ButtonComponent>();
16-
scene->getSystem<UISystem>()->createButtonObjects(entity, btcomp);
1714
}
1815

1916
Button::Button(Scene* scene, Entity entity): Image(scene, entity){

engine/core/object/ui/Panel.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ using namespace Supernova;
1111

1212
Panel::Panel(Scene* scene): Image(scene){
1313
addComponent<PanelComponent>({});
14-
15-
PanelComponent& panelcomp = getComponent<PanelComponent>();
16-
scene->getSystem<UISystem>()->createPanelObjects(entity, panelcomp);
1714
}
1815

1916
Panel::Panel(Scene* scene, Entity entity): Image(scene, entity){

engine/core/object/ui/Scrollbar.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ using namespace Supernova;
1010

1111
Scrollbar::Scrollbar(Scene* scene): Image(scene){
1212
addComponent<ScrollbarComponent>({});
13-
14-
ScrollbarComponent& scrollcomp = getComponent<ScrollbarComponent>();
15-
scene->getSystem<UISystem>()->createScrollbarObjects(entity, scrollcomp);
1613
}
1714

1815
Scrollbar::Scrollbar(Scene* scene, Entity entity): Image(scene, entity){

engine/core/object/ui/Text.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void Text::setMaxTextSize(unsigned int maxTextSize){
7474
if (textcomp.maxTextSize != maxTextSize){
7575
textcomp.maxTextSize = maxTextSize;
7676

77-
textcomp.needReload = true;
77+
textcomp.needReloadAtlas = true;
7878
textcomp.needUpdateText = true;
7979
ui.needReload = true;
8080
}
@@ -107,7 +107,7 @@ void Text::setFont(const std::string &font){
107107
if (textcomp.font != font){
108108
textcomp.font = font;
109109

110-
textcomp.needReload = true;
110+
textcomp.needReloadAtlas = true;
111111
textcomp.needUpdateText = true;
112112
}
113113
}
@@ -123,7 +123,7 @@ void Text::setFontSize(unsigned int fontSize){
123123

124124
textcomp.fontSize = fontSize;
125125

126-
textcomp.needReload = true;
126+
textcomp.needReloadAtlas = true;
127127
textcomp.needUpdateText = true;
128128
}
129129

engine/core/object/ui/TextEdit.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ using namespace Supernova;
1111

1212
TextEdit::TextEdit(Scene* scene): Image(scene){
1313
addComponent<TextEditComponent>({});
14-
15-
TextEditComponent& tecomp = getComponent<TextEditComponent>();
16-
scene->getSystem<UISystem>()->createTextEditObjects(entity, tecomp);
1714
}
1815

1916
TextEdit::TextEdit(Scene* scene, Entity entity): Image(scene, entity){

engine/core/subsystem/ActionSystem.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// (c) 2024 Eduardo Doria.
2+
// (c) 2025 Eduardo Doria.
33
//
44

55
#include "ActionSystem.h"
@@ -1117,14 +1117,16 @@ void ActionSystem::update(double dt){
11171117
}
11181118
}
11191119

1120-
void ActionSystem::entityDestroyed(Entity entity){
1121-
Signature signature = scene->getSignature(entity);
1120+
void ActionSystem::onComponentAdded(Entity entity, ComponentId componentId) {
11221121

1123-
if (signature.test(scene->getComponentId<ActionComponent>())){
1124-
actionDestroy(scene->getComponent<ActionComponent>(entity));
1125-
}
1122+
}
11261123

1127-
if (signature.test(scene->getComponentId<AnimationComponent>())){
1128-
animationDestroy(scene->getComponent<AnimationComponent>(entity));
1129-
}
1124+
void ActionSystem::onComponentRemoved(Entity entity, ComponentId componentId) {
1125+
if (componentId == scene->getComponentId<ActionComponent>()) {
1126+
ActionComponent& action = scene->getComponent<ActionComponent>(entity);
1127+
actionDestroy(action);
1128+
} else if (componentId == scene->getComponentId<AnimationComponent>()) {
1129+
AnimationComponent& animation = scene->getComponent<AnimationComponent>(entity);
1130+
animationDestroy(animation);
1131+
}
11301132
}

0 commit comments

Comments
 (0)