diff options
author | Max-001 <80035972+Max-001@users.noreply.github.com> | 2024-10-02 15:26:24 +0200 |
---|---|---|
committer | Max-001 <80035972+Max-001@users.noreply.github.com> | 2024-10-02 15:26:24 +0200 |
commit | 2bd37e7d98728d72ba44da18eefea91547a7885a (patch) | |
tree | 63ebde9c853c5f683e50ffc4dd8950b08dc48efa /mwe/ecs-homemade/src/main.cpp | |
parent | e409986d9a21ca96ee0b491826eb0008ff6ab8e0 (diff) |
Refactored componentManager (it now uses smart pointer, does not return raw pointers (it only returns references) and it is now possible to store multiple components of the same type for the same entity)
Diffstat (limited to 'mwe/ecs-homemade/src/main.cpp')
-rw-r--r-- | mwe/ecs-homemade/src/main.cpp | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index 73f710b..f72a044 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -14,6 +14,7 @@ int main() { //Rigidbody rigidbody0(1, 2, 3); gameObect0.AddComponent<Sprite>(); //Add a sprite to entity0 gameObect0.AddComponent<Rigidbody>(1, 2, 3); //Also add a rigidbody to entity0 + gameObect0.AddComponent<Rigidbody>(3, 2, 1); //Add a second rigidbody to entity0 //Rigidbody rigidbody1(4, 5, 6); gameObect1.AddComponent<Rigidbody>(4, 5, 6); //Only add a rigidbody to entity1 @@ -21,19 +22,14 @@ int main() { //The entities are now initialized //Now I will demonstrate some ways of retreiving/getting components - std::vector<std::reference_wrapper<Rigidbody>> rigidbodyOfEntity0 = ComponentManager::GetInstance().GetComponentsOfID<Rigidbody>(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<std::uint32_t> rigidbodyIDs = ComponentManager::GetInstance().GetAllComponentIDs<Rigidbody>(); //Get all the IDs that have a Rigidbody component - for(std::uint32_t ID : rigidbodyIDs) { - std::cout << "Rigidbody ID: " << ID << std::endl; + std::vector<std::reference_wrapper<Rigidbody>> rigidbodyOfEntity0 = ComponentManager::GetInstance().GetComponentsByID<Rigidbody>(gameObect0.mId); //Get the pointer to the Rigidbody component of entity 0 + for(Rigidbody& rigidbodyEntity0 : rigidbodyOfEntity0) { + std::cout << "rigidbodyOfEntity0: " << rigidbodyEntity0.mMass << " " << rigidbodyEntity0.mGravityScale << " " << rigidbodyEntity0.mBodyType << std::endl; + std::cout << std::endl; } - std::cout << std::endl; - std::vector<Rigidbody*> rigidbodyComponents = ComponentManager::GetInstance().GetAllComponentPointer<Rigidbody>(); //Get all the pointers to the Rigidbody component(s) - for(Rigidbody* rigidbody : rigidbodyComponents) { - std::cout << "rigidbody: " << rigidbody->mMass << " " << rigidbody->mGravityScale << " " << rigidbody->mBodyType << std::endl; + std::vector<std::pair<std::reference_wrapper<Rigidbody>, std::uint32_t>> rigidBodies = ComponentManager::GetInstance().GetComponentsByType<Rigidbody>(); + for(auto& [rigidbody, id] : rigidBodies) { + std::cout << "Rigidbody of id: " << id << ": " << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl; } - std::cout << std::endl;*/ } |