26 if (_components.find(entity->getId()) == _components.end()) {
27 Log.
Error(
"No entity %ld", entity->getId());
31 std::map<std::type_index, Ptr<Component>>& compMap = _components[entity->getId()];
32 if (compMap.find(
typeid(T)) == compMap.end()) {
33 Log.
Error(
"No component of type %s in entity %ld",
typeid(T).name(), entity->getId());
38 return std::dynamic_pointer_cast<T>(component);
43 addInternalComponent<T>(entity->getId(), component);
46 template <
class T,
class... Args>
48 addInternalComponent<T>(entity->getId(), Make<T>(args...));
52 void Engine::addInternalComponent(
const unsigned int entityId,
const Ptr<Component>& component) {
54 const auto& type =
typeid(T);
56 if (_components.find(entityId) == _components.end()) {
57 Log.
Error(
"No entity %ld", entityId);
61 std::map<std::type_index, Ptr<Component>>& compMap = _components[entityId];
64 if (compMap.find(type) != compMap.end()) {
65 if (_onComponentRemoved.find(type) != _onComponentRemoved.end()) {
66 _onComponentRemoved[type](compMap[type], _entities[entityId]);
70 compMap[type] = component;
71 _bits[entityId].set(ComponentBits::get<T>());
73 if (_onComponentAdded.find(type) != _onComponentAdded.end()) {
74 auto& func = _onComponentAdded[type];
75 func(component, _entities[entityId]);
81 const auto& type =
typeid(T);
84 _systems[type]->_engine =
this;
87 template <
class T,
class... Args>
89 const auto& type =
typeid(T);
91 _systems[type] = Make<T>(args...);
92 _systems[type]->_engine =
this;
97 _onComponentAdded[
typeid(T)] = callback;
102 _onComponentRemoved[
typeid(T)] = callback;
void createSystem(Args...args)
Create a system into the engine.
void registerComponentAdded(std::function< void(const Ptr< Component > &, const Ptr< Entity > &)> callback)
Register a callback for a certain component type when it's added.
void addComponent(const Ptr< Entity > &entity, const Ptr< Component > &component)
Add a component for an entity.
void Error(const std::string fmt,...)
Write an error message.
static hx3d::LogImpl Log
Current log implementation.
void createComponent(const Ptr< Entity > &entity, Args...args)
Create a component for an entity with variable args.
void registerComponentRemoved(std::function< void(const Ptr< Component > &, const Ptr< Entity > &)> callback)
Register a callback for a certain component type when it's removed.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Ptr< T > getComponent(const Ptr< Entity > &entity)
Get the component for an entity.
void addSystem(const Ptr< System > &sys)
Add a system to the engine.