aboutsummaryrefslogtreecommitdiff
path: root/mwe/ecs-homemade/inc/ComponentManager.h
diff options
context:
space:
mode:
authorMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-02 15:26:24 +0200
committerMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-02 15:26:24 +0200
commit2bd37e7d98728d72ba44da18eefea91547a7885a (patch)
tree63ebde9c853c5f683e50ffc4dd8950b08dc48efa /mwe/ecs-homemade/inc/ComponentManager.h
parente409986d9a21ca96ee0b491826eb0008ff6ab8e0 (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/inc/ComponentManager.h')
-rw-r--r--mwe/ecs-homemade/inc/ComponentManager.h33
1 files changed, 20 insertions, 13 deletions
diff --git a/mwe/ecs-homemade/inc/ComponentManager.h b/mwe/ecs-homemade/inc/ComponentManager.h
index 5b0629f..763c28e 100644
--- a/mwe/ecs-homemade/inc/ComponentManager.h
+++ b/mwe/ecs-homemade/inc/ComponentManager.h
@@ -6,31 +6,38 @@
#include <vector>
#include <typeindex>
#include <memory>
-#include <any>
+#include <utility>
class ComponentManager {
public:
- static ComponentManager& GetInstance();
+ static ComponentManager& GetInstance(); //Singleton
- ComponentManager(const ComponentManager&) = delete;
- ComponentManager(ComponentManager&&) = delete;
- ComponentManager& operator=(const ComponentManager&) = delete;
- ComponentManager& operator=(ComponentManager&&) = delete;
+ ComponentManager(const ComponentManager&) = delete; //Singleton
+ ComponentManager(ComponentManager&&) = delete; //Singleton
+ ComponentManager& operator=(const ComponentManager&) = delete; //Singleton
+ ComponentManager& operator=(ComponentManager&&) = delete; //Singleton
template <typename T, typename... Args>
- void AddComponent(std::uint32_t id, Args&&... args);
+ void AddComponent(std::uint32_t id, Args&&... args); //Add a component
+ //TODO: void DeleteAllComponentsOfId(std::uint32_t id); //Deletes all components of a specific id
+ //TODO: void DeleteAllComponents(); //Deletes all components
+
template <typename T>
- std::vector<std::reference_wrapper<T>> GetComponentsOfID(std::uint32_t id);
- /*template <typename T>
- std::vector<std::uint32_t> GetAllComponentIDs();
+ std::vector<std::reference_wrapper<T>> GetComponentsByID(std::uint32_t id) const; //Get a vector<> of all components at specific id
template <typename T>
- std::vector<T*> GetAllComponentPointer();*/
+ std::vector<std::pair<std::reference_wrapper<T>, std::uint32_t>> GetComponentsByType() const; //Get a vector<> of all components of a specific type
private:
- static ComponentManager mInstance;
+ static ComponentManager mInstance; //Singleton
- ComponentManager();
+ ComponentManager(); //Singleton
+ /*
+ * The std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> below might seem a bit strange, let me explain this structure:
+ * The std::unordered_map<> has a key and value. The key is a std::type_index and the value is a std::vector. So, a new std::vector will be created for each new std::type_index.
+ * 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>>>> mComponents;
};