Skip to content

Commit 5c4ec27

Browse files
committed
addressing more comments.
1 parent aa321c0 commit 5c4ec27

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

attachments/simple_engine/entity.h

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <string>
66
#include <algorithm>
77
#include <chrono>
8-
#include <typeindex>
9-
#include <unordered_map>
108
#include <type_traits>
119

1210
#include "component.h"
@@ -22,7 +20,6 @@ class Entity {
2220
std::string name;
2321
bool active = true;
2422
std::vector<std::unique_ptr<Component>> components;
25-
std::unordered_map<std::type_index, Component*> componentMap;
2623

2724
public:
2825
/**
@@ -88,10 +85,7 @@ class Entity {
8885
// Set the owner
8986
componentPtr->SetOwner(this);
9087

91-
// Add to the map for quick lookup
92-
componentMap[std::type_index(typeid(T))] = componentPtr;
93-
94-
// Add to the vector for ownership
88+
// Add to the vector for ownership and iteration
9589
components.push_back(std::move(component));
9690

9791
// Initialize the component
@@ -109,11 +103,12 @@ class Entity {
109103
T* GetComponent() const {
110104
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
111105

112-
auto it = componentMap.find(std::type_index(typeid(T)));
113-
if (it != componentMap.end()) {
114-
return static_cast<T*>(it->second);
106+
// Search from the back to preserve previous behavior of returning the last-added component of type T
107+
for (auto it = components.rbegin(); it != components.rend(); ++it) {
108+
if (auto* casted = dynamic_cast<T*>(it->get())) {
109+
return casted;
110+
}
115111
}
116-
117112
return nullptr;
118113
}
119114

@@ -126,21 +121,9 @@ class Entity {
126121
bool RemoveComponent() {
127122
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
128123

129-
auto it = componentMap.find(std::type_index(typeid(T)));
130-
if (it != componentMap.end()) {
131-
Component* componentPtr = it->second;
132-
133-
// Remove from the map
134-
componentMap.erase(it);
135-
136-
// Find and remove from the vector
137-
auto vecIt = std::find_if(components.begin(), components.end(),
138-
[componentPtr](const std::unique_ptr<Component>& comp) {
139-
return comp.get() == componentPtr;
140-
});
141-
142-
if (vecIt != components.end()) {
143-
components.erase(vecIt);
124+
for (auto it = components.rbegin(); it != components.rend(); ++it) {
125+
if (dynamic_cast<T*>(it->get()) != nullptr) {
126+
components.erase(std::next(it).base());
144127
return true;
145128
}
146129
}
@@ -156,6 +139,6 @@ class Entity {
156139
template<typename T>
157140
bool HasComponent() const {
158141
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
159-
return componentMap.contains(std::type_index(typeid(T)));
142+
return GetComponent<T>() != nullptr;
160143
}
161144
};

0 commit comments

Comments
 (0)