aboutsummaryrefslogtreecommitdiffstats
path: root/yage/entity/entity.h
diff options
context:
space:
mode:
Diffstat (limited to 'yage/entity/entity.h')
-rw-r--r--yage/entity/entity.h29
1 files changed, 25 insertions, 4 deletions
diff --git a/yage/entity/entity.h b/yage/entity/entity.h
index 2aa31a66..93bea8fb 100644
--- a/yage/entity/entity.h
+++ b/yage/entity/entity.h
@@ -1,9 +1,12 @@
#pragma once
-#include <vector>
-
#include "component.h"
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <vector>
+
namespace yage
{
@@ -24,17 +27,35 @@ public:
Entity create_entity();
EntityManager &delete_entity(Entity entity);
bool is_valid(Entity entity) const;
- EntityManager &add_component(Entity entity, BaseComponent *component);
+ EntityManager &add_component(Entity entity,
+ std::unique_ptr<BaseComponent> &&component);
+ template <typename T>
+ EntityManager &each(std::function<void(T &)> update);
private:
Entity update_next_entity();
Entity next_entity_ = 0;
-public:
std::vector<ComponentGroup> component_group_;
std::vector<ComponentMask> component_masks_;
std::vector<Entity> deleted_;
};
+template <typename T>
+EntityManager &EntityManager::each(std::function<void(T &)> update)
+{
+ T c;
+ auto id = static_cast<BaseComponent *>(&c)->getGroup();
+ for (auto it = component_group_[id].begin();
+ it != component_group_[id].end(); ++it) {
+ auto iteration = it - component_group_[id].begin();
+ if (is_valid(iteration) && component_masks_[iteration][id]) {
+ update(*static_cast<T *>((*it).get()));
+ }
+ }
+
+ return *this;
+}
+
} // namespace yage