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" 39 template<
typename Entity>
44 template<
typename Component>
45 struct Wrapper { Component component; };
53 if(registry->valid(entity)) {
54 registry->destroy(entity);
71 : registry{®istry},
95 : handlers{std::move(other.handlers)},
96 registry{other.registry},
117 auto tmp{std::move(other)};
118 handlers.swap(tmp.handlers);
119 std::swap(registry, tmp.registry);
120 std::swap(entity, tmp.entity);
133 template<
typename Component,
typename... Args>
134 Component &
set(Args &&... args) {
136 const auto &wrapper = prototype.registry->template get<Wrapper<Component>>(prototype.entity);
137 other.template accommodate<Component>(dst, wrapper.component);
141 if(!other.template has<Component>(dst)) {
142 const auto &wrapper = prototype.registry->template get<Wrapper<Component>>(prototype.entity);
143 other.template assign<Component>(dst, wrapper.component);
147 handlers[registry->template type<Component>()] = Handler{
accommodate, assign};
148 auto &wrapper = registry->template accommodate<Wrapper<Component>>(entity, Component{std::forward<Args>(args)...});
149 return wrapper.component;
156 template<
typename Component>
158 registry->template reset<Wrapper<Component>>(entity);
159 handlers.erase(registry->template type<Component>());
167 template<
typename... Component>
168 bool has() const ENTT_NOEXCEPT {
169 return registry->template has<Wrapper<Component>...>(entity);
184 template<
typename Component>
185 const Component &
get()
const ENTT_NOEXCEPT {
186 return registry->template get<Wrapper<Component>>(entity).component;
201 template<
typename Component>
202 inline Component &
get() ENTT_NOEXCEPT {
203 return const_cast<Component &
>(
const_cast<const Prototype *
>(
this)->get<Component>());
218 template<
typename... Component>
219 inline std::enable_if_t<(
sizeof...(Component) > 1), std::tuple<
const Component &...>>
220 get()
const ENTT_NOEXCEPT {
221 return std::tuple<
const Component &...>{get<Component>()...};
236 template<
typename... Component>
237 inline std::enable_if_t<(
sizeof...(Component) > 1), std::tuple<Component &...>>
238 get() ENTT_NOEXCEPT {
239 return std::tuple<Component &...>{get<Component>()...};
261 const auto entity = other.
create();
309 for(
auto &handler: handlers) {
310 handler.second.assign(*
this, other, dst);
358 for(
auto &handler: handlers) {
359 handler.second.accommodate(*
this, other, dst);
478 std::unordered_map<component_type, Handler> handlers;
497 #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.
constexpr auto null
Null 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.