LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Scene.h
1#pragma once
2
3#include <unordered_set>
4
8#include "LkEngine/Core/LObject/ObjectPtr.h"
9#include "LkEngine/Core/Delegate/Delegate.h"
10
11#include "LkEngine/Asset/Asset.h"
12
13#include "Components.h"
14
15#include "LkEngine/Editor/EditorCamera.h"
16
17#include <entt/entt.hpp>
18
19namespace LkEngine {
20
21 class LEntity;
22 class World;
23 class LRenderer;
24 class LSceneRenderer;
25 struct Physics2DSpecification;
26
27 LK_DECLARE_MULTICAST_DELEGATE(FOnSceneSetActive, const TObjectPtr<LScene>&);
28 LK_DECLARE_MULTICAST_DELEGATE(FOnSceneCreated, const TObjectPtr<LScene>&);
29 extern FOnSceneSetActive GOnSceneSetActive;
30 extern FOnSceneCreated GOnSceneCreated;
31
32 enum class ESceneType
33 {
34 Normal = 0,
35 Editor,
36 };
37
38 enum class ESceneState
39 {
40 Edit = 0,
41 Play,
42 Pause,
43 Simulate
44 };
45
46 class LScene : public LAsset
47 {
48 public:
49 LScene(const std::string& SceneName, const bool IsEditorScene = false);
50 LScene() = delete;
51 ~LScene() = default;
52
53 void OnRenderRuntime(TObjectPtr<LSceneRenderer> SceneRenderer, const float DeltaTime);
54 void OnUpdateRuntime(const float InDeltaTime);
55 void OnRenderEditor(TObjectPtr<LSceneRenderer> SceneRenderer, LEditorCamera& EditorCamera, const float DeltaTime);
56 void OnUpdateEditor(const float InDeltaTime);
57
58 void EndScene();
59
60 FORCEINLINE LUUID GetUUID() const { return SceneID; }
61 LEntity GetMainCameraEntity();
62
63 template<typename T = LEntity>
64 std::vector<T> GetEntities();
65
66 uint64_t GetEntityCount() const { return static_cast<uint64_t>(EntityMap.size()); }
67 void SortEntities();
68
69 LEntity FindEntity(std::string_view EntityName);
70 LEntity GetEntityWithUUID(const LUUID UUID);
71
72 FORCEINLINE entt::registry& GetRegistry() { return Registry; }
73 void DestroyEntity(const LEntity Entity);
74 bool HasEntity(const LEntity Entity) const;
75 bool IsEntityInRegistry(const LEntity Entity) const;
76
77 LEntity CreateEntity(const std::string& InName = "");
78 LEntity CreateEntityWithID(const LUUID UUID, const std::string& InName = "");
79 LEntity CreateChildEntity(LEntity Parent, const std::string& InName = "");
80 LEntity TryGetEntityWithUUID(const LUUID ID);
81
82 void ParentEntity(LEntity Entity, LEntity Parent);
83 void UnparentEntity(LEntity Entity, bool bConvertToWorldSpace = true);
84
85 glm::mat4 GetWorldSpaceTransform(LEntity Entity);
86
87 std::string GetName() const { return Name; }
88 void SetName(const std::string& InName) { Name = InName; }
89
90 void SetActive(const bool Active);
91 FORCEINLINE bool IsActive() const { return (this == ActiveScene.Get()); }
92
93 void Clear();
94 void Pause(const bool IsPaused);
95
96 void CopyTo(TObjectPtr<LScene>& TargetScene);
97
98 template<typename T>
99 void OnComponentAdded(LEntity Entity, T& TComponent);
100
101 template<typename T>
102 static void CopyComponent(entt::registry& DestinationRegistry,
103 entt::registry& SourceRegistry,
104 const std::unordered_map<LUUID, entt::entity>& enttMap)
105 {
106 auto SourceEntities = SourceRegistry.view<T>();
107 for (auto SourceEntity : SourceEntities)
108 {
109 entt::entity DestEntity = enttMap.at(SourceRegistry.get<LIDComponent>(SourceEntity).ID);
110 auto& SourceComp = SourceRegistry.get<T>(SourceEntity);
111 auto& DestComp = DestinationRegistry.emplace_or_replace<T>(DestEntity, SourceComp);
112 }
113 }
114
115 template<typename TComponent>
116 void CopyComponentIfExists(entt::entity Destination, entt::registry& DestinationRegistry, entt::entity Source)
117 {
118 if (Registry.all_of<TComponent>(Source))
119 {
120 auto& SourceComp = Registry.get<TComponent>(Source);
121 DestinationRegistry.emplace_or_replace<TComponent>(Destination, SourceComp);
122 }
123 }
124
126 #if defined(LK_ENGINE_MSVC)
127 template<typename TComponent>
128 static void CopyComponentFromScene(LEntity Destination,
129 TObjectPtr<LScene> DestinationScene,
130 LEntity Source,
131 TObjectPtr<LScene> SourceScene)
132 {
133 SourceScene->CopyComponentIfExists<TComponent>((entt::entity)Destination, DestinationScene->Registry, (entt::entity)Source);
134 }
135 #endif
136
137 template<typename ...Components>
139 {
140 return Registry.view<Components...>();
141 }
142
143 std::unordered_set<LUUID> GetAssetList();
144
145 static TObjectPtr<LScene> GetActiveScene() { return ActiveScene; }
146 static uint8_t GetSceneCount() { return SceneCounter; }
147
148 public:
149 static constexpr const char* FILE_EXTENSION = "lkscene";
150 private:
151 inline static TObjectPtr<LScene> ActiveScene = nullptr; /* TO REMOVE */
152 inline static uint8_t SceneCounter = 0;
153 private:
154 LUUID SceneID = 0;
155 std::string Name = "";
156 entt::entity SceneEntity;
157
158 bool bPaused = false;
159 bool bEditorScene = false;
160
161 entt::registry Registry{};
162 //std::unordered_map<LUUID, LEntity> EntityMap{};
163 std::unordered_map<LUUID, entt::entity> EntityMap{};
164
165 uint16_t ViewportWidth = 0;
166 uint16_t ViewportHeight = 0;
167 int Frames = 0;
168
169 TObjectPtr<LSceneRenderer> Renderer;
170
171 friend class LEntity;
172 friend class LEditorLayer;
173 friend class LSceneSerializer;
174 friend class LSceneManagerPanel;
175
176 LCLASS(LScene);
177 };
178
179 namespace Enum
180 {
181 static constexpr const char* ToString(const ESceneState SceneState)
182 {
183 switch (SceneState)
184 {
185 case ESceneState::Edit: return "Edit";
186 case ESceneState::Play: return "Play";
187 case ESceneState::Pause: return "Pause";
188 case ESceneState::Simulate: return "Simulate";
189 }
190
191 LK_CORE_ASSERT(false, "Enum::ToString(ESceneState) failed for value: {}", static_cast<int>(SceneState));
192 return nullptr;
193 }
194 }
195
196}
Core header.
LObject implementation.
Timer.
Definition Asset.h:21
Definition EditorCamera.h:32
Definition Entity.h:18
Definition Scene.h:47
LEntity TryGetEntityWithUUID(const LUUID ID)
Definition Scene.cpp:84
LEntity GetEntityWithUUID(const LUUID UUID)
Definition Scene.cpp:76
auto GetAllEntitiesWith()
Definition Scene.h:138
void OnRenderEditor(TObjectPtr< LSceneRenderer > SceneRenderer, LEditorCamera &EditorCamera, const float DeltaTime)
Definition Scene.cpp:444
FORCEINLINE LUUID GetUUID() const
FIXME.
Definition Scene.h:60
Definition ObjectPtr.h:102
#define LCLASS(Class)
Definition CoreMacros.h:226
Definition Asset.h:11
Definition UUID.h:13