LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Components.h
1#pragma once
2
4
5#include "LkEngine/Asset/Asset.h"
6#include "LkEngine/Asset/MaterialAsset.h"
7
9
10#include "LkEngine/Renderer/CameraBase.h"
11#include "LkEngine/Renderer/Material.h"
12
13#include "LkEngine/Physics/2D/ContactListener2D.h"
14
15#include "SceneCamera.h"
16
17
18namespace LkEngine {
19
26 {
27 virtual ~IComponent() = default;
28
29 virtual std::string ToString() const = 0;
30 };
31
33 {
34 LUUID ID;
35
36 operator LUUID() { return ID; }
37 };
38
40 {
41 std::string Tag;
42
43 LTagComponent() = default;
44 LTagComponent(const LTagComponent& Other) = default;
45 LTagComponent(const std::string& tag)
46 : Tag(tag)
47 {
48 }
49
50 operator std::string&() { return Tag; }
51 operator const std::string&() const { return Tag; }
52 };
53
55 {
56 LUUID ParentHandle = 0;
57 std::vector<LUUID> Children;
58
59 LRelationshipComponent() = default;
60 LRelationshipComponent(const LUUID InParentHandle)
61 : ParentHandle(InParentHandle)
62 {
63 }
64 LRelationshipComponent(const LRelationshipComponent& Other) = default;
65 };
66
67 // TODO: Patch out the use of Rotation2D and just use the Rotation quaternion
69 {
70 public:
71 glm::vec3 Translation = { 0.0f, 0.0f, 0.0f };
72 glm::vec3 Scale = { 1.0f, 1.0f, 1.0f };
73
74 private:
75 glm::vec3 RotationEuler = { 0.0f, 0.0f, 0.0f };
76 glm::quat Rotation = { 1.0f, 0.0f, 0.0f, 0.0f };
77
78 public:
79 float Rotation2D = 0.0f;
80 bool bIsStatic = false;
81
82 LTransformComponent() = default;
83 LTransformComponent(const LTransformComponent& Other) = default;
84 LTransformComponent(const glm::vec3& Translation)
85 : Translation(Translation)
86 {
87 }
88
89 FORCEINLINE glm::vec3 GetTranslation() const { return Translation; }
90 FORCEINLINE glm::vec3 GetScale() const { return Scale; }
91
92 FORCEINLINE float GetRotation2D() const { return Rotation2D; }
93
94 FORCEINLINE glm::mat4 GetTransform() const
95 {
96 return glm::translate(glm::mat4(1.0f), Translation)
97 * glm::toMat4(Rotation)
98 * glm::scale(glm::mat4(1.0f), Scale);
99 }
100
101 FORCEINLINE glm::mat4 GetInvTransform() const
102 {
103 glm::mat4 inv_translation = glm::translate(glm::mat4(1.0f), -Translation);
104 glm::quat inv_rotation = glm::conjugate(Rotation);
105 glm::mat4 inv_scale = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f / Scale.x, 1.0f / Scale.y, 1.0f / Scale.z));
106 return inv_scale * glm::toMat4(inv_rotation) * inv_translation;
107 }
108
109 FORCEINLINE glm::quat GetRotation() const { return Rotation; }
110 FORCEINLINE glm::vec3 GetRotationEuler() const { return RotationEuler; }
111
112 FORCEINLINE void SetRotationEuler(const glm::vec3& euler)
113 {
114 RotationEuler = euler;
115 Rotation = glm::quat(RotationEuler);
116 }
117
118 FORCEINLINE void SetRotation(const glm::quat& InQuat)
119 {
120 Rotation = InQuat;
121
122 const glm::vec3 OriginalEulerer = RotationEuler;
123 RotationEuler = glm::eulerAngles(Rotation);
124
125 /* Attempt to avoid 180deg flips in the Euler angles when using SetRotation(quat). */
126 if ((fabs(RotationEuler.x - OriginalEulerer.x) == glm::pi<float>())
127 && (fabs(RotationEuler.z - OriginalEulerer.z) == glm::pi<float>()))
128 {
129 RotationEuler.x = OriginalEulerer.x;
130 RotationEuler.y = glm::pi<float>() - RotationEuler.y;
131 RotationEuler.z = OriginalEulerer.z;
132 }
133 }
134
135 FORCEINLINE bool IsStatic() const { return bIsStatic; }
136
137 virtual std::string ToString() const override
138 {
139 return LK_FMT_LIB::format("[TransformComponent] Translation={} Scale={} RotEuler={}", Translation, Scale, RotationEuler);
140 }
141 };
142
144 {
145 std::string FilePath;
146 glm::vec2 Size;
147 glm::vec4 Color;
148 bool Passthrough = false;
149
150 LSpriteComponent(const std::string& filepath,
151 const glm::vec2& size = { 100.0f, 100.0f },
152 const glm::vec4& color = { 1.0f, 1.0f, 1.0f, 1.0f })
153 : FilePath(filepath)
154 , Size(size)
155 , Color(color)
156 {
157 }
158
159 LSpriteComponent(const glm::vec2& size = { 0.0f, 0.0f },
160 const glm::vec4& color = { 1.0f, 1.0f, 1.0f, 1.0f })
161 : FilePath("")
162 , Size(size)
163 , Color(color)
164 {
165 }
166
167 const glm::vec2& GetSize() const { return Size; }
168 const glm::vec4& GetColor() const { return Color; }
169 float GetWidth() const { return Size.x; }
170 float GetHeight() const { return Size.y; }
171
172 void SetSize(const glm::vec2& size)
173 {
174 Size = size;
175 }
176
177 void SetSize(float x, float y) { Size = { x, y }; }
178 void SetPassthrough(bool passthrough) { Passthrough = passthrough; }
179 bool IsPassthrough() const { return Passthrough; }
180 };
181
183 {
184 LSceneCamera Camera;
185 ECameraProjection ProjectionType = ECameraProjection::Perspective;
186 bool bPrimary = true;
187
188 LCameraComponent() = default;
190 : Camera(Other.Camera)
191 , ProjectionType(Other.ProjectionType)
192 {
193 }
194
195 operator LSceneCamera&() { return Camera; }
196 operator const LSceneCamera&() const { return Camera; }
197
198 std::string ToString() const
199 {
200 return LK_FMT_LIB::format("[CameraComponent] Primary={} "
201 "CameraType={} ProjectionType={} Pitch={:.2f} Yaw={:.2f}",
202 (bPrimary ? "True" : "False"),
203 Enum::ToString(Camera.GetType()), Enum::ToString(ProjectionType),
204 Camera.GetPitch(), Camera.GetYaw()
205 );
206 }
207
208 };
209
211 enum class ERigidBodyType
212 {
213 None = -1,
214 Static, // 0
215 Dynamic, // 1
216 Kinematic // 2
217 };
218
220 {
221 enum class Type
222 {
223 None = -1,
224 Static, // 0
225 Dynamic, // 1
226 Kinematic // 2
227 };
228
229 Type BodyType;
230 bool FixedRotation = false;
231 float Mass = 1.0f;
232 float LinearDrag = 0.01f;
233 float AngularDrag = 0.05f;
234 float GravityScale = 1.0f;
235 bool IsBullet = false;
236 void* RuntimeBody = nullptr; // Assigned at component creation
237
239 : BodyType(Type::Dynamic)
240 {
241 }
242 LRigidBody2DComponent(Type type)
243 : BodyType(type)
244 {
245 }
246 LRigidBody2DComponent(const LRigidBody2DComponent& Other) = default;
247 };
248
250 {
251 glm::vec2 Offset = { 0.0f, 0.0f };
252 glm::vec2 Size = { 0.50f, 0.50f };
253
254 float Density = 1.0f;
255 float Friction = 1.0f;
256 bool IsSensor = false;
257 void* RuntimeBody = nullptr; // Assigned at component creation
258
259 LBoxCollider2DComponent() = default;
260 LBoxCollider2DComponent(const LBoxCollider2DComponent& Other) = default;
261 };
262
264 {
265 FAssetHandle Mesh;
266 uint32_t SubmeshIndex = 0;
268
270 std::vector<FAssetHandle> BoneEntityIDs{};
271
272 bool bVisible = true;
273
274 LMeshComponent() = default;
275 LMeshComponent(const LMeshComponent& Other)
276 : Mesh(Other.Mesh)
277 , SubmeshIndex(Other.SubmeshIndex)
278 , MaterialTable(TObjectPtr<LkEngine::LMaterialTable>::Create(Other.MaterialTable))
280 {
281 }
282 LMeshComponent(const FAssetHandle InMesh, const uint32_t InSubmeshIndex = 0)
283 : Mesh(InMesh)
284 , SubmeshIndex(InSubmeshIndex)
285 {
286 }
287 };
288
290 {
291 FAssetHandle StaticMesh;
293 bool bVisible = true;
294
295 LStaticMeshComponent() = default;
297 : StaticMesh(Other.StaticMesh)
298 , MaterialTable(TObjectPtr<LMaterialTable>::Create(Other.MaterialTable))
299 , bVisible(Other.bVisible)
300 {
301 }
302 LStaticMeshComponent(const FAssetHandle InStaticMesh)
303 : StaticMesh(InStaticMesh)
304 {
305 }
306 };
307
309 {
310#if 0
311 std::unique_ptr<b2World> World;
312 ContactListener2D ContactListener;
313 bool DebugDrawerAttached = false;
314
315 const glm::vec2 GetGravity() const
316 {
317 auto g = World->GetGravity();
318 return glm::vec2(g.x, g.y);
319 }
320
321 int GetBodyCount() const
322 {
323 LK_VERIFY(World);
324 return World->GetBodyCount(); }
325
326 int GetContactCount() const
327 {
328 LK_VERIFY(World);
329 return World->GetContactCount();
330 }
331
332 FORCEINLINE bool IsPaused() const
333 {
334 LK_VERIFY(World);
335 return World->IsLocked();
336 }
337
338 FORCEINLINE bool HasDebugDrawerAttached() const
339 {
340 return DebugDrawerAttached;
341 }
342#endif
343 };
344
346 {
347 LUUID SceneID{};
348 };
349
350 template<typename... TComponent>
352 {
353 };
354
366 >;
367
368}
369
Core header.
Mathematics used by the engine.
Definition MaterialAsset.h:76
Definition SceneCamera.h:12
Definition ObjectPtr.h:102
static TObjectPtr< T > Create(TArgs &&... Args)
Create new pointer instance of type T.
Definition Asset.h:11
ERigidBodyType
Definition Components.h:212
Definition Components.h:309
Definition Components.h:352
Definition Components.h:26
Definition Components.h:250
Definition Components.h:183
Definition Components.h:33
Definition Components.h:264
std::vector< FAssetHandle > BoneEntityIDs
Definition Components.h:270
Definition Components.h:55
Definition Components.h:220
Definition Components.h:346
Definition Components.h:144
Definition Components.h:290
Definition Components.h:40
Definition Components.h:69
Definition UUID.h:13