jubilant-funicular
ECS.cpp
1 #include "nta/ECS.h"
2 
3 namespace nta {
4  ECS::ECS(const ComponentRegistry& registry) : m_registry(registry) {
5  for (auto it = m_registry.cbegin(); it != m_registry.cend(); ++it) {
6  it->second.create_list(m_components);
7  }
8  }
10  Entity ret = m_entity_gen();
11  return ret;
12  }
13  void ECS::gen_entities(std::size_t num, Entity* ids) {
14  for (std::size_t i = 0; i < num; i++) ids[i] = gen_entity();
15  }
17  if (!does_entity_exist(id)) return false;
18  m_entity_gen.free(id);
19  for (auto it = m_registry.cbegin(); it != m_registry.cend(); ++it) {
20  it->second.get_component(m_components, id).map([&](Component& cmpn) {
21  m_component_info.remove(cmpn.get_id());
22  });
23  it->second.delete_entity(m_components, m_cmpn_gen, id);
24  }
25  return true;
26  }
27  bool ECS::delete_owner(ComponentID cmpn) {
28  return get_owner(cmpn).map_or<bool>([&](Entity owner) {
29  return delete_entity(owner);
30  }, false);
31  }
32  std::size_t ECS::num_components() const {
33  std::size_t num = 0;
34  for (auto it = m_registry.cbegin(); it != m_registry.cend(); ++it) {
35  num += it->second.num_components(m_components);
36  }
37  return num;
38  }
40  if (m_cmpn_gen.is_free(cmpn)) return false;
41 
42  m_cmpn_gen.free(cmpn);
43 
44  auto maybe_info = m_component_info[cmpn];
45  if (maybe_info.is_none()) {
46  assert(false && "This should never happen");
47  }
48  ComponentInfo info = maybe_info.unwrap();
49  m_component_info.remove(cmpn);
50 
51  return m_registry[info.type].map_or<bool>([&](ComponentRegistry::Record rec) {
52  return rec.delete_component(m_components, cmpn, info.owner);
53  }, false);
54  }
55  bool ECS::does_entity_exist(Entity entity) const {
56  return m_entity_gen.is_in_use(entity);
57  }
59  return m_component_info[cmpn].map<Entity>([](const ComponentInfo& info) { return info.owner; });
60  }
62  auto maybe_info = m_component_info[cmpn];
63  if (maybe_info.is_none()) return utils::make_none<Component&>();
64  auto info = maybe_info.unwrap();
65  return m_registry[info.type].map_or<utils::Option<Component&>>([&](ComponentRegistry::Record rec) {
66  return rec.get_component(m_components, info.owner);
67  }, utils::make_none<Component&>());
68  }
69  void ECS::clear() {
70  for (auto& record : m_registry) {
71  record.second.clear(m_components);
72  }
73  m_component_info.clear();
74  m_entity_gen.clear();
75  m_cmpn_gen.clear();
76  }
77 }
nta::ECS::num_components
std::size_t num_components() const
Returns the number of components of the given type.
Definition: ECS.h:179
nta::Component
Base class for components.
Definition: ECS.h:23
nta::ECS::m_component_info
utils::SlotMap< ComponentInfo > m_component_info
Definition: ECS.h:153
nta::ECS::delete_entity
bool delete_entity(Entity id)
Definition: ECS.cpp:16
nta::ECS::ComponentInfo
Info directly attached to a ComponentID.
Definition: ECS.h:138
nta::utils::Option
Definition: Option.h:17
nta::Component::get_id
const ComponentID get_id() const
Returns this Component's id.
Definition: ECS.h:32
nta::ComponentRegistry::Record
Definition: ECS.h:39
nta
Definition: Animation2D.h:6
nta::ECS::m_cmpn_gen
utils::IDFactory< ComponentID > m_cmpn_gen
Responsible for creating unique IDs for the Components.
Definition: ECS.h:157
nta::ECS::m_registry
const ComponentRegistry m_registry
Definition: ECS.h:161
nta::ECS::does_entity_exist
bool does_entity_exist(Entity entity) const
Returns true if the given Entity exists.
Definition: ECS.cpp:55
nta::ECS::get_component
utils::Option< T & > get_component(Entity entity) const
Returns the Component of the given type associated to the given Entity.
Definition: ECS.h:298
nta::ECS::gen_entity
Entity gen_entity()
Generates a new Entity, returning its ID.
Definition: ECS.cpp:9
nta::ECS::m_components
utils::TypeMap m_components
Definition: ECS.h:148
nta::ECS::delete_component
bool delete_component(ComponentID cmpn)
Definition: ECS.cpp:39
nta::ECS::clear
void clear()
Removes all entities and components from this system.
Definition: ECS.cpp:69
nta::utils::SlotMapKey<>
nta::ECS::gen_entities
void gen_entities(std::size_t num, Entity *ids)
Generates several entities, storing their IDs in ids.
Definition: ECS.cpp:13
nta::ECS::get_owner
utils::Option< Entity > get_owner(ComponentID cmpn) const
Returns the Entity associated to this Component.
Definition: ECS.cpp:58
nta::ECS::m_entity_gen
utils::IDFactory< Entity > m_entity_gen
Responsible for creating unique IDs for the Entities.
Definition: ECS.h:155