Core: Add DisabledComponent (to temporary disable an entity)

This commit is contained in:
SirLynix 2023-05-07 22:43:48 +02:00
parent c69397707e
commit bbbd3f88c1
10 changed files with 58 additions and 11 deletions

View File

@ -29,6 +29,7 @@
#ifndef NAZARA_CORE_COMPONENTS_HPP
#define NAZARA_CORE_COMPONENTS_HPP
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Core/Components/LifetimeComponent.hpp>
#endif // NAZARA_CORE_COMPONENTS_HPP

View File

@ -0,0 +1,29 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_COMPONENTS_DISABLEDCOMPONENT_HPP
#define NAZARA_CORE_COMPONENTS_DISABLEDCOMPONENT_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Utility/Config.hpp>
namespace Nz
{
class DisabledComponent
{
public:
DisabledComponent() = default;
DisabledComponent(const DisabledComponent&) = default;
DisabledComponent(DisabledComponent&&) = default;
~DisabledComponent() = default;
DisabledComponent& operator=(const DisabledComponent&) = default;
DisabledComponent& operator=(DisabledComponent&&) = default;
};
}
#endif // NAZARA_CORE_COMPONENTS_DISABLEDCOMPONENT_HPP

View File

@ -53,6 +53,7 @@ namespace Nz
private:
void OnCameraDestroy(entt::registry& registry, entt::entity entity);
void OnDisabledConstructed(entt::registry& registry, entt::entity entity);
void OnGraphicsDestroy(entt::registry& registry, entt::entity entity);
void OnLightDestroy(entt::registry& registry, entt::entity entity);
void OnNodeDestroy(entt::registry& registry, entt::entity entity);
@ -116,6 +117,8 @@ namespace Nz
entt::observer m_sharedSkeletonConstructObserver;
entt::observer m_skeletonConstructObserver;
entt::scoped_connection m_cameraDestroyConnection;
entt::scoped_connection m_disabledConstructedConnection;
entt::scoped_connection m_disabledDestroyConnection;
entt::scoped_connection m_graphicsDestroyConnection;
entt::scoped_connection m_lightDestroyConnection;
entt::scoped_connection m_nodeDestroyConnection;

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/BulletPhysics3D/Systems/BulletPhysics3DSystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <iostream>
#include <Nazara/BulletPhysics3D/Debug.hpp>
@ -102,7 +103,7 @@ namespace Nz
// TODO: Only replicate active entities
m_activeObjectCount = 0;
auto view = m_registry.view<NodeComponent, const BulletRigidBody3DComponent>();
auto view = m_registry.view<NodeComponent, const BulletRigidBody3DComponent>(entt::exclude<DisabledComponent>);
for (auto entity : view)
{
auto& rigidBodyComponent = view.get<const BulletRigidBody3DComponent>(entity);

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/ChipmunkPhysics2D/Systems/ChipmunkPhysics2DSystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/ChipmunkPhysics2D/Debug.hpp>
@ -49,7 +50,7 @@ namespace Nz
m_physWorld.Step(elapsedTime);
// Replicate rigid body position to their node components
auto view = m_registry.view<NodeComponent, const ChipmunkRigidBody2DComponent>();
auto view = m_registry.view<NodeComponent, const ChipmunkRigidBody2DComponent>(entt::exclude<DisabledComponent>);
for (auto [entity, nodeComponent, rigidBodyComponent] : view.each())
{
if (rigidBodyComponent.IsSleeping())

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Systems/LifetimeSystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Core/Components/LifetimeComponent.hpp>
#include <Nazara/Core/Debug.hpp>
@ -10,7 +11,7 @@ namespace Nz
{
void LifetimeSystem::Update(Time elapsedTime)
{
auto view = m_registry.view<LifetimeComponent>();
auto view = m_registry.view<LifetimeComponent>(entt::exclude<DisabledComponent>);
for (auto [entity, lifetimeComponent] : view.each())
{
lifetimeComponent.DecreaseLifetime(elapsedTime);

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Systems/RenderSystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Graphics/ForwardFramePipeline.hpp>
#include <Nazara/Graphics/ViewerInstance.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
@ -21,16 +22,17 @@ namespace Nz
{
RenderSystem::RenderSystem(entt::registry& registry) :
m_registry(registry),
m_cameraConstructObserver(registry, entt::collector.group<CameraComponent, NodeComponent>()),
m_graphicsConstructObserver(registry, entt::collector.group<GraphicsComponent, NodeComponent>()),
m_lightConstructObserver(registry, entt::collector.group<LightComponent, NodeComponent>()),
m_sharedSkeletonConstructObserver(registry, entt::collector.group<GraphicsComponent, NodeComponent, SharedSkeletonComponent>(entt::exclude<SkeletonComponent>)),
m_skeletonConstructObserver(registry, entt::collector.group<GraphicsComponent, NodeComponent, SkeletonComponent>(entt::exclude<SharedSkeletonComponent>)),
m_cameraConstructObserver(registry, entt::collector.group<CameraComponent, NodeComponent>(entt::exclude<DisabledComponent>)),
m_graphicsConstructObserver(registry, entt::collector.group<GraphicsComponent, NodeComponent>(entt::exclude<DisabledComponent>)),
m_lightConstructObserver(registry, entt::collector.group<LightComponent, NodeComponent>(entt::exclude<DisabledComponent>)),
m_sharedSkeletonConstructObserver(registry, entt::collector.group<GraphicsComponent, NodeComponent, SharedSkeletonComponent>(entt::exclude<DisabledComponent, SkeletonComponent>)),
m_skeletonConstructObserver(registry, entt::collector.group<GraphicsComponent, NodeComponent, SkeletonComponent>(entt::exclude<DisabledComponent, SharedSkeletonComponent>)),
m_cameraEntityPool(8),
m_graphicsEntityPool(1024),
m_lightEntityPool(32)
{
m_cameraDestroyConnection = registry.on_destroy<CameraComponent>().connect<&RenderSystem::OnCameraDestroy>(this);
m_disabledConstructedConnection = registry.on_construct<DisabledComponent>().connect<&RenderSystem::OnDisabledConstructed>(this);
m_graphicsDestroyConnection = registry.on_destroy<GraphicsComponent>().connect<&RenderSystem::OnGraphicsDestroy>(this);
m_lightDestroyConnection = registry.on_destroy<LightComponent>().connect<&RenderSystem::OnLightDestroy>(this);
m_nodeDestroyConnection = registry.on_destroy<NodeComponent>().connect<&RenderSystem::OnNodeDestroy>(this);
@ -89,6 +91,12 @@ namespace Nz
m_cameraEntityPool.Free(cameraEntity->poolIndex);
}
void RenderSystem::OnDisabledConstructed(entt::registry& registry, entt::entity entity)
{
// This is essentially the same
OnNodeDestroy(registry, entity);
}
void RenderSystem::OnGraphicsDestroy([[maybe_unused]] entt::registry& registry, entt::entity entity)
{
assert(&m_registry == &registry);

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/JoltPhysics3D/Systems/JoltPhysics3DSystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <iostream>
#include <Nazara/JoltPhysics3D/Debug.hpp>
@ -102,7 +103,7 @@ namespace Nz
Time t2 = GetElapsedNanoseconds();
// Replicate active rigid body position to their node components
auto view = m_registry.view<NodeComponent, const JoltRigidBody3DComponent>();
auto view = m_registry.view<NodeComponent, const JoltRigidBody3DComponent>(entt::exclude<DisabledComponent>);
for (auto entity : view)
{
auto& rigidBodyComponent = view.get<const JoltRigidBody3DComponent>(entity);

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Systems/SkeletonSystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Utility/Components/SharedSkeletonComponent.hpp>
#include <Nazara/Utility/Components/SkeletonComponent.hpp>
@ -43,7 +44,7 @@ namespace Nz
});
// Updated attached skeleton joints (TODO: Only do this if necessary)
auto view = m_registry.view<NodeComponent, SharedSkeletonComponent>();
auto view = m_registry.view<NodeComponent, SharedSkeletonComponent>(entt::exclude<DisabledComponent>);
for (auto entity : view)
{
auto& sharedSkeletonComponent = view.get<SharedSkeletonComponent>(entity);

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Systems/VelocitySystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Utility/Components/VelocityComponent.hpp>
#include <Nazara/Utility/Debug.hpp>
@ -13,7 +14,7 @@ namespace Nz
{
float delta = elapsedTime.AsSeconds();
auto view = m_registry.view<NodeComponent, VelocityComponent>();
auto view = m_registry.view<NodeComponent, VelocityComponent>(entt::exclude<DisabledComponent>);
for (auto [entity, nodeComponent, velocityComponent] : view.each())
{
NazaraUnused(entity);