diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-16 15:26:54 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-16 15:26:54 +0100 |
commit | 6b3feac981ce78144fb69e490640a4b07dd29f1d (patch) | |
tree | 88c9a7b1b03ada97a8152591deb035681f99b7c8 /mwe/ecs-homemade | |
parent | 8209678e20605936b2ce58331c1a65d8f23fee91 (diff) | |
parent | 9f6475e7b0698c414138e2a8140b47f01ce9c5d1 (diff) |
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/events
Diffstat (limited to 'mwe/ecs-homemade')
-rw-r--r-- | mwe/ecs-homemade/inc/ComponentManager.h | 6 | ||||
-rw-r--r-- | mwe/ecs-homemade/inc/ComponentManager.hpp | 21 | ||||
-rw-r--r-- | mwe/ecs-homemade/inc/GameObjectMax.hpp | 3 | ||||
-rw-r--r-- | mwe/ecs-homemade/src/ComponentManager.cpp | 3 | ||||
-rw-r--r-- | mwe/ecs-homemade/src/GameObjectMax.cpp | 3 | ||||
-rw-r--r-- | mwe/ecs-homemade/src/main.cpp | 11 |
6 files changed, 17 insertions, 30 deletions
diff --git a/mwe/ecs-homemade/inc/ComponentManager.h b/mwe/ecs-homemade/inc/ComponentManager.h index 0ba358e..99c108f 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.h +++ b/mwe/ecs-homemade/inc/ComponentManager.h @@ -33,8 +33,7 @@ public: //Get a vector<> of all components at specific type and id template <typename T> - std::vector<std::reference_wrapper<T>> - GetComponentsByID(std::uint32_t id) const; + std::vector<std::reference_wrapper<T>> GetComponentsByID(std::uint32_t id) const; //Get a vector<> of all components of a specific type template <typename T> std::vector<std::reference_wrapper<T>> GetComponentsByType() const; @@ -50,8 +49,7 @@ private: * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component. * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id). */ - std::unordered_map<std::type_index, - std::vector<std::vector<std::unique_ptr<Component>>>> + std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> mComponents; }; diff --git a/mwe/ecs-homemade/inc/ComponentManager.hpp b/mwe/ecs-homemade/inc/ComponentManager.hpp index 92db1d4..af9c3a1 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.hpp +++ b/mwe/ecs-homemade/inc/ComponentManager.hpp @@ -7,8 +7,7 @@ T & ComponentManager::AddComponent(std::uint32_t id, Args &&... args) { //Check if this component type is already in the unordered_map<> if (mComponents.find(type) == mComponents.end()) { //If not, create a new (empty) vector<> of vector<unique_ptr<Component>> - mComponents[type] - = std::vector<std::vector<std::unique_ptr<Component>>>(); + mComponents[type] = std::vector<std::vector<std::unique_ptr<Component>>>(); } //Resize the vector<> if the id is greater than the current size @@ -18,8 +17,7 @@ T & ComponentManager::AddComponent(std::uint32_t id, Args &&... args) { } //Create a new component of type T using perfect forwarding and store its unique_ptr in the vector<> - mComponents[type][id].push_back( - std::make_unique<T>(std::forward<Args>(args)...)); + mComponents[type][id].push_back(std::make_unique<T>(std::forward<Args>(args)...)); return static_cast<T &>(*mComponents[type][id].back().get()); } @@ -68,15 +66,13 @@ ComponentManager::GetComponentsByID(std::uint32_t id) const { if (mComponents.find(type) != mComponents.end()) { //Get the correct vector<> - const std::vector<std::vector<std::unique_ptr<Component>>> & - componentArray + const std::vector<std::vector<std::unique_ptr<Component>>> & componentArray = mComponents.at(type); //Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id < componentArray.size()) { //Loop trough the whole vector<> - for (const std::unique_ptr<Component> & componentPtr : - componentArray[id]) { + for (const std::unique_ptr<Component> & componentPtr : componentArray[id]) { //Cast the unique_ptr to a raw pointer T * castedComponent = static_cast<T *>(componentPtr.get()); @@ -94,8 +90,7 @@ ComponentManager::GetComponentsByID(std::uint32_t id) const { } template <typename T> -std::vector<std::reference_wrapper<T>> -ComponentManager::GetComponentsByType() const { +std::vector<std::reference_wrapper<T>> ComponentManager::GetComponentsByType() const { //Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); @@ -107,13 +102,11 @@ ComponentManager::GetComponentsByType() const { if (mComponents.find(type) != mComponents.end()) { //Get the correct vector<> - const std::vector<std::vector<std::unique_ptr<Component>>> & - componentArray + const std::vector<std::vector<std::unique_ptr<Component>>> & componentArray = mComponents.at(type); //Loop through the whole vector<> - for (const std::vector<std::unique_ptr<Component>> & component : - componentArray) { + for (const std::vector<std::unique_ptr<Component>> & component : componentArray) { //Loop trough the whole vector<> for (const std::unique_ptr<Component> & componentPtr : component) { //Cast the unique_ptr to a raw pointer diff --git a/mwe/ecs-homemade/inc/GameObjectMax.hpp b/mwe/ecs-homemade/inc/GameObjectMax.hpp index 2f433bb..92375bb 100644 --- a/mwe/ecs-homemade/inc/GameObjectMax.hpp +++ b/mwe/ecs-homemade/inc/GameObjectMax.hpp @@ -2,6 +2,5 @@ template <typename T, typename... Args> T & GameObject::AddComponent(Args &&... args) { - return ComponentManager::GetInstance().AddComponent<T>( - mId, std::forward<Args>(args)...); + return ComponentManager::GetInstance().AddComponent<T>(mId, std::forward<Args>(args)...); } diff --git a/mwe/ecs-homemade/src/ComponentManager.cpp b/mwe/ecs-homemade/src/ComponentManager.cpp index 536c152..33ba12e 100644 --- a/mwe/ecs-homemade/src/ComponentManager.cpp +++ b/mwe/ecs-homemade/src/ComponentManager.cpp @@ -12,8 +12,7 @@ void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) { if (id < componentArray .size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> - componentArray[id] - .clear(); //Clear the components at this specific id + componentArray[id].clear(); //Clear the components at this specific id } } } diff --git a/mwe/ecs-homemade/src/GameObjectMax.cpp b/mwe/ecs-homemade/src/GameObjectMax.cpp index 753c8e2..0516f68 100644 --- a/mwe/ecs-homemade/src/GameObjectMax.cpp +++ b/mwe/ecs-homemade/src/GameObjectMax.cpp @@ -2,8 +2,7 @@ #include "ComponentManager.h" -GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, - int layer) +GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) : mId(id), mName(name), mTag(tag), diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index 70c5d2c..85ab1f1 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -53,8 +53,7 @@ int main() { } std::vector<std::reference_wrapper<BehaviourScript>> scripts - = ComponentManager::GetInstance() - .GetComponentsByType<BehaviourScript>(); + = ComponentManager::GetInstance().GetComponentsByType<BehaviourScript>(); for (BehaviourScript & script : scripts) { //script.onStart(); //script.onUpdate(); @@ -66,10 +65,10 @@ int main() { delete gameObject[i]; } - auto Addtime = std::chrono::duration_cast<std::chrono::microseconds>( - stopAdding - startAdding); - auto LoopTime = std::chrono::duration_cast<std::chrono::microseconds>( - stopLooping - stopAdding); + auto Addtime + = std::chrono::duration_cast<std::chrono::microseconds>(stopAdding - startAdding); + auto LoopTime + = std::chrono::duration_cast<std::chrono::microseconds>(stopLooping - stopAdding); std::cout << "AddTime: " << Addtime.count() << " us" << std::endl; std::cout << "LoopTime: " << LoopTime.count() << " us" << std::endl; } |