From e409986d9a21ca96ee0b491826eb0008ff6ab8e0 Mon Sep 17 00:00:00 2001 From: Max-001 <80035972+Max-001@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:35:01 +0200 Subject: Changed componentsManager to allow mutliple componts of the same type for one entity (id) --- mwe/ecs-homemade/inc/ComponentManager.h | 12 ++++---- mwe/ecs-homemade/inc/ComponentManager.hpp | 47 ++++++++++++++++++++----------- mwe/ecs-homemade/inc/GameObjectMax.h | 4 +-- mwe/ecs-homemade/inc/GameObjectMax.hpp | 6 ++-- mwe/ecs-homemade/src/main.cpp | 20 ++++++------- 5 files changed, 51 insertions(+), 38 deletions(-) (limited to 'mwe') 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 + void AddComponent(std::uint32_t id, Args&&... args); template - void AddComponent(T* component, std::uint32_t id); - template - T* GetComponent(std::uint32_t id); - template + std::vector> GetComponentsOfID(std::uint32_t id); + /*template std::vector GetAllComponentIDs(); template - std::vector GetAllComponentPointer(); + std::vector GetAllComponentPointer();*/ private: static ComponentManager mInstance; ComponentManager(); - std::unordered_map> mComponents; //TODO: Make this not only work with Component* OR add extra checks at templated methodes!!! + std::unordered_map>>> 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 -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(); + +template +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>>(); //If not, create a new (empty) vector<> of unique_ptr } - // 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(std::forward(args)...)); //Create a new component of type T using perfect forwarding and store its unique_ptr in the correct vector } -// GetComponent implementation template -T* ComponentManager::GetComponent(std::uint32_t id) { +std::vector> ComponentManager::GetComponentsOfID(std::uint32_t id) { std::type_index type = typeid(T); + + std::vector> componentVector; + if (mComponents.find(type) != mComponents.end()) { - auto& componentArray = mComponents[type]; + + const auto& componentArray = mComponents[type]; + if (id < componentArray.size()) { - return static_cast(componentArray[id]); // Cast to the correct type + for (const auto& componentPtr : componentArray[id]) { + // Use static_cast instead of dynamic_cast + T* castedComponent = static_cast(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 std::vector ComponentManager::GetAllComponentIDs() { std::type_index type = typeid(T); @@ -55,4 +68,4 @@ std::vector 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 - void AddComponent(T* component); + template + 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 -void GameObject::AddComponent(T* component) { - ComponentManager::GetInstance().AddComponent(component, mId); +template +void GameObject::AddComponent(Args&&... args) { + ComponentManager::GetInstance().AddComponent(mId, std::forward(args)...); } diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index 3dd082f..73f710b 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -10,22 +10,22 @@ int main() { GameObject gameObect0(0, "Name: 0", "Tag: 0", 0); //Entity 0 GameObject gameObect1(1, "Name: 1", "Tag: 1", 1); //Entity 1 - Sprite sprite0; - Rigidbody rigidbody0(1, 2, 3); - gameObect0.AddComponent(&sprite0); //Add a sprite to entity0 - gameObect0.AddComponent(&rigidbody0); //Also add a rigidbody to entity0 + //Sprite sprite0; + //Rigidbody rigidbody0(1, 2, 3); + gameObect0.AddComponent(); //Add a sprite to entity0 + gameObect0.AddComponent(1, 2, 3); //Also add a rigidbody to entity0 - Rigidbody rigidbody1(4, 5, 6); - gameObect1.AddComponent(&rigidbody1); //Only add a rigidbody to entity1 + //Rigidbody rigidbody1(4, 5, 6); + gameObect1.AddComponent(4, 5, 6); //Only add a rigidbody to entity1 //The entities are now initialized //Now I will demonstrate some ways of retreiving/getting components - Rigidbody* rigidbodyOfEntity0 = ComponentManager::GetInstance().GetComponent(gameObect0.mId); //Get the pointer to the Rigidbody component of entity 0 - std::cout << "rigidbodyOfEntity0: " << rigidbodyOfEntity0->mMass << " " << rigidbodyOfEntity0->mGravityScale << " " << rigidbodyOfEntity0->mBodyType << std::endl; + std::vector> rigidbodyOfEntity0 = ComponentManager::GetInstance().GetComponentsOfID(gameObect0.mId); //Get the pointer to the Rigidbody component of entity 0 + std::cout << "rigidbodyOfEntity0: " << rigidbodyOfEntity0[0].get().mMass << " " << rigidbodyOfEntity0[0].get().mGravityScale << " " << rigidbodyOfEntity0[0].get().mBodyType << std::endl; std::cout << std::endl; - std::vector rigidbodyIDs = ComponentManager::GetInstance().GetAllComponentIDs(); //Get all the IDs that have a Rigidbody component + /*std::vector rigidbodyIDs = ComponentManager::GetInstance().GetAllComponentIDs(); //Get all the IDs that have a Rigidbody component for(std::uint32_t ID : rigidbodyIDs) { std::cout << "Rigidbody ID: " << ID << std::endl; } @@ -35,5 +35,5 @@ int main() { for(Rigidbody* rigidbody : rigidbodyComponents) { std::cout << "rigidbody: " << rigidbody->mMass << " " << rigidbody->mGravityScale << " " << rigidbody->mBodyType << std::endl; } - std::cout << std::endl; + std::cout << std::endl;*/ } -- cgit v1.2.3