aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-02 12:35:01 +0200
committerMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-02 12:35:01 +0200
commite409986d9a21ca96ee0b491826eb0008ff6ab8e0 (patch)
tree89f04830a0e3727f53e15b4ebff70a95add40c30
parentaedbb8b82f13d8b390419e59cea098ea73295df5 (diff)
Changed componentsManager to allow mutliple componts of the same type for one entity (id)
-rw-r--r--mwe/ecs-homemade/inc/ComponentManager.h12
-rw-r--r--mwe/ecs-homemade/inc/ComponentManager.hpp47
-rw-r--r--mwe/ecs-homemade/inc/GameObjectMax.h4
-rw-r--r--mwe/ecs-homemade/inc/GameObjectMax.hpp6
-rw-r--r--mwe/ecs-homemade/src/main.cpp20
5 files changed, 51 insertions, 38 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)...);
}
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<Sprite>(&sprite0); //Add a sprite to entity0
- gameObect0.AddComponent<Rigidbody>(&rigidbody0); //Also add a rigidbody to entity0
+ //Sprite sprite0;
+ //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
- Rigidbody rigidbody1(4, 5, 6);
- gameObect1.AddComponent<Rigidbody>(&rigidbody1); //Only add a rigidbody to entity1
+ //Rigidbody rigidbody1(4, 5, 6);
+ gameObect1.AddComponent<Rigidbody>(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<Rigidbody>(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<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
+ /*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;
}
@@ -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;*/
}