Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Actor.h
Go to the documentation of this file.
1#pragma once
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 {
33 template <typename T, typename... Args>
35 {
36 };
37
38 template <typename T, typename First, typename... Rest>
40 : std::bool_constant<std::is_constructible_v<
41 T,
42 Scene *,
43 entt::entity &,
44 First,
45 std::shared_ptr<SceneNode>,
46 Rest...>>
47 {
48 };
49
50 template <typename T, typename... Args>
52 std::is_base_of_v<Components::Logic, T> &&
53 ( ConstructibleWithSceneEntityFirstNodeRest<T, Args...>::value ||
54 std::is_constructible_v<T, Scene *, entt::entity &, std::shared_ptr<SceneNode>, Args...> ||
55 std::is_constructible_v<T, Scene *, entt::entity &, Args..., std::shared_ptr<SceneNode>> ||
56 std::is_constructible_v<T, Scene *, std::shared_ptr<SceneNode>, Args...> ||
57 std::is_constructible_v<T, Scene *, entt::entity &, Args...> ||
58 std::is_constructible_v<T, Scene *, Args...> );
59 } // namespace detail
60
64 class Actor : public Entity
65 {
66 friend class Scene;
67
68 public:
69 explicit Actor( const SpawnContext & context );
70 ~Actor() override = default;
71
73
81 template <typename T, typename... Args>
82 requires detail::ConstructibleWithActorContext<T, Args...>
83 T * addComponent( Args &&... args )
84 {
85 if constexpr ( detail::ConstructibleWithSceneEntityFirstNodeRest<T, Args...>::value )
86 {
87 return addComponentWithSceneEntityFirstNodeRest<T>( std::forward<Args>( args )... );
88 }
89 else if constexpr ( std::is_constructible_v<
90 T,
91 Scene *,
92 entt::entity &,
93 std::shared_ptr<SceneNode>,
94 Args...> )
95 {
97 getOwningScene(), data_, getSceneNode(), std::forward<Args>( args )...
98 );
99 }
100 else if constexpr ( std::is_constructible_v<
101 T,
102 Scene *,
103 entt::entity &,
104 Args...,
105 std::shared_ptr<SceneNode>> )
106 {
108 getOwningScene(), data_, std::forward<Args>( args )..., getSceneNode()
109 );
110 }
111 else if constexpr ( std::is_constructible_v<T, Scene *, std::shared_ptr<SceneNode>, Args...> )
112 {
114 getOwningScene(), getSceneNode(), std::forward<Args>( args )...
115 );
116 }
117 else if constexpr ( std::is_constructible_v<T, Scene *, entt::entity &, Args...> )
118 {
119 return Entity::addComponent<T>( getOwningScene(), data_, std::forward<Args>( args )... );
120 }
121 else
122 {
123 return Entity::addComponent<T>( getOwningScene(), std::forward<Args>( args )... );
124 }
125 }
126
130 virtual void beginPlay();
131
136 void tick( double deltaTime ) override;
137
141 virtual void endPlay();
142
143 [[nodiscard]] virtual std::string getActorName() const;
144
149
154
159 glm::vec3 getActorLocation() const;
160
165 glm::vec3 getActorRotation() const;
166
171 glm::quat getActorRotationQuat() const;
172
177 glm::vec3 getActorScale() const;
178
183 void setActorLocation( glm::vec3 newLocation ) const;
188 void setActorRotation( glm::vec3 newRotation ) const;
189
196 void rotateActor( glm::vec3 deltaRotation ) const;
197
202 void setActorScale( glm::vec3 newScale ) const;
203
208 glm::vec3 getForwardVector() const;
209
214 glm::vec3 getRightVector() const;
215
220 glm::vec3 getUpVector() const;
221
226 glm::mat4 getWorldTransform() const;
227
231 virtual bool drawImGui() const;
232
233 protected:
234 std::optional<std::shared_ptr<SceneNode>> sceneNode;
235
236 public:
242
247 [[nodiscard]] EngineKern * getEngine() const;
248
253 [[nodiscard]] Core::PhysicsEngine * getPhysicsEngine() const;
254
259 std::shared_ptr<SceneNode> getSceneNode() const;
260
261 private:
262 template <typename T, typename First, typename... Rest>
263 T * addComponentWithSceneEntityFirstNodeRest( First && first, Rest &&... rest )
264 {
267 data_,
268 std::forward<First>( first ),
269 getSceneNode(),
270 std::forward<Rest>( rest )...
271 );
272 }
273
274 Scene * owningScene_ = nullptr;
275 std::weak_ptr<Components::Scene> rootComponent_;
276 bool hasEndedPlay_ = false;
277 };
278
279} // namespace Engine::Entities
A scene component has a transform.
The physics engine manages creating and destroying physics objects for the physics simulation.
friend class Scene
Definition Actor.h:66
Actor(const SpawnContext &context)
glm::vec3 getUpVector() const
Gets the up world vector.
void setActorLocation(glm::vec3 newLocation) const
Set the new world location for this actor.
EngineKern * getEngine() const
Gets the engine this actor belongs to.
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 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
Definition Actor.h:234
virtual bool drawImGui() const
Allows drawing custom Imgui panels for the editor.
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.
Core::PhysicsEngine * getPhysicsEngine() const
Gets the physics engine through this actor's owning engine.
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
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:263
std::weak_ptr< Components::Scene > rootComponent_
Definition Actor.h:275
T * addComponent(Args &&... args)
Adds a component using this actor's scene, ECS entity and scene node where supported.
Definition Actor.h:83
glm::quat getActorRotationQuat() const
Gets the actors world rotation.
virtual std::string getActorName() const
T * addComponent(Args &&... args)
Adds a component to the entity. Checks for uniqueness if the component defines 'static constexpr bool...
Definition Entity.h:47
entt::entity data_
Definition Entity.h:190
A scene is the overarching structure which can spawn, contain and destroy actors or entities.
Definition Scene.h:62
Manages IBL (Image-Based Lighting) resources for specular reflections.
Scene ownership data passed to actor and component constructors during spawning.