使用lambda函数和模板函数重构Copy组件功能
This commit is contained in:
@ -159,6 +159,16 @@ namespace Hazel
|
||||
CircleCollider2DComponent() = default;
|
||||
CircleCollider2DComponent(const CircleCollider2DComponent&) = default;
|
||||
};
|
||||
|
||||
template<typename... Components>
|
||||
struct ComponentGroup
|
||||
{
|
||||
};
|
||||
using AllComponents =
|
||||
ComponentGroup<TransformComponent, SpriteRendererComponent,
|
||||
CircleRendererComponent, BoxCollider2DComponent,
|
||||
CircleCollider2DComponent, CameraComponent,
|
||||
NativeScriptComponent, RigidBody2DComponent>;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -35,7 +35,7 @@ namespace Hazel
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
T& AddOrRelpaceComponent(Args&&... args)
|
||||
T& AddOrReplaceComponent(Args&&... args)
|
||||
{
|
||||
T& component = m_Scene->m_Registry.emplace_or_replace<T>(m_EntityHandle, std::forward<Args>(args)...);
|
||||
m_Scene->OnComponentAdded<T>(*this, component);
|
||||
|
||||
@ -40,31 +40,42 @@ namespace Hazel
|
||||
delete m_PhysicsWorld;
|
||||
}
|
||||
|
||||
template<typename Component>
|
||||
template<typename... Component>
|
||||
static void CopyComponent(entt::registry& dst, entt::registry& src, const std::unordered_map<UUID, entt::entity>& enttMap)
|
||||
{
|
||||
|
||||
([&]()
|
||||
{
|
||||
auto view = src.view<Component>();
|
||||
for (auto e : view)
|
||||
for (auto srcEntity : view)
|
||||
{
|
||||
UUID uuid = src.get<IDComponent>(e).ID;
|
||||
if (enttMap.find(uuid) == enttMap.end())
|
||||
entt::entity dstEntity = enttMap.at(src.get<IDComponent>(srcEntity).ID);
|
||||
|
||||
auto& srcComponent = src.get<Component>(srcEntity);
|
||||
dst.emplace_or_replace<Component>(dstEntity, srcComponent);
|
||||
}
|
||||
}(), ...);
|
||||
}
|
||||
|
||||
template<typename... Component>
|
||||
static void CopyComponent(ComponentGroup<Component ...>, entt::registry& dst, entt::registry& src, const std::unordered_map<UUID, entt::entity>& enttMap)
|
||||
{
|
||||
HZ_CORE_ERROR("ERROR");
|
||||
assert(-1);
|
||||
CopyComponent<Component ...>(dst, src, enttMap);
|
||||
}
|
||||
|
||||
entt::entity entity = enttMap.at(uuid);
|
||||
auto& component = view.get<Component>(e);
|
||||
|
||||
dst.emplace_or_replace<Component>(entity, component);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Component>
|
||||
template<typename... Component>
|
||||
static void CopyComponentIfExists(Entity dst, Entity src)
|
||||
{
|
||||
([&](){
|
||||
if (src.HasComponent<Component>())
|
||||
dst.AddOrRelpaceComponent<Component>(src.GetComponent<Component>());
|
||||
dst.AddOrReplaceComponent<Component>(src.GetComponent<Component>());
|
||||
}(), ...);
|
||||
}
|
||||
|
||||
template<typename... Component>
|
||||
static void CopyComponentIfExists(ComponentGroup<Component ...>, Entity dst, Entity src)
|
||||
{
|
||||
CopyComponentIfExists<Component ...>(dst, src);
|
||||
}
|
||||
|
||||
Ref<Scene> Scene::Copy(Ref<Scene> other)
|
||||
@ -89,6 +100,9 @@ namespace Hazel
|
||||
enttMap[uuid] = (entt::entity)newEntity;
|
||||
}
|
||||
|
||||
// Copy all components except IDComponent and TagComponent
|
||||
CopyComponent(AllComponents{}, dstSceneRegistry, srcSceneRegistry, enttMap);
|
||||
/*
|
||||
CopyComponent<TransformComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
|
||||
CopyComponent<SpriteRendererComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
|
||||
CopyComponent<CircleRendererComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
|
||||
@ -97,6 +111,7 @@ namespace Hazel
|
||||
CopyComponent<RigidBody2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
|
||||
CopyComponent<BoxCollider2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
|
||||
CopyComponent<CircleCollider2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
|
||||
*/
|
||||
|
||||
return newScene;
|
||||
}
|
||||
@ -388,6 +403,8 @@ namespace Hazel
|
||||
{
|
||||
Entity newEntity = CreateEntity(entity.GetName() + " copy");
|
||||
|
||||
CopyComponentIfExists(AllComponents{}, newEntity, entity);
|
||||
/*
|
||||
CopyComponentIfExists<TransformComponent>(newEntity, entity);
|
||||
CopyComponentIfExists<SpriteRendererComponent>(newEntity, entity);
|
||||
CopyComponentIfExists<CircleRendererComponent>(newEntity, entity);
|
||||
@ -396,6 +413,7 @@ namespace Hazel
|
||||
CopyComponentIfExists<RigidBody2DComponent>(newEntity, entity);
|
||||
CopyComponentIfExists<BoxCollider2DComponent>(newEntity, entity);
|
||||
CopyComponentIfExists<CircleCollider2DComponent>(newEntity, entity);
|
||||
*/
|
||||
}
|
||||
|
||||
Entity Scene::GetPrimaryCameraEntity()
|
||||
|
||||
@ -146,7 +146,6 @@ namespace Hazel
|
||||
if (m_ShowPhysicsColliders)
|
||||
{
|
||||
// box collider
|
||||
|
||||
auto bcview = m_ActiveScene->GetAllEntitiesWith<TransformComponent, BoxCollider2DComponent>();
|
||||
for (auto entity : bcview)
|
||||
{
|
||||
@ -169,7 +168,7 @@ namespace Hazel
|
||||
Renderer2D::DrawRect(transform, glm::vec4(0.2f, 1.0f, 0.2f, 1.0f));
|
||||
}
|
||||
|
||||
|
||||
// circle collider
|
||||
auto ccview = m_ActiveScene->GetAllEntitiesWith<TransformComponent, CircleCollider2DComponent>();
|
||||
for (auto entity : ccview)
|
||||
{
|
||||
@ -562,7 +561,7 @@ namespace Hazel
|
||||
m_CameraController.OnEvent(e);
|
||||
m_EditorCamera.OnEvent(e);
|
||||
}
|
||||
if (e.button.clicks && m_ViewportHovered && !ImGuizmo::IsOver() && Input::IsMouseButtonPressed(SDL_BUTTON_LEFT) && !Input::IsKeyPressed(SDL_SCANCODE_LALT))
|
||||
if (e.button.clicks && m_ViewportHovered && !ImGuizmo::IsOver() && Input::IsMouseButtonPressed(BUTTON_LEFT) && !Input::IsKeyPressed(SDL_SCANCODE_LALT))
|
||||
m_SceneHierachyPanel.SetSelectedEntity(m_HoveredEntity);
|
||||
|
||||
#define SHORTCUT_EXIT (SDL_KMOD_CTRL + SDLK_W)
|
||||
|
||||
Reference in New Issue
Block a user