EnTT  3.5.2
actor.hpp
1 #ifndef ENTT_ENTITY_ACTOR_HPP
2 #define ENTT_ENTITY_ACTOR_HPP
3 
4 
5 #include <utility>
6 #include <type_traits>
7 #include "../config/config.h"
8 #include "registry.hpp"
9 #include "entity.hpp"
10 #include "fwd.hpp"
11 
12 
13 namespace entt {
14 
15 
26 template<typename Entity>
27 struct [[deprecated("Consider using the handle class instead")]] basic_actor {
31  using entity_type = Entity;
32 
33  basic_actor() ENTT_NOEXCEPT
34  : entt{null}, reg{nullptr}
35  {}
36 
46  basic_actor(basic_actor &&other) ENTT_NOEXCEPT
47  : entt{other.entt}, reg{other.reg}
48  {
49  other.entt = null;
50  }
51 
56  explicit basic_actor(registry_type &ref)
57  : entt{ref.create()}, reg{&ref}
58  {}
59 
65  explicit basic_actor(entity_type entity, registry_type &ref) ENTT_NOEXCEPT
66  : entt{entity}, reg{&ref}
67  {
68  ENTT_ASSERT(ref.valid(entity));
69  }
70 
72  virtual ~basic_actor() {
73  if(*this) {
74  reg->destroy(entt);
75  }
76  }
77 
88  basic_actor & operator=(basic_actor &&other) ENTT_NOEXCEPT {
89  if(this != &other) {
90  auto tmp{std::move(other)};
91  std::swap(reg, tmp.reg);
92  std::swap(entt, tmp.entt);
93  }
94 
95  return *this;
96  }
97 
112  template<typename Component, typename... Args>
113  decltype(auto) assign(Args &&... args) {
114  return reg->template emplace_or_replace<Component>(entt, std::forward<Args>(args)...);
115  }
116 
121  template<typename Component>
122  void remove() {
123  reg->template remove<Component>(entt);
124  }
125 
131  template<typename... Component>
132  [[nodiscard]] bool has() const {
133  return reg->template has<Component...>(entt);
134  }
135 
141  template<typename... Component>
142  [[nodiscard]] decltype(auto) get() const {
143  return std::as_const(*reg).template get<Component...>(entt);
144  }
145 
147  template<typename... Component>
148  [[nodiscard]] decltype(auto) get() {
149  return reg->template get<Component...>(entt);
150  }
151 
157  template<typename... Component>
158  [[nodiscard]] auto try_get() const {
159  return std::as_const(*reg).template try_get<Component...>(entt);
160  }
161 
163  template<typename... Component>
164  [[nodiscard]] auto try_get() {
165  return reg->template try_get<Component...>(entt);
166  }
167 
172  [[nodiscard]] const registry_type & backend() const ENTT_NOEXCEPT {
173  return *reg;
174  }
175 
177  [[nodiscard]] registry_type & backend() ENTT_NOEXCEPT {
178  return const_cast<registry_type &>(std::as_const(*this).backend());
179  }
180 
185  [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT {
186  return entt;
187  }
188 
193  [[nodiscard]] explicit operator bool() const {
194  return reg && reg->valid(entt);
195  }
196 
197 private:
198  entity_type entt;
199  registry_type *reg;
200 };
201 
202 
203 }
204 
205 
206 #endif
entt::basic_actor::backend
const registry_type & backend() const noexcept
Returns a reference to the underlying registry.
Definition: actor.hpp:172
entt::basic_registry::create
entity_type create()
Creates a new entity and returns it.
Definition: registry.hpp:473
entt::basic_actor::entity_type
Entity entity_type
Underlying entity identifier.
Definition: actor.hpp:31
entt::get
constexpr get_t< Type... > get
Variable template for lists of observed components.
Definition: utility.hpp:40
entt::basic_handle
Non-owning handle to an entity.
Definition: handle.hpp:19
entt::basic_actor::has
bool has() const
Checks if an actor has the given components.
Definition: actor.hpp:132
entt::basic_actor::basic_actor
basic_actor(basic_actor &&other) noexcept
Move constructor.
Definition: actor.hpp:46
entt::basic_actor::~basic_actor
virtual ~basic_actor()
Default destructor.
Definition: actor.hpp:72
entt::basic_actor::basic_actor
basic_actor(registry_type &ref)
Constructs an actor from a given registry.
Definition: actor.hpp:56
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::basic_actor::backend
registry_type & backend() noexcept
Returns a reference to the underlying registry.
Definition: actor.hpp:177
entt::basic_actor::entity
entity_type entity() const noexcept
Returns the entity associated with an actor.
Definition: actor.hpp:185
entt::basic_actor::operator=
basic_actor & operator=(basic_actor &&other) noexcept
Move assignment operator.
Definition: actor.hpp:88
entt::basic_actor::basic_actor
basic_actor(entity_type entity, registry_type &ref) noexcept
Constructs an actor from a given entity.
Definition: actor.hpp:65
entt::basic_registry
Fast and reliable entity-component system.
Definition: registry.hpp:43
entt::basic_actor::try_get
auto try_get() const
Returns pointers to the given components for an actor.
Definition: actor.hpp:158
entt::basic_actor::remove
void remove()
Removes the given component from an actor.
Definition: actor.hpp:122
entt::entity
entity
Default entity identifier.
Definition: fwd.hpp:52
entt::basic_actor::try_get
auto try_get()
Returns pointers to the given components for an actor.
Definition: actor.hpp:164
entt::basic_actor
Dedicated to those who aren't confident with the entity-component-system architecture.
Definition: actor.hpp:27