diff options
author | Max-001 <80035972+Max-001@users.noreply.github.com> | 2024-10-02 12:35:01 +0200 |
---|---|---|
committer | Max-001 <80035972+Max-001@users.noreply.github.com> | 2024-10-02 12:35:01 +0200 |
commit | e409986d9a21ca96ee0b491826eb0008ff6ab8e0 (patch) | |
tree | 89f04830a0e3727f53e15b4ebff70a95add40c30 /mwe/ecs-homemade/inc | |
parent | aedbb8b82f13d8b390419e59cea098ea73295df5 (diff) |
Changed componentsManager to allow mutliple componts of the same type for one entity (id)
Diffstat (limited to 'mwe/ecs-homemade/inc')
-rw-r--r-- | mwe/ecs-homemade/inc/ComponentManager.h | 12 | ||||
-rw-r--r-- | mwe/ecs-homemade/inc/ComponentManager.hpp | 47 | ||||
-rw-r--r-- | mwe/ecs-homemade/inc/GameObjectMax.h | 4 | ||||
-rw-r--r-- | mwe/ecs-homemade/inc/GameObjectMax.hpp | 6 |
4 files changed, 41 insertions, 28 deletions
diff --git a/mwe/ecs-homemade/inc/ComponentManager.h b/mwe/ecs-homemade/inc/ComponentManager.h index cfc5f20..5b0629f 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.h +++ b/mwe/ecs-homemade/inc/ComponentManager.h @@ -17,21 +17,21 @@ public: ComponentManager& operator=(const ComponentManager&) = delete; ComponentManager& operator=(ComponentManager&&) = delete; + template <typename T, typename... Args> + void AddComponent(std::uint32_t id, Args&&... args); template <typename T> - void AddComponent(T* component, std::uint32_t id); - template <typename T> - T* GetComponent(std::uint32_t id); - template <typename T> + std::vector<std::reference_wrapper<T>> GetComponentsOfID(std::uint32_t id); + /*template <typename T> std::vector<std::uint32_t> GetAllComponentIDs(); template <typename T> - std::vector<T*> GetAllComponentPointer(); + std::vector<T*> GetAllComponentPointer();*/ private: static ComponentManager mInstance; ComponentManager(); - std::unordered_map<std::type_index, std::vector<Component*>> mComponents; //TODO: Make this not only work with Component* OR add extra checks at templated methodes!!! + std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> mComponents; }; #include "ComponentManager.hpp" 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; -} +}*/ diff --git a/mwe/ecs-homemade/inc/GameObjectMax.h b/mwe/ecs-homemade/inc/GameObjectMax.h index dbfe981..f0bcec9 100644 --- a/mwe/ecs-homemade/inc/GameObjectMax.h +++ b/mwe/ecs-homemade/inc/GameObjectMax.h @@ -7,8 +7,8 @@ class GameObject { public: GameObject(std::uint32_t id, std::string name, std::string tag, int layer); - template <typename T> - void AddComponent(T* component); + template <typename T, typename... Args> + void AddComponent(Args&&... args); std::uint32_t mId; std::string mName; diff --git a/mwe/ecs-homemade/inc/GameObjectMax.hpp b/mwe/ecs-homemade/inc/GameObjectMax.hpp index e1a49be..1e952ba 100644 --- a/mwe/ecs-homemade/inc/GameObjectMax.hpp +++ b/mwe/ecs-homemade/inc/GameObjectMax.hpp @@ -1,6 +1,6 @@ #include "ComponentManager.h" -template <typename T> -void GameObject::AddComponent(T* component) { - ComponentManager::GetInstance().AddComponent(component, mId); +template <typename T, typename... Args> +void GameObject::AddComponent(Args&&... args) { + ComponentManager::GetInstance().AddComponent<T>(mId, std::forward<Args>(args)...); } |