aboutsummaryrefslogtreecommitdiffstats
path: root/yage/entity/entity.h
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-06-23 15:51:37 +0100
committerYann Herklotz <ymherklotz@gmail.com>2018-06-23 15:51:37 +0100
commit801299f3175fccb940550cba48903decd7882822 (patch)
treea065643549f7fa2103b065be16a862c5a34ef2e7 /yage/entity/entity.h
parent083fcf24c50753b6f5fa04c5358be783be64407d (diff)
downloadYAGE-801299f3175fccb940550cba48903decd7882822.tar.gz
YAGE-801299f3175fccb940550cba48903decd7882822.zip
Entity implementation with added 'each' function
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