55#include < unordered_map>
66
77namespace ecs {
8+ /* *
9+ * \brief Create an entity with a unique ID.
10+ */
811 template <typename ... Components>
912 Entity Registry::createEntity () {
1013 int id = this ->_generateID ();
1114 return this ->createEntity (id);
1215 }
1316
17+ /* *
18+ * \brief Create an entity with a specific ID.
19+ */
1420 template <typename ... Components>
1521 Entity Registry::createEntity (uint64_t id) {
1622 std::lock_guard<std::mutex> lock (this ->_mutex );
@@ -25,6 +31,9 @@ namespace ecs {
2531 return entity;
2632 }
2733
34+ /* *
35+ * \brief Get all entities with component.
36+ */
2837 template <typename Component>
2938 std::unordered_map<Entity, Component> Registry::getEntities () {
3039 std::lock_guard<std::mutex> lock (this ->_mutex );
@@ -35,34 +44,56 @@ namespace ecs {
3544 return castedMap;
3645 }
3746
47+ /* *
48+ * \brief Add a component to an entity.
49+ */
3850 template <typename Component>
3951 void Registry::addComponent (const Entity& entity) {
4052 std::lock_guard<std::mutex> lock (this ->_mutex );
4153 _components[typeid (Component)][entity] = Component ();
4254 }
4355
56+ /* *
57+ * \brief Add multiple components to an entity.
58+ */
4459 template <typename ... Components>
4560 void Registry::addComponents (const Entity& entity) {
4661 (addComponent<Components>(entity), ...);
4762 }
4863
64+ /* *
65+ * \brief Set a component to an entity.
66+ *
67+ * Set a component to an entity. If the entity already has a component of the same type, it will be replaced.
68+ * If the entity does not have a component of the same type, it will be added.
69+ */
4970 template <typename Component>
5071 void Registry::setComponent (const Entity& entity, Component component) {
5172 std::lock_guard<std::mutex> lock (this ->_mutex );
5273 _components[typeid (Component)][entity] = component;
5374 }
5475
76+ /* *
77+ * \brief Check if an entity has a component.
78+ */
5579 template <typename Component>
5680 bool Registry::contains (const Entity& entity) {
5781 std::lock_guard<std::mutex> lock (this ->_mutex );
5882 return _components[typeid (Component)].contains (entity);
5983 }
6084
85+ /* *
86+ * \brief Check if an entity has a component with a specific value.
87+ */
6188 template <typename Component>
6289 bool Registry::contains (const Entity& entity, const Component& component) {
6390 return this ->contains <Component>(entity) && getComponent<Component>(entity) == component;
6491 }
6592
93+ /* *
94+ * \brief Get a component from an entity.
95+ * \throws ComponentNotFound if the entity does not have the component.
96+ */
6697 template <typename Component>
6798 Component& Registry::getComponent (const Entity& entity) {
6899 if (!contains<Component>(entity)) {
0 commit comments