aboutsummaryrefslogtreecommitdiff
path: root/mwe/ecs-homemade/inc/ComponentManager.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'mwe/ecs-homemade/inc/ComponentManager.hpp')
-rw-r--r--mwe/ecs-homemade/inc/ComponentManager.hpp47
1 files changed, 30 insertions, 17 deletions
diff --git a/mwe/ecs-homemade/inc/ComponentManager.hpp b/mwe/ecs-homemade/inc/ComponentManager.hpp
index df8ea32..a7fc30f 100644
--- a/mwe/ecs-homemade/inc/ComponentManager.hpp
+++ b/mwe/ecs-homemade/inc/ComponentManager.hpp
@@ -1,31 +1,44 @@
-// AddComponent implementation
-template <typename T>
-void ComponentManager::AddComponent(T* component, std::uint32_t id) {
- std::type_index type = typeid(T);
- if (mComponents.find(type) == mComponents.end()) {
- mComponents[type] = std::vector<Component*>();
+
+template <typename T, typename... Args>
+void ComponentManager::AddComponent(std::uint32_t id, Args&&... args) {
+ std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>)
+
+ if (mComponents.find(type) == mComponents.end()) { //Check if this component type is already in the unordered_map<>
+ mComponents[type] = std::vector<std::vector<std::unique_ptr<Component>>>(); //If not, create a new (empty) vector<> of unique_ptr<Component>
}
- // Resize the vector if the id is greater than current size
- if (id >= mComponents[type].size()) {
- mComponents[type].resize(id + 1, nullptr); // Initialize new slots to nullptr
+
+ if (id >= mComponents[type].size()) { //Resize the vector<> if the id is greater than the current size
+ mComponents[type].resize(id + 1); //Initialize new slots to nullptr (resize does this automatically)
}
- mComponents[type][id] = component; // Store the raw pointer
+
+ mComponents[type][id].push_back(std::make_unique<T>(std::forward<Args>(args)...)); //Create a new component of type T using perfect forwarding and store its unique_ptr in the correct vector
}
-// GetComponent implementation
template <typename T>
-T* ComponentManager::GetComponent(std::uint32_t id) {
+std::vector<std::reference_wrapper<T>> ComponentManager::GetComponentsOfID(std::uint32_t id) {
std::type_index type = typeid(T);
+
+ std::vector<std::reference_wrapper<T>> componentVector;
+
if (mComponents.find(type) != mComponents.end()) {
- auto& componentArray = mComponents[type];
+
+ const auto& componentArray = mComponents[type];
+
if (id < componentArray.size()) {
- return static_cast<T*>(componentArray[id]); // Cast to the correct type
+ for (const auto& componentPtr : componentArray[id]) {
+ // Use static_cast instead of dynamic_cast
+ T* castedComponent = static_cast<T*>(componentPtr.get());
+ if (castedComponent) { // Ensure cast was successful
+ componentVector.push_back(*castedComponent); // Add the raw pointer to the vector
+ }
+ }
}
}
- return nullptr; // Return nullptr if not found
+
+ return componentVector; // Return empty vector if not found
}
-// GetAllComponentIDs implementation
+/*// GetAllComponentIDs implementation
template <typename T>
std::vector<std::uint32_t> ComponentManager::GetAllComponentIDs() {
std::type_index type = typeid(T);
@@ -55,4 +68,4 @@ std::vector<T*> ComponentManager::GetAllComponentPointer() {
}
}
return pointers;
-}
+}*/