Entity功能¶
如果把这个类传入带这个参数的函数,可以返回handle
operator entt::entity() const { return m_EntityHandle; }
T& AddComponent(Args&&... args)
{
CORE_DEBUG_ASSERT(!HasComponent<T>(), "Entity already has component!");
T& component = m_Scene->m_Registry.emplace<T>(m_EntityHandle, std::forward<Args>(args)...);
m_Scene->OnComponentAdded<T>(*this, component);
return component;
}
Scene¶
void Scene::DestroyEntity(Entity entity)
{
m_Registry.destroy(entity);
}
//-------------组件添加时触发的函数-------------------
template<typename T>
void Scene::OnComponentAdded(Entity entity, T& component)
{
static_assert(false);
}
//--------------特化模板-------------
template<>
void Scene::OnComponentAdded<TransformComponent>(Entity entity, TransformComponent& component)
{
}
template<>
void Scene::OnComponentAdded<CameraComponent>(Entity entity, CameraComponent& component)
{
component.Camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
}
template<>
void Scene::OnComponentAdded<SpriteRendererComponent>(Entity entity, SpriteRendererComponent& component)
{
}
template<>
void Scene::OnComponentAdded<TagComponent>(Entity entity, TagComponent& component)
{
}
template<>
void Scene::OnComponentAdded<NativeScriptComponent>(Entity entity, NativeScriptComponent& component)
{
}
IMGUI¶
创建实体¶
右键创建菜单,显示创建实体。 如果有当前选中的实体(上下文) + 绘制 Add Component 按钮 + 打开addcoponent界面 + 如果打开了addconponent + 菜单选项camera + 菜单选项speiteRenderer
if (ImGui::BeginPopupContextWindow(0, 1, false))
{
if (ImGui::MenuItem("Create Empty Entity"))
m_Context->CreateEntity("Empty Entity");
ImGui::EndPopup();
}
ImGui::End();
ImGui::Begin("Properties");
if (m_SelectionContext)
{
DrawComponents(m_SelectionContext);
if (ImGui::Button("Add Component"))
ImGui::OpenPopup("AddComponent");
if (ImGui::BeginPopup("AddComponent"))
{
if (ImGui::MenuItem("Camera"))
{
m_SelectionContext.AddComponent<CameraComponent>();
ImGui::CloseCurrentPopup();
}
if (ImGui::MenuItem("Sprite Renderer"))
{
m_SelectionContext.AddComponent<SpriteRendererComponent>();
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
删除实体¶
-
先建立一个创建的菜单
bool entityDeleted = false; if (ImGui::BeginPopupContextItem()) { if (ImGui::MenuItem("Delete Entity")) entityDeleted = true; ImGui::EndPopup(); }
-
再判断这个条件来执行上下文操作
if (entityDeleted) { m_Context->DestroyEntity(entity); if (m_SelectionContext == entity) m_SelectionContext = {}; }
组件自己删除¶
添加一个组件自己删除的功能
if (entity.HasComponent<SpriteRendererComponent>())
{
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 4, 4 });
bool open = ImGui::TreeNodeEx((void*)typeid(SpriteRendererComponent).hash_code(), treeNodeFlags, "Sprite Renderer");
ImGui::SameLine(ImGui::GetWindowWidth() - 25.0f);
if (ImGui::Button("+", ImVec2{ 20, 20 }))
{
ImGui::OpenPopup("ComponentSettings");
}
ImGui::PopStyleVar();
bool removeComponent = false;
if (ImGui::BeginPopup("ComponentSettings"))
{
if (ImGui::MenuItem("Remove component"))
removeComponent = true;
ImGui::EndPopup();
}
if (open)
{
auto& src = entity.GetComponent<SpriteRendererComponent>();
ImGui::ColorEdit4("Color", glm::value_ptr(src.Color));
ImGui::TreePop();
}
if (removeComponent)
entity.RemoveComponent<SpriteRendererComponent>();
}