@@ -7,53 +7,66 @@ Entity::Entity()
77 : mDebugColor(Color32_Green)
88 , mComponents()
99{
10- // add automatically transform component
10+ // automatically add transform component
1111 AddComponent<TransformComponent>();
1212}
1313
1414Entity::~Entity ()
1515{
16- DeleteAllComponents ();
16+ RemoveAllComponents ();
1717}
1818
19- void Entity::UpdateFrame (float deltaTime)
19+ void Entity::UpdateComponents (float deltaTime)
2020{
21+ bool pendingDeletion = false ;
2122 for (EntityComponent* currComponent: mComponents )
2223 {
24+ if (currComponent->IsComponentDeleted ())
25+ {
26+ pendingDeletion = true ;
27+ continue ;
28+ }
29+
2330 if (currComponent->IsComponentActive ())
2431 {
2532 currComponent->UpdateComponent (deltaTime);
2633 }
2734 }
35+
36+ if (pendingDeletion)
37+ {
38+ for (auto curr_iterator = mComponents .begin (); curr_iterator != mComponents .end (); )
39+ {
40+ EntityComponent* currComponent = *curr_iterator;
41+ if (currComponent->IsComponentDeleted ())
42+ {
43+ curr_iterator = mComponents .erase (curr_iterator);
44+ currComponent->DestroyComponent ();
45+ continue ;
46+ }
47+ ++curr_iterator;
48+ }
49+ }
2850}
2951
3052void Entity::SetActive (bool isActive)
3153{
3254 for (EntityComponent* currComponent: mComponents )
3355 {
56+ if (currComponent->IsComponentDeleted ())
57+ continue ;
58+
3459 currComponent->EnableComponent (isActive);
3560 }
3661}
3762
3863void Entity::UpdateComponentsCache ()
3964{
4065 // cache transform component
41- mTransform = nullptr ;
42- for (EntityComponent* currComponent: mComponents )
43- {
44- mTransform = cxx::rtti_cast<TransformComponent>(currComponent);
45- if (mTransform )
46- break ;
47- }
48-
66+ mTransform = GetComponent<TransformComponent>();
67+
4968 // cache renderable component
50- mRenderable = nullptr ;
51- for (EntityComponent* currComponent: mComponents )
52- {
53- mRenderable = cxx::rtti_cast<RenderableComponent>(currComponent);
54- if (mRenderable )
55- break ;
56- }
69+ mRenderable = GetComponent<RenderableComponent>();
5770}
5871
5972void Entity::AttachComponent (EntityComponent* component)
@@ -66,23 +79,23 @@ void Entity::AttachComponent(EntityComponent* component)
6679 component->AwakeComponent ();
6780}
6881
69- void Entity::DeleteComponent (EntityComponent* component)
82+ void Entity::RemoveComponent (EntityComponent* component)
7083{
7184 if (component)
7285 {
7386 cxx::erase_elements (mComponents , component);
74- component->DeleteComponent ();
87+ component->DestroyComponent ();
7588
7689 UpdateComponentsCache ();
7790 }
7891 debug_assert (component);
7992}
8093
81- void Entity::DeleteAllComponents ()
94+ void Entity::RemoveAllComponents ()
8295{
8396 for (EntityComponent* currComponent: mComponents )
8497 {
85- currComponent->DeleteComponent ();
98+ currComponent->DestroyComponent ();
8699 }
87100 mComponents .clear ();
88101 UpdateComponentsCache ();
@@ -92,9 +105,17 @@ bool Entity::IsActive() const
92105{
93106 for (EntityComponent* currComponent: mComponents )
94107 {
108+ if (currComponent->IsComponentDeleted ())
109+ continue ;
110+
95111 if (currComponent->IsComponentActive ())
96112 return true ;
97113 }
98114
99115 return false ;
100- }
116+ }
117+
118+ void Entity::SetController (EntityController* controller)
119+ {
120+ mController = controller;
121+ }
0 commit comments