1 #ifndef ENTT_ENTITY_PROTOTYPE_HPP 2 #define ENTT_ENTITY_PROTOTYPE_HPP 9 #include <unordered_map> 10 #include "../config/config.h" 11 #include "registry.hpp" 38 template<
typename Entity>
43 template<
typename Component>
44 struct Wrapper { Component component; };
52 if(registry->valid(entity)) {
53 registry->destroy(entity);
70 : registry{®istry},
94 : handlers{std::move(other.handlers)},
95 registry{other.registry},
116 auto tmp{std::move(other)};
117 handlers.swap(tmp.handlers);
118 std::swap(registry, tmp.registry);
119 std::swap(entity, tmp.entity);
132 template<
typename Component,
typename... Args>
133 Component &
set(Args &&... args) {
135 const auto &wrapper = prototype.registry->template get<Wrapper<Component>>(prototype.entity);
136 other.template accommodate<Component>(dst, wrapper.component);
140 if(!other.template has<Component>(dst)) {
141 const auto &wrapper = prototype.registry->template get<Wrapper<Component>>(prototype.entity);
142 other.template accommodate<Component>(dst, wrapper.component);
146 handlers[registry->template type<Component>()] = Handler{
accommodate, assign};
147 auto &wrapper = registry->template accommodate<Wrapper<Component>>(entity, Component{std::forward<Args>(args)...});
148 return wrapper.component;
155 template<
typename Component>
157 registry->template reset<Wrapper<Component>>(entity);
158 handlers.erase(registry->template type<Component>());
166 template<
typename... Component>
167 bool has() const ENTT_NOEXCEPT {
168 return registry->template has<Wrapper<Component>...>(entity);
183 template<
typename Component>
184 const Component &
get()
const ENTT_NOEXCEPT {
185 return registry->template get<Wrapper<Component>>(entity).component;
200 template<
typename Component>
201 inline Component &
get() ENTT_NOEXCEPT {
202 return const_cast<Component &
>(
const_cast<const Prototype *
>(
this)->get<Component>());
217 template<
typename... Component>
218 inline std::enable_if_t<(
sizeof...(Component) > 1), std::tuple<
const Component &...>>
219 get()
const ENTT_NOEXCEPT {
220 return std::tuple<
const Component &...>{get<Component>()...};
235 template<
typename... Component>
236 inline std::enable_if_t<(
sizeof...(Component) > 1), std::tuple<Component &...>>
237 get() ENTT_NOEXCEPT {
238 return std::tuple<Component &...>{get<Component>()...};
260 const auto entity = other.
create();
308 for(
auto &handler: handlers) {
309 handler.second.assign(*
this, other, dst);
357 for(
auto &handler: handlers) {
358 handler.second.accommodate(*
this, other, dst);
477 std::unordered_map<component_type, Handler> handlers;
496 #endif // ENTT_ENTITY_PROTOTYPE_HPP std::size_t size_type
Unsigned integer type.
Entity entity_type
Underlying entity identifier.
Fast and reliable entity-component system.
Prototype(Registry< Entity > ®istry)
Constructs a prototype that is bound to a given registry.
void assign(const entity_type dst) const
Assigns the components of a prototype to a given entity.
Prototype(Prototype &&other)
Move constructor.
void operator()(const entity_type dst) const ENTT_NOEXCEPT
Assigns the components of a prototype to an entity.
entity_type create() const
Creates a new entity using a given prototype.
void accommodate(registry_type &other, const entity_type dst) const
Assigns or replaces the components of a prototype for an entity.
void operator()(registry_type &other, const entity_type dst) const ENTT_NOEXCEPT
Assigns the components of a prototype to an entity.
~Prototype()
Releases all its resources.
void unset() ENTT_NOEXCEPT
Removes the given component from a prototype.
entity_type create()
Creates a new entity and returns it.
entity_type operator()(registry_type &other) const ENTT_NOEXCEPT
Creates a new entity using a given prototype.
Prototype & operator=(const Prototype &)=delete
Copying a prototype isn't allowed.
bool has() const ENTT_NOEXCEPT
Checks if a prototype owns all the given components.
typename component_family::family_type component_type
Unsigned integer type.
entity_type operator()() const ENTT_NOEXCEPT
Creates a new entity using a given prototype.
Prototype container for concepts.
void assign(registry_type &other, const entity_type dst) const
Assigns the components of a prototype to a given entity.
void accommodate(const entity_type dst) const
Assigns or replaces the components of a prototype for an entity.
Prototype & operator=(Prototype &&other)
Move assignment operator.
entity_type create(registry_type &other) const
Creates a new entity using a given prototype.