1#ifndef OE_SCENE_COMPONENT_H
2#define OE_SCENE_COMPONENT_H
7#include <unordered_map>
56 virtual void onUpdate(
const double,
const int8_t = 0)
72 template <
typename Entity,
typename ComponentType>
78 template <
typename Entity>
82 const std::string& getName()
const
95 std::string _name = {};
97 template <
typename E,
typename C>
104 template <
typename Entity,
typename ComponentType = EntityComponent<Entity>>
107 using ComponentContainer = std::vector<std::shared_ptr<ComponentType>>;
117 template <
typename T = ComponentType,
typename ...Args>
118 std::shared_ptr<T>
addNamed(
const std::string& name, Args&& ...args)
120 if (name.length() == 0)
122 throw std::invalid_argument(
"Component name is missing!");
125 if (_component_by_keys.count(name) != 0)
127 throw std::invalid_argument(
"Component " + name +
"already exists!");
130 std::shared_ptr<T> result = std::make_shared<T>(_entity, std::forward<Args>(args)...);
132 _components.push_back(result);
134 result->_name = name;
135 _component_by_keys[name] = result;
143 template <
typename T = ComponentType,
typename ...Args>
144 std::shared_ptr<T>
add(Args&& ...args)
146 std::shared_ptr<T> result = std::make_shared<T>(_entity, std::forward<Args>(args)...);
148 _components.push_back(result);
156 template <
typename T = ComponentType>
159 if (_component_by_keys.count(name))
161 return dynamic_cast<T*
>(_component_by_keys.at(name).get());
170 template <
typename T = ComponentType>
173 if (_component_by_keys.count(name))
175 return dynamic_cast<T*
>(_component_by_keys.at(name).get());
184 template <
typename T>
187 for (
auto it = _components.begin(); it != _components.end(); it++)
189 if (
auto result =
dynamic_cast<const T*
>(it->get()))
199 template <
typename T>
202 for (
auto it = _components.begin(); it != _components.end(); it++)
204 if (
auto result =
dynamic_cast<T*
>(it->get()))
217 if (!_component_by_keys.count(name))
220 std::shared_ptr<Component> component = _component_by_keys[name];
223 _component_by_keys.erase(name);
226 _components.erase(std::remove_if(
229 [component](
const auto& c) {return c == component;}
230 ), _components.end());
243 void update(
const double delta,
const int8_t flags = 0)
const
245 for (
auto it = _components.begin(); it != _components.end(); it++)
247 (*it)->onUpdate(delta, flags);
254 typename ComponentContainer::iterator
begin() noexcept
256 return _components.begin();
262 typename ComponentContainer::iterator
end() noexcept
264 return _components.end();
270 const typename ComponentContainer::const_iterator
begin() const noexcept
272 return _components.begin();
278 const typename ComponentContainer::const_iterator
end() const noexcept
280 return _components.end();
286 const typename ComponentContainer::const_iterator
cbegin() const noexcept
288 return _components.begin();
294 const typename ComponentContainer::const_iterator
cend() const noexcept
296 return _components.end();
302 ComponentContainer _components;
303 std::unordered_map<std::string, std::shared_ptr<ComponentType>> _component_by_keys;
309 template <
typename Entity,
typename ComponentType>
337 template <
typename T = ComponentType,
typename ...Args>
340 return _components.
template add<T, Args...>(std::forward<Args>(args)...);
346 template <
typename T = ComponentType,
typename ...Args>
349 return _components.
template addNamed<T, Args...>(name, std::forward<Args>(args)...);
363 template <
typename T>
366 return _components.
template getComponentByType<T>();
372 template <
typename T>
375 return _components.
template getComponentByType<T>();
381 template <
typename T = ComponentType>
384 return _components.
template getComponentByName<T>(name);
390 template <
typename T = ComponentType>
393 return _components.
template getComponentByName<T>(name);
Component holder that can be bound to an entity.
Definition component.h:106
ComponentContainer::iterator end() noexcept
Definition component.h:262
ComponentContainer::iterator begin() noexcept
Definition component.h:254
const ComponentContainer::const_iterator cend() const noexcept
Definition component.h:294
std::shared_ptr< T > add(Args &&...args)
Add a new a component.
Definition component.h:144
const ComponentContainer::const_iterator end() const noexcept
Definition component.h:278
void update(const double delta, const int8_t flags=0) const
Update all components.
Definition component.h:243
void remove(const std::string &name)
Remove a component.
Definition component.h:214
const T * getComponentByName(const std::string &name) const noexcept
Fetch a component when you know its name (const version)
Definition component.h:157
T * getComponentByName(const std::string &name) noexcept
Fetch a component when you know its name.
Definition component.h:171
std::shared_ptr< T > addNamed(const std::string &name, Args &&...args)
Add a new a component.
Definition component.h:118
const T * getComponentByType() const noexcept
Fetch a component by its type (const version)
Definition component.h:185
const ComponentContainer::const_iterator cbegin() const noexcept
Definition component.h:286
const ComponentContainer::const_iterator begin() const noexcept
Definition component.h:270
T * getComponentByType() noexcept
Fetch a component by its type.
Definition component.h:200
Scene component that can be attached to an entity.
Definition component.h:43
bool is_active
Toggle to check if the Component is active, actual meaning heavily depends of the component type.
Definition component.h:66
virtual void onUpdate(const double, const int8_t=0)
Run actions on entity update.
Definition component.h:56
Component bound to a specific entity.
Definition component.h:80
Util class to add components handling to an entity.
Definition component.h:311
void removeComponent(const std::string &name)
Remove a component.
Definition component.h:355
T * getComponentByType() noexcept
Fetch a component by its type.
Definition component.h:373
const ComponentList< Entity, ComponentType > & getComponents() const noexcept
Get components bound to the entity.
Definition component.h:329
T * getComponentByName(const std::string &name) noexcept
Fetch a component when you know its name.
Definition component.h:382
ComponentList< Entity, ComponentType > & getComponents() noexcept
Get components bound to the entity.
Definition component.h:321
std::shared_ptr< T > addComponent(Args &&... args)
Add a new a component.
Definition component.h:338
std::shared_ptr< T > addNamedComponent(const std::string &name, Args &&... args)
Add a new a component.
Definition component.h:347
const T * getComponentByType() const noexcept
Fetch a component by its type.
Definition component.h:364
const T * getComponentByName(const std::string &name) const noexcept
Fetch a component when you know its name.
Definition component.h:391
Scene / node components (camera, lighting, ...)
Definition pipeline.h:9