Yet Another Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
entity.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "component.h"
4 
5 #include <functional>
6 #include <iostream>
7 #include <memory>
8 #include <vector>
9 
10 namespace yage
11 {
12 
17 typedef unsigned int Entity;
18 
25 {
26 public:
29  bool is_valid(Entity entity) const;
31  std::unique_ptr<BaseComponent> &&component);
32  template <typename T>
33  EntityManager &each(std::function<void(T &)> update);
34 
35 private:
36  Entity update_next_entity();
37 
38  Entity next_entity_ = 0;
39 
40  std::vector<ComponentGroup> component_group_;
41  std::vector<ComponentMask> component_masks_;
42  std::vector<Entity> deleted_;
43 };
44 
45 template <typename T>
46 EntityManager &EntityManager::each(std::function<void(T &)> update)
47 {
48  T c;
49  auto id = static_cast<BaseComponent *>(&c)->getGroup();
50  for (auto it = component_group_[id].begin();
51  it != component_group_[id].end(); ++it) {
52  auto iteration = it - component_group_[id].begin();
53  if (is_valid(iteration) && component_masks_[iteration][id]) {
54  update(*static_cast<T *>((*it).get()));
55  }
56  }
57 
58  return *this;
59 }
60 
61 } // namespace yage
Definition: component.h:18
Has to keep track of all the different entities and their current state.
Definition: entity.h:24
Entity create_entity()
Definition: entity.cpp:12
unsigned int Entity
The entity is currently just an unsigned integer, which may change to a class in the future...
Definition: entity.h:17
EntityManager & each(std::function< void(T &)> update)
Definition: entity.h:46
EntityManager & delete_entity(Entity entity)
Definition: entity.cpp:19
EntityManager & add_component(Entity entity, std::unique_ptr< BaseComponent > &&component)
Definition: entity.cpp:35
bool is_valid(Entity entity) const
Definition: entity.cpp:25