Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Actor.h
1#pragma once
2#include "Engine/Entity/Entity.h"
3#include "Engine/World/SceneGraph.h"
4
5#include <memory>
6#include <optional>
7#include <string>
8#include <type_traits>
9#include <utility>
10
11namespace Engine::Components
12{
13 class Scene;
14} // namespace Engine::Components
15
16namespace Engine
17{
18 class EngineKern;
19
20 namespace Core
21 {
22 class PhysicsEngine;
23 } // namespace Core
24} // namespace Engine
25
26namespace Engine::Entities
27{
28 class Scene;
29 struct SpawnContext;
30
31 namespace detail
32 {
36 template <typename T, typename... Args>
38 {
39 };
40
44 template <typename T, typename First, typename... Rest>
46 : std::bool_constant<std::is_constructible_v<
47 T,
48 Scene *,
49 entt::entity &,
50 First,
51 std::shared_ptr<SceneNode>,
52 Rest...>>
53 {
54 };
55
56 template <typename T, typename... Args>
58 std::is_base_of_v<Components::Logic, T> &&
59 ( ConstructibleWithSceneEntityFirstNodeRest<T, Args...>::value ||
60 std::is_constructible_v<T, Scene *, entt::entity &, std::shared_ptr<SceneNode>, Args...> ||
61 std::is_constructible_v<T, Scene *, entt::entity &, Args..., std::shared_ptr<SceneNode>> ||
62 std::is_constructible_v<T, Scene *, std::shared_ptr<SceneNode>, Args...> ||
63 std::is_constructible_v<T, Scene *, entt::entity &, Args...> ||
64 std::is_constructible_v<T, Scene *, Args...> );
65 } // namespace detail
66
70 class Actor : public Entity
71 {
72 friend class Scene;
73
74 public:
79 explicit Actor( const SpawnContext & context );
80
84 ~Actor() override = default;
85
87
95 template <typename T, typename... Args>
96 requires detail::ConstructibleWithActorContext<T, Args...>
97 T * addComponent( Args &&... args )
98 {
99 if constexpr ( detail::ConstructibleWithSceneEntityFirstNodeRest<T, Args...>::value )
100 {
101 return addComponentWithSceneEntityFirstNodeRest<T>( std::forward<Args>( args )... );
102 }
103 else if constexpr ( std::is_constructible_v<
104 T,
105 Scene *,
106 entt::entity &,
107 std::shared_ptr<SceneNode>,
108 Args...> )
109 {
111 getOwningScene(), data_, getSceneNode(), std::forward<Args>( args )...
112 );
113 }
114 else if constexpr ( std::is_constructible_v<
115 T,
116 Scene *,
117 entt::entity &,
118 Args...,
119 std::shared_ptr<SceneNode>> )
120 {
122 getOwningScene(), data_, std::forward<Args>( args )..., getSceneNode()
123 );
124 }
125 else if constexpr ( std::is_constructible_v<T, Scene *, std::shared_ptr<SceneNode>, Args...> )
126 {
128 getOwningScene(), getSceneNode(), std::forward<Args>( args )...
129 );
130 }
131 else if constexpr ( std::is_constructible_v<T, Scene *, entt::entity &, Args...> )
132 {
133 return Entity::addComponent<T>( getOwningScene(), data_, std::forward<Args>( args )... );
134 }
135 else
136 {
137 return Entity::addComponent<T>( getOwningScene(), std::forward<Args>( args )... );
138 }
139 }
140
144 virtual void beginPlay();
145
150 void tick( double deltaTime ) override;
151
155 virtual void endPlay();
156
160 [[nodiscard]] virtual std::string getActorName() const;
161
166
171
178 Actor& parent,
180 );
181
188 const std::shared_ptr<SceneNode>& parentNode,
190 );
191
197
202 glm::vec3 getActorLocation() const;
203
208 glm::vec3 getActorRotation() const;
209
214 glm::quat getActorRotationQuat() const;
215
220 glm::vec3 getActorScale() const;
221
226 void setActorLocation( glm::vec3 newLocation ) const;
231 void setActorRotation( glm::vec3 newRotation ) const;
232
239 void rotateActor( glm::vec3 deltaRotation ) const;
240
245 void setActorScale( glm::vec3 newScale ) const;
246
251 glm::vec3 getForwardVector() const;
252
257 glm::vec3 getRightVector() const;
258
263 glm::vec3 getUpVector() const;
264
269 glm::mat4 getWorldTransform() const;
270
274 virtual bool drawImGui() const;
275
276 protected:
280 void onEngineBeginPlay() override;
281
285 void onEngineEndPlay() override;
286
290 void onEngineDestroyed() override;
291
292 std::optional<std::shared_ptr<SceneNode>> sceneNode;
293
294 public:
300
305 [[nodiscard]] static EngineKern * getEngine();
306
311 std::shared_ptr<SceneNode> getSceneNode() const;
312
313 private:
314 template <typename T, typename First, typename... Rest>
315 T * addComponentWithSceneEntityFirstNodeRest( First && first, Rest &&... rest )
316 {
319 data_,
320 std::forward<First>( first ),
321 getSceneNode(),
322 std::forward<Rest>( rest )...
323 );
324 }
325
326 Scene * owningScene_ = nullptr;
327 std::weak_ptr<Components::Scene> rootComponent_;
328 bool hasEndedPlay_ = false;
329 };
330
331} // namespace Engine::Entities
A scene component has a transform.
Owns the Bullet dynamics world and dispatches engine collision events.
Owns the engine runtime and provides access to engine subsystems.
Definition Engine.h:67
friend class Scene
Definition Actor.h:72
Actor(const SpawnContext &context)
Creates an actor from a scene spawn context.
glm::vec3 getUpVector() const
Gets the up world vector.
void setActorLocation(glm::vec3 newLocation) const
Set the new world location for this actor.
virtual void endPlay()
Gets executed when an actor is getting removed from the scene.
glm::vec3 getActorRotation() const
Gets the actors rotation in the world.
void setActorRotation(glm::vec3 newRotation) const
Sets the world rotation.
virtual void beginPlay()
Gets executed when the actor is spawned into the world.
glm::mat4 getWorldTransform() const
Gets the model matrix.
void attachTo(Actor &parent, AttachmentTransformRule rule=AttachmentTransformRule::KeepWorld)
Attaches this actor to another actor.
void destroyActor()
Queues this actor for destruction after the current tick finishes.
void destroyActorImmediate()
Destroys this actor immediately. Prefer destroyActor() during gameplay/tick code.
void rotateActor(glm::vec3 deltaRotation) const
Applies an incremental rotation to the actor using quaternion multiplication. Use this instead of get...
std::optional< std::shared_ptr< SceneNode > > sceneNode
Scene graph node owned by this actor.
Definition Actor.h:292
void onEngineDestroyed() override
Handles actor cleanup when the entity is destroyed.
virtual bool drawImGui() const
Allows drawing custom Imgui panels for the editor.
static EngineKern * getEngine()
Gets the engine this actor belongs to.
void setActorScale(glm::vec3 newScale) const
sets the world scale for this actor
glm::vec3 getActorScale() const
Scales the actor to world scale.
Scene * getOwningScene() const
Get the pointer to the scene this entity is a part of.
glm::vec3 getRightVector() const
Gets the right.
glm::vec3 getActorLocation() const
Gets the actors location in the world.
void tick(double deltaTime) override
Executed every frame.
~Actor() override=default
Destroys the actor.
glm::vec3 getForwardVector() const
Gets the forward world vector.
std::shared_ptr< SceneNode > getSceneNode() const
Getter for the scene graph node for this object.
T * addComponentWithSceneEntityFirstNodeRest(First &&first, Rest &&... rest)
Definition Actor.h:315
void onEngineEndPlay() override
Bridges the entity end-play lifecycle to Actor::endPlay().
std::weak_ptr< Components::Scene > rootComponent_
Definition Actor.h:327
T * addComponent(Args &&... args)
Adds a component using this actor's scene, ECS entity and scene node where supported.
Definition Actor.h:97
void onEngineBeginPlay() override
Bridges the entity begin-play lifecycle to Actor::beginPlay().
glm::quat getActorRotationQuat() const
Gets the actors world rotation.
void detach(AttachmentTransformRule rule=AttachmentTransformRule::KeepWorld)
Detaches this actor from its parent node.
virtual std::string getActorName() const
Returns the display name for this actor.
void attachTo(const std::shared_ptr< SceneNode > &parentNode, AttachmentTransformRule rule=AttachmentTransformRule::KeepWorld)
Attaches this actor to a scene node.
T * addComponent(Args &&... args)
Adds a component to the entity. Checks for uniqueness if the component defines 'static constexpr bool...
Definition Entity.h:88
Entity()
Creates an entity in the constructed lifecycle state.
entt::entity data_
ECS entity handle associated with this entity.
Definition Entity.h:217
Owns scene entities, actor lifetime, and the scene graph root.
Definition Scene.h:84
AttachmentTransformRule
Controls which transform space is preserved when attaching or reparenting nodes.
Definition SceneGraph.h:18
@ KeepWorld
Preserve the node's world transform while recomputing its local transform.
Definition SceneGraph.h:19
Scene ownership data passed to actor and component constructors during spawning.
Detects component constructors that accept scene, entity, first user argument, node,...
Definition Actor.h:38