1#ifndef OE_SCENE_COMPONENT_H
2#define OE_SCENE_COMPONENT_H
7#include <unordered_map>
57 virtual void onUpdate(
const std::chrono::nanoseconds,
const int8_t = 0)
73 template <
typename Entity,
typename ComponentType>
79 template <
typename Entity>
83 const std::string& getName()
const
96 std::string _name = {};
98 template <
typename E,
typename C>
105 template <
typename Entity,
typename ComponentType = EntityComponent<Entity>>
108 using ComponentContainer = std::vector<std::shared_ptr<ComponentType>>;
118 template <
typename T = ComponentType,
typename ...Args>
119 std::shared_ptr<T>
addNamed(
const std::string& name, Args&& ...args)
121 if (name.length() == 0)
123 throw std::invalid_argument(
"Component name is missing!");
126 if (_component_by_keys.count(name) != 0)
128 throw std::invalid_argument(
"Component " + name +
"already exists!");
131 std::shared_ptr<T> result = std::make_shared<T>(_entity, std::forward<Args>(args)...);
133 _components.push_back(result);
135 result->_name = name;
136 _component_by_keys[name] = result;
144 template <
typename T = ComponentType,
typename ...Args>
145 std::shared_ptr<T>
add(Args&& ...args)
147 std::shared_ptr<T> result = std::make_shared<T>(_entity, std::forward<Args>(args)...);
149 _components.push_back(result);
157 template <
typename T = ComponentType>
160 if (_component_by_keys.count(name))
162 return dynamic_cast<T*
>(_component_by_keys.at(name).get());
171 template <
typename T = ComponentType>
174 if (_component_by_keys.count(name))
176 return dynamic_cast<T*
>(_component_by_keys.at(name).get());
185 template <
typename T>
188 for (
auto it = _components.begin(); it != _components.end(); it++)
190 if (
auto result =
dynamic_cast<const T*
>(it->get()))
200 template <
typename T>
203 for (
auto it = _components.begin(); it != _components.end(); it++)
205 if (
auto result =
dynamic_cast<T*
>(it->get()))
218 if (!_component_by_keys.count(name))
221 std::shared_ptr<Component> component = _component_by_keys[name];
224 _component_by_keys.erase(name);
227 _components.erase(std::remove_if(
230 [component](
const auto& c) {return c == component;}
231 ), _components.end());
244 void update(
const std::chrono::nanoseconds delta,
const int8_t flags = 0)
const
246 for (
auto it = _components.begin(); it != _components.end(); it++)
248 (*it)->onUpdate(delta, flags);
255 typename ComponentContainer::iterator
begin() noexcept
257 return _components.begin();
263 typename ComponentContainer::iterator
end() noexcept
265 return _components.end();
271 const typename ComponentContainer::const_iterator
begin() const noexcept
273 return _components.begin();
279 const typename ComponentContainer::const_iterator
end() const noexcept
281 return _components.end();
287 const typename ComponentContainer::const_iterator
cbegin() const noexcept
289 return _components.begin();
295 const typename ComponentContainer::const_iterator
cend() const noexcept
297 return _components.end();
303 ComponentContainer _components;
304 std::unordered_map<std::string, std::shared_ptr<ComponentType>> _component_by_keys;
310 template <
typename Entity,
typename ComponentType>
338 template <
typename T = ComponentType,
typename ...Args>
341 return _components.
template add<T, Args...>(std::forward<Args>(args)...);
347 template <
typename T = ComponentType,
typename ...Args>
350 return _components.
template addNamed<T, Args...>(name, std::forward<Args>(args)...);
364 template <
typename T>
367 return _components.
template getComponentByType<T>();
373 template <
typename T>
376 return _components.
template getComponentByType<T>();
382 template <
typename T = ComponentType>
385 return _components.
template getComponentByName<T>(name);
391 template <
typename T = ComponentType>
394 return _components.
template getComponentByName<T>(name);
Component holder that can be bound to an entity.
Definition component.h:107
ComponentContainer::iterator end() noexcept
Definition component.h:263
ComponentContainer::iterator begin() noexcept
Definition component.h:255
const ComponentContainer::const_iterator cend() const noexcept
Definition component.h:295
std::shared_ptr< T > add(Args &&...args)
Add a new a component.
Definition component.h:145
const ComponentContainer::const_iterator end() const noexcept
Definition component.h:279
void remove(const std::string &name)
Remove a component.
Definition component.h:215
const T * getComponentByName(const std::string &name) const noexcept
Fetch a component when you know its name (const version)
Definition component.h:158
T * getComponentByName(const std::string &name) noexcept
Fetch a component when you know its name.
Definition component.h:172
void update(const std::chrono::nanoseconds delta, const int8_t flags=0) const
Update all components.
Definition component.h:244
std::shared_ptr< T > addNamed(const std::string &name, Args &&...args)
Add a new a component.
Definition component.h:119
const T * getComponentByType() const noexcept
Fetch a component by its type (const version)
Definition component.h:186
const ComponentContainer::const_iterator cbegin() const noexcept
Definition component.h:287
const ComponentContainer::const_iterator begin() const noexcept
Definition component.h:271
T * getComponentByType() noexcept
Fetch a component by its type.
Definition component.h:201
Scene component that can be attached to an entity.
Definition component.h:44
bool is_active
Toggle to check if the Component is active, actual meaning heavily depends of the component type.
Definition component.h:67
virtual void onUpdate(const std::chrono::nanoseconds, const int8_t=0)
Run actions on entity update.
Definition component.h:57
Component bound to a specific entity.
Definition component.h:81
Util class to add components handling to an entity.
Definition component.h:312
void removeComponent(const std::string &name)
Remove a component.
Definition component.h:356
T * getComponentByType() noexcept
Fetch a component by its type.
Definition component.h:374
const ComponentList< Entity, ComponentType > & getComponents() const noexcept
Get components bound to the entity.
Definition component.h:330
T * getComponentByName(const std::string &name) noexcept
Fetch a component when you know its name.
Definition component.h:383
ComponentList< Entity, ComponentType > & getComponents() noexcept
Get components bound to the entity.
Definition component.h:322
std::shared_ptr< T > addComponent(Args &&... args)
Add a new a component.
Definition component.h:339
std::shared_ptr< T > addNamedComponent(const std::string &name, Args &&... args)
Add a new a component.
Definition component.h:348
const T * getComponentByType() const noexcept
Fetch a component by its type.
Definition component.h:365
const T * getComponentByName(const std::string &name) const noexcept
Fetch a component when you know its name.
Definition component.h:392
Scene / node components (camera, lighting, ...)
Definition pipeline.h:9