From b242f28862ac8e9c1137c3f8e3640011a1ddddfa Mon Sep 17 00:00:00 2001 From: Max-001 <80035972+Max-001@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:29:17 +0200 Subject: DeleteComponents<> method created --- mwe/ecs-homemade/inc/ComponentManager.hpp | 10 ++++++++-- mwe/ecs-homemade/src/ComponentManager.cpp | 2 +- mwe/ecs-homemade/src/main.cpp | 5 +++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mwe/ecs-homemade/inc/ComponentManager.hpp b/mwe/ecs-homemade/inc/ComponentManager.hpp index 18bd548..e6523ca 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.hpp +++ b/mwe/ecs-homemade/inc/ComponentManager.hpp @@ -16,12 +16,18 @@ void ComponentManager::AddComponent(std::uint32_t id, Args&&... args) { template void ComponentManager::DeleteComponentsById(std::uint32_t id) { + std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) + } template void ComponentManager::DeleteComponents() { + 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()) { //Find the type (in the unordered_map<>) + mComponents[type].clear(); //Clear the whole vector<> of this specific type + } } template @@ -30,7 +36,7 @@ std::vector> ComponentManager::GetComponentsByID(std:: std::vector> componentVector; //Create an empty vector<> - if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_list<>) + if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) const std::vector>>& componentArray = mComponents.at(type); //Get the correct vector<> @@ -55,7 +61,7 @@ std::vector, std::uint32_t>> ComponentManage std::vector, std::uint32_t>> componentVector; //Create an empty vector<> std::uint32_t id = 0; //Set the id to 0 (the id will also be stored in the returned vector<>) - if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_list<>) + if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) const std::vector>>& componentArray = mComponents.at(type); //Get the correct vector<> diff --git a/mwe/ecs-homemade/src/ComponentManager.cpp b/mwe/ecs-homemade/src/ComponentManager.cpp index cca84bc..def50be 100644 --- a/mwe/ecs-homemade/src/ComponentManager.cpp +++ b/mwe/ecs-homemade/src/ComponentManager.cpp @@ -13,5 +13,5 @@ void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) { } void ComponentManager::DeleteAllComponents() { - mComponents.clear(); + mComponents.clear(); //Clear the whole unordered_map<> } diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index df229a2..e1ca9e9 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -69,6 +69,9 @@ int main() { } std::cout << std::endl; + ComponentManager::GetInstance().DeleteComponents(); + gameObect5.AddComponent(); + std::cout << "Finding all rigidbodies of entity 3" << std::endl; std::vector> rigidbodyOfEntity3 = ComponentManager::GetInstance().GetComponentsByID(gameObect3.mId); for(Rigidbody& rigidbodyEntity3 : rigidbodyOfEntity3) { @@ -99,8 +102,6 @@ int main() { } std::cout << std::endl; - ComponentManager::GetInstance().DeleteAllComponents(); - std::cout << "Finding all rigidbodies of all entities for the second time (after changing mMass to -1)" << std::endl; std::vector, std::uint32_t>> rigidBodies2 = ComponentManager::GetInstance().GetComponentsByType(); for(auto& [rigidbody2, id2] : rigidBodies2) { -- cgit v1.2.3