Remove Nazara SDK
This commit is contained in:
@@ -1,87 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Application.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <Nazara/Core/StringExt.hpp>
|
||||
#include <regex>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::Application
|
||||
* \brief NDK class that represents the application, it offers a set of tools to ease the development
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an Application object with command-line arguments
|
||||
*
|
||||
* Pass the argc and argv arguments from the main function.
|
||||
*
|
||||
* Command-line arguments can be retrieved by application methods
|
||||
*
|
||||
* This calls Sdk::Initialize()
|
||||
*
|
||||
* \remark Only one Application instance can exist at a time
|
||||
*/
|
||||
Application::Application(int argc, char* argv[]) :
|
||||
Application()
|
||||
{
|
||||
ParseCommandline(argc, argv);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Runs the application by updating worlds, taking care about windows, ...
|
||||
*/
|
||||
bool Application::Run()
|
||||
{
|
||||
if (m_shouldQuit)
|
||||
return false;
|
||||
|
||||
m_updateTime = m_updateClock.Restart() / 1'000'000.f;
|
||||
|
||||
for (World& world : m_worlds)
|
||||
world.Update(m_updateTime);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Application::ClearWorlds()
|
||||
{
|
||||
m_worlds.clear();
|
||||
}
|
||||
|
||||
void Application::ParseCommandline(int argc, char* argv[])
|
||||
{
|
||||
std::regex optionRegex(R"(-(\w+))");
|
||||
std::regex valueRegex(R"(-(\w+)\s*=\s*(.+))");
|
||||
|
||||
std::smatch results;
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
std::string argument(argv[i]);
|
||||
if (std::regex_match(argument, results, valueRegex))
|
||||
{
|
||||
std::string key = Nz::ToLower(results[1].str());
|
||||
std::string value(results[2].str());
|
||||
|
||||
m_parameters[key] = value;
|
||||
NazaraDebug("Registred parameter from command-line: " + key + "=" + value);
|
||||
}
|
||||
else if (std::regex_match(argument, results, optionRegex))
|
||||
{
|
||||
std::string option(results[1].str());
|
||||
|
||||
m_options.insert(option);
|
||||
NazaraDebug("Registred option from command-line: " + option);
|
||||
}
|
||||
else
|
||||
NazaraWarning("Ignored command-line argument #" + Nz::NumberToString(i) + " \"" + argument + '"');
|
||||
}
|
||||
}
|
||||
|
||||
Application* Application::s_application = nullptr;
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/BaseComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::BaseComponent
|
||||
* \brief NDK class that represents the common base of all components
|
||||
*
|
||||
* \remark This class is meant to be purely abstract, for type erasure
|
||||
*/
|
||||
|
||||
BaseComponent::~BaseComponent() = default;
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to an entity
|
||||
*/
|
||||
|
||||
void BaseComponent::OnAttached()
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to this component
|
||||
*
|
||||
* \param component Component being attached
|
||||
*/
|
||||
|
||||
void BaseComponent::OnComponentAttached(BaseComponent& component)
|
||||
{
|
||||
NazaraUnused(component);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from this component
|
||||
*
|
||||
* \param component Component being detached
|
||||
*/
|
||||
|
||||
void BaseComponent::OnComponentDetached(BaseComponent& component)
|
||||
{
|
||||
NazaraUnused(component);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from an entity
|
||||
*/
|
||||
|
||||
void BaseComponent::OnDetached()
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when the entity is destroyed and we're still attached to it
|
||||
*
|
||||
* \remark This is always called before the entity proper destruction, and thus its components.
|
||||
*/
|
||||
void BaseComponent::OnEntityDestruction()
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when the entity is disabled
|
||||
*
|
||||
* \remark Disabling an entity will remove it from systems it belongs to, but sometimes the entity will need to do
|
||||
* additional work in order to be properly disabled (i.e.: disabling physics simulation & collisions)
|
||||
*/
|
||||
void BaseComponent::OnEntityDisabled()
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when the entity is disabled
|
||||
*
|
||||
* \remark Enabling an entity will add it back to systems it belongs to, but sometimes the entity will need to do
|
||||
* additional work in order to be properly re-enabled (i.e.: enabling physics simulation & collisions)
|
||||
*/
|
||||
void BaseComponent::OnEntityEnabled()
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries;
|
||||
std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex;
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/BaseSystem.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::BaseSystem
|
||||
* \brief NDK class that represents the common base of all systems
|
||||
*
|
||||
* \remark This class is meant to be purely abstract, for type erasure
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Destructs the object and unregisters it-self on every entities
|
||||
*/
|
||||
|
||||
BaseSystem::~BaseSystem()
|
||||
{
|
||||
for (const EntityHandle& entity : m_entities)
|
||||
entity->UnregisterSystem(m_systemIndex);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the key of the entity matches the lock of the system
|
||||
* \return true If it is the case
|
||||
*
|
||||
* \param Pointer to the entity
|
||||
*/
|
||||
|
||||
bool BaseSystem::Filters(const Entity* entity) const
|
||||
{
|
||||
if (!entity)
|
||||
return false;
|
||||
|
||||
const Nz::Bitset<>& components = entity->GetComponentBits();
|
||||
|
||||
m_filterResult.PerformsAND(m_requiredComponents, components);
|
||||
if (m_filterResult != m_requiredComponents)
|
||||
return false; // At least one required component is not available
|
||||
|
||||
m_filterResult.PerformsAND(m_excludedComponents, components);
|
||||
if (m_filterResult.TestAny())
|
||||
return false; // At least one excluded component is available
|
||||
|
||||
// If we have a list of needed components
|
||||
if (m_requiredAnyComponents.TestAny())
|
||||
{
|
||||
if (!m_requiredAnyComponents.Intersects(components))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the update order of this system
|
||||
*
|
||||
* The system update order is used by the world it belongs to in order to know in which order they should be updated, as some application logic may rely a specific update order.
|
||||
* A system with a greater update order (ex: 1) is guaranteed to be updated after a system with a lesser update order (ex: -1), otherwise the order is unspecified (and is not guaranteed to be stable).
|
||||
*
|
||||
* \param updateOrder The relative update order of the system
|
||||
*
|
||||
* \remark The update order is only used by World::Update(float) and does not have any effect regarding a call to BaseSystem::Update(float)
|
||||
*
|
||||
* \see GetUpdateOrder
|
||||
*/
|
||||
void BaseSystem::SetUpdateOrder(int updateOrder)
|
||||
{
|
||||
m_updateOrder = updateOrder;
|
||||
|
||||
if (m_world)
|
||||
m_world->InvalidateSystemOrder();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when entity is added to the system
|
||||
*
|
||||
* \param Pointer to the entity
|
||||
*/
|
||||
|
||||
void BaseSystem::OnEntityAdded(Entity* entity)
|
||||
{
|
||||
NazaraUnused(entity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when entity is removed to the system
|
||||
*
|
||||
* \param Pointer to the entity
|
||||
*/
|
||||
|
||||
void BaseSystem::OnEntityRemoved(Entity* entity)
|
||||
{
|
||||
NazaraUnused(entity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when entity is validated for the system
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
* \param justAdded Is the entity newly added
|
||||
*/
|
||||
|
||||
void BaseSystem::OnEntityValidation(Entity* entity, bool justAdded)
|
||||
{
|
||||
NazaraUnused(entity);
|
||||
NazaraUnused(justAdded);
|
||||
}
|
||||
|
||||
SystemIndex BaseSystem::s_nextIndex;
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/ClientApplication.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <regex>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::ClientApplication
|
||||
* \brief NDK class that represents a client-side application, it offers a set of tools to ease the development
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an ClientApplication object with command-line arguments
|
||||
*
|
||||
* Pass the argc and argv arguments from the main function.
|
||||
*
|
||||
* Command-line arguments can be retrieved by application methods
|
||||
*
|
||||
* This calls Sdk::Initialize()
|
||||
*
|
||||
* \remark Only one Application instance can exist at a time
|
||||
*/
|
||||
ClientApplication::ClientApplication(int argc, char* argv[]) :
|
||||
ClientApplication()
|
||||
{
|
||||
ParseCommandline(argc, argv);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Runs the application by updating worlds, taking care about windows, ...
|
||||
*/
|
||||
bool ClientApplication::Run()
|
||||
{
|
||||
if (!Application::Run())
|
||||
return false;
|
||||
|
||||
bool hasAtLeastOneActiveWindow = false;
|
||||
|
||||
auto it = m_windows.begin();
|
||||
while (it != m_windows.end())
|
||||
{
|
||||
Nz::Window& window = *it->window;
|
||||
|
||||
window.ProcessEvents();
|
||||
|
||||
if (!window.IsOpen(true))
|
||||
{
|
||||
it = m_windows.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
hasAtLeastOneActiveWindow = true;
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
if (m_exitOnClosedWindows && !hasAtLeastOneActiveWindow)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ClientApplication* ClientApplication::s_clientApplication = nullptr;
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/ClientSdk.hpp>
|
||||
#include <Nazara/Audio/Audio.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <Nazara/Graphics/Graphics.hpp>
|
||||
#include <Nazara/Physics2D/Physics2D.hpp>
|
||||
#include <Nazara/Physics3D/Physics3D.hpp>
|
||||
#include <Nazara/Platform/Platform.hpp>
|
||||
#include <Nazara/Utility/Utility.hpp>
|
||||
#include <NazaraSDK/Algorithm.hpp>
|
||||
#include <NazaraSDK/BaseSystem.hpp>
|
||||
#include <NazaraSDK/Sdk.hpp>
|
||||
#include <NazaraSDK/Components/ListenerComponent.hpp>
|
||||
#include <NazaraSDK/Systems/ListenerSystem.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::Sdk
|
||||
* \brief NDK class that represents the software development kit, a set of tools made to ease the conception of application
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Initializes the Sdk module
|
||||
* \return true if initialization is successful
|
||||
*
|
||||
* \remark Produces a NazaraNotice
|
||||
*/
|
||||
ClientSdk::ClientSdk(Config /*config*/) :
|
||||
ModuleBase("ClientSDK", this)
|
||||
{
|
||||
Nz::ErrorFlags errFlags(Nz::ErrorMode::ThrowException, true);
|
||||
|
||||
// Client components
|
||||
InitializeComponent<ListenerComponent>("NdkList");
|
||||
|
||||
// Systems
|
||||
|
||||
// Client systems
|
||||
InitializeSystem<ListenerSystem>();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Uninitializes the Sdk module
|
||||
*
|
||||
* \remark Produces a NazaraNotice
|
||||
*/
|
||||
ClientSdk::~ClientSdk() = default;
|
||||
|
||||
ClientSdk* ClientSdk::s_instance = nullptr;
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/CollisionComponent2D.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem2D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::CollisionComponent2D
|
||||
* \brief NDK class that represents a two-dimensional collision geometry
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Gets the collision box representing the entity
|
||||
* \return The physics collision box
|
||||
*/
|
||||
Nz::Rectf CollisionComponent2D::GetAABB() const
|
||||
{
|
||||
return GetRigidBody()->GetAABB();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the position offset between the actual rigid body center of mass position and the origin of the geometry
|
||||
* \return Position offset
|
||||
*/
|
||||
const Nz::Vector2f& CollisionComponent2D::GetGeomOffset() const
|
||||
{
|
||||
return GetRigidBody()->GetPositionOffset();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Convenience function to align center of geometry to a specific point
|
||||
*
|
||||
* \param geomOffset Position offset
|
||||
*
|
||||
* \remark This does not change the center of mass
|
||||
*/
|
||||
void CollisionComponent2D::Recenter(const Nz::Vector2f& origin)
|
||||
{
|
||||
const Nz::RigidBody2D* rigidBody = GetRigidBody();
|
||||
SetGeomOffset(origin - rigidBody->GetAABB().GetCenter() + rigidBody->GetPositionOffset());
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets geometry for the entity
|
||||
*
|
||||
* \param geom Geometry used for collisions
|
||||
*/
|
||||
void CollisionComponent2D::SetGeom(std::shared_ptr<Nz::Collider2D> geom, bool recomputeMoment, bool recomputeMassCenter)
|
||||
{
|
||||
m_geom = std::move(geom);
|
||||
|
||||
GetRigidBody()->SetGeom(m_geom, recomputeMoment, recomputeMassCenter);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the position offset between the actual rigid body center of mass position and the origin of the geometry
|
||||
*
|
||||
* \param geomOffset Position offset
|
||||
*/
|
||||
void CollisionComponent2D::SetGeomOffset(const Nz::Vector2f& geomOffset)
|
||||
{
|
||||
GetRigidBody()->SetPositionOffset(geomOffset);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Initializes the static body
|
||||
*
|
||||
* \remark Produces a NazaraAssert if entity is invalid
|
||||
* \remark Produces a NazaraAssert if entity is not linked to a world, or the world has no physics system
|
||||
*/
|
||||
void CollisionComponent2D::InitializeStaticBody()
|
||||
{
|
||||
NazaraAssert(m_entity, "Invalid entity");
|
||||
World* entityWorld = m_entity->GetWorld();
|
||||
|
||||
NazaraAssert(entityWorld, "Entity must have world");
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a physics system");
|
||||
Nz::PhysWorld2D& physWorld = entityWorld->GetSystem<PhysicsSystem2D>().GetPhysWorld();
|
||||
|
||||
m_staticBody = std::make_unique<Nz::RigidBody2D>(&physWorld, 0.f, m_geom);
|
||||
m_staticBody->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));
|
||||
|
||||
Nz::Matrix4f matrix;
|
||||
if (m_entity->HasComponent<NodeComponent>())
|
||||
matrix = m_entity->GetComponent<NodeComponent>().GetTransformMatrix();
|
||||
else
|
||||
matrix.MakeIdentity();
|
||||
|
||||
m_staticBody->SetPosition(Nz::Vector2f(matrix.GetTranslation()));
|
||||
}
|
||||
|
||||
Nz::RigidBody2D* CollisionComponent2D::GetRigidBody()
|
||||
{
|
||||
if (m_entity->HasComponent<PhysicsComponent2D>())
|
||||
{
|
||||
PhysicsComponent2D& physComponent = m_entity->GetComponent<PhysicsComponent2D>();
|
||||
return physComponent.GetRigidBody();
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraAssert(m_staticBody, "An entity without physics component should have a static body");
|
||||
return m_staticBody.get();
|
||||
}
|
||||
}
|
||||
|
||||
const Nz::RigidBody2D* CollisionComponent2D::GetRigidBody() const
|
||||
{
|
||||
if (m_entity->HasComponent<PhysicsComponent2D>())
|
||||
{
|
||||
PhysicsComponent2D& physComponent = m_entity->GetComponent<PhysicsComponent2D>();
|
||||
return physComponent.GetRigidBody();
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraAssert(m_staticBody, "An entity without physics component should have a static body");
|
||||
return m_staticBody.get();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to an entity
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnAttached()
|
||||
{
|
||||
if (!m_entity->HasComponent<PhysicsComponent2D>())
|
||||
InitializeStaticBody();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to this component
|
||||
*
|
||||
* \param component Component being attached
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnComponentAttached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<PhysicsComponent2D>(component))
|
||||
m_staticBody.reset();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from this component
|
||||
*
|
||||
* \param component Component being detached
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnComponentDetached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<PhysicsComponent2D>(component))
|
||||
InitializeStaticBody();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from an entity
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnDetached()
|
||||
{
|
||||
m_staticBody.reset();
|
||||
}
|
||||
|
||||
ComponentIndex CollisionComponent2D::componentIndex;
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/CollisionComponent3D.hpp>
|
||||
#include <Nazara/Physics3D/RigidBody3D.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent3D.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem3D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::CollisionComponent3D
|
||||
* \brief NDK class that represents the component for collision (meant for static objects)
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Sets geometry for the entity
|
||||
*
|
||||
* \param geom Geometry used for collisions
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the entity has no physics component and has no static body
|
||||
*/
|
||||
|
||||
void CollisionComponent3D::SetGeom(std::shared_ptr<Nz::Collider3D> geom)
|
||||
{
|
||||
m_geom = std::move(geom);
|
||||
|
||||
if (m_entity->HasComponent<PhysicsComponent3D>())
|
||||
{
|
||||
// We update the geometry of the PhysiscsObject linked to the PhysicsComponent3D
|
||||
PhysicsComponent3D& physComponent = m_entity->GetComponent<PhysicsComponent3D>();
|
||||
physComponent.GetRigidBody()->SetGeom(m_geom);
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraAssert(m_staticBody, "An entity without physics component should have a static body");
|
||||
m_staticBody->SetGeom(m_geom);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Initializes the static body
|
||||
*
|
||||
* \remark Produces a NazaraAssert if entity is invalid
|
||||
* \remark Produces a NazaraAssert if entity is not linked to a world, or the world has no physics system
|
||||
*/
|
||||
|
||||
void CollisionComponent3D::InitializeStaticBody()
|
||||
{
|
||||
NazaraAssert(m_entity, "Invalid entity");
|
||||
World* entityWorld = m_entity->GetWorld();
|
||||
|
||||
NazaraAssert(entityWorld, "Entity must have world");
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem3D>(), "World must have a physics system");
|
||||
Nz::PhysWorld3D& physWorld = entityWorld->GetSystem<PhysicsSystem3D>().GetWorld();
|
||||
|
||||
m_staticBody = std::make_unique<Nz::RigidBody3D>(&physWorld, m_geom);
|
||||
m_staticBody->EnableAutoSleep(false);
|
||||
m_staticBody->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to an entity
|
||||
*/
|
||||
|
||||
void CollisionComponent3D::OnAttached()
|
||||
{
|
||||
if (!m_entity->HasComponent<PhysicsComponent3D>())
|
||||
InitializeStaticBody();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to this component
|
||||
*
|
||||
* \param component Component being attached
|
||||
*/
|
||||
|
||||
void CollisionComponent3D::OnComponentAttached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<PhysicsComponent3D>(component))
|
||||
m_staticBody.reset();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from this component
|
||||
*
|
||||
* \param component Component being detached
|
||||
*/
|
||||
|
||||
void CollisionComponent3D::OnComponentDetached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<PhysicsComponent3D>(component))
|
||||
InitializeStaticBody();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from an entity
|
||||
*/
|
||||
|
||||
void CollisionComponent3D::OnDetached()
|
||||
{
|
||||
m_staticBody.reset();
|
||||
}
|
||||
|
||||
void CollisionComponent3D::OnEntityDisabled()
|
||||
{
|
||||
if (m_staticBody)
|
||||
m_staticBody->EnableSimulation(false);
|
||||
}
|
||||
|
||||
void CollisionComponent3D::OnEntityEnabled()
|
||||
{
|
||||
if (m_staticBody)
|
||||
m_staticBody->EnableSimulation(true);
|
||||
}
|
||||
|
||||
ComponentIndex CollisionComponent3D::componentIndex;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/ConstraintComponent2D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
ConstraintComponent2D::ConstraintComponent2D(const ConstraintComponent2D& /*joint*/)
|
||||
{
|
||||
}
|
||||
|
||||
bool ConstraintComponent2D::RemoveConstraint(Nz::Constraint2D* constraintPtr)
|
||||
{
|
||||
auto it = std::find_if(m_constraints.begin(), m_constraints.end(), [constraintPtr](const ConstraintData& constraintData) { return constraintData.constraint.get() == constraintPtr; });
|
||||
if (it != m_constraints.end())
|
||||
m_constraints.erase(it);
|
||||
|
||||
return !m_constraints.empty();
|
||||
}
|
||||
|
||||
ComponentIndex ConstraintComponent2D::componentIndex;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/LifetimeComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
ComponentIndex LifetimeComponent::componentIndex;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/ListenerComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
ComponentIndex ListenerComponent::componentIndex;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
ComponentIndex NodeComponent::componentIndex;
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
#include <NazaraSDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::PhysicsComponent2D
|
||||
* \brief NDK class that represents a physics point, without any collision
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to an entity
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the world does not have a physics system
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnAttached()
|
||||
{
|
||||
World* entityWorld = m_entity->GetWorld();
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a 2D physics system");
|
||||
|
||||
Nz::PhysWorld2D& world = entityWorld->GetSystem<PhysicsSystem2D>().GetPhysWorld();
|
||||
|
||||
Nz::Vector2f positionOffset;
|
||||
|
||||
std::shared_ptr<Nz::Collider2D> geom;
|
||||
if (m_entity->HasComponent<CollisionComponent2D>())
|
||||
{
|
||||
const CollisionComponent2D& entityCollision = m_entity->GetComponent<CollisionComponent2D>();
|
||||
geom = entityCollision.GetGeom();
|
||||
positionOffset = entityCollision.GetStaticBody()->GetPositionOffset(); //< Calling GetGeomOffset would retrieve current component which is not yet initialized
|
||||
}
|
||||
else
|
||||
positionOffset = Nz::Vector2f::Zero();
|
||||
|
||||
Nz::Matrix4f matrix;
|
||||
if (m_entity->HasComponent<NodeComponent>())
|
||||
matrix = m_entity->GetComponent<NodeComponent>().GetTransformMatrix();
|
||||
else
|
||||
matrix.MakeIdentity();
|
||||
|
||||
m_object = std::make_unique<Nz::RigidBody2D>(&world, 1.f, geom);
|
||||
m_object->SetPositionOffset(positionOffset);
|
||||
m_object->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));
|
||||
|
||||
if (m_entity->HasComponent<NodeComponent>())
|
||||
{
|
||||
auto& entityNode = m_entity->GetComponent<NodeComponent>();
|
||||
m_object->SetPosition(Nz::Vector2f(entityNode.GetPosition()));
|
||||
m_object->SetRotation(entityNode.GetRotation().To2DAngle());
|
||||
}
|
||||
|
||||
if (m_pendingStates.valid)
|
||||
ApplyPhysicsState(*m_object);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to this component
|
||||
*
|
||||
* \param component Component being attached
|
||||
*
|
||||
* \remark Produces a NazaraAssert if physical object is invalid
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnComponentAttached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<CollisionComponent2D>(component))
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid object");
|
||||
m_object->SetGeom(static_cast<CollisionComponent2D&>(component).GetGeom(), false, false);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from this component
|
||||
*
|
||||
* \param component Component being detached
|
||||
*
|
||||
* \remark Produces a NazaraAssert if physical object is invalid
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnComponentDetached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<CollisionComponent2D>(component))
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid object");
|
||||
m_object->SetGeom(std::make_shared<Nz::NullCollider2D>(), false, false);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from an entity
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnDetached()
|
||||
{
|
||||
if (m_object)
|
||||
{
|
||||
CopyPhysicsState(*m_object);
|
||||
m_object.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsComponent2D::OnEntityDestruction()
|
||||
{
|
||||
// Kill rigidbody before entity destruction to force contact callbacks to be called while the entity is still valid
|
||||
m_object.reset();
|
||||
}
|
||||
|
||||
void PhysicsComponent2D::OnEntityDisabled()
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->EnableSimulation(false);
|
||||
}
|
||||
|
||||
void PhysicsComponent2D::OnEntityEnabled()
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->EnableSimulation(true);
|
||||
}
|
||||
|
||||
ComponentIndex PhysicsComponent2D::componentIndex;
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/PhysicsComponent3D.hpp>
|
||||
#include <Nazara/Physics3D/RigidBody3D.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
#include <NazaraSDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem3D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::PhysicsComponent3D
|
||||
* \brief NDK class that represents the component for physics (meant for dynamic objects)
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to an entity
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the world does not have a physics system
|
||||
*/
|
||||
|
||||
void PhysicsComponent3D::OnAttached()
|
||||
{
|
||||
World* entityWorld = m_entity->GetWorld();
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem3D>(), "World must have a physics system");
|
||||
|
||||
Nz::PhysWorld3D& world = entityWorld->GetSystem<PhysicsSystem3D>().GetWorld();
|
||||
|
||||
std::shared_ptr<Nz::Collider3D> geom;
|
||||
if (m_entity->HasComponent<CollisionComponent3D>())
|
||||
geom = m_entity->GetComponent<CollisionComponent3D>().GetGeom();
|
||||
|
||||
Nz::Matrix4f matrix;
|
||||
if (m_entity->HasComponent<NodeComponent>())
|
||||
matrix = m_entity->GetComponent<NodeComponent>().GetTransformMatrix();
|
||||
else
|
||||
matrix.MakeIdentity();
|
||||
|
||||
m_object = std::make_unique<Nz::RigidBody3D>(&world, geom, matrix);
|
||||
m_object->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));
|
||||
|
||||
if (m_pendingStates.valid)
|
||||
ApplyPhysicsState(*m_object);
|
||||
else
|
||||
m_object->SetMass(1.f);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to this component
|
||||
*
|
||||
* \param component Component being attached
|
||||
*
|
||||
* \remark Produces a NazaraAssert if physical object is invalid
|
||||
*/
|
||||
|
||||
void PhysicsComponent3D::OnComponentAttached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<CollisionComponent3D>(component))
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid object");
|
||||
|
||||
m_object->SetGeom(static_cast<CollisionComponent3D&>(component).GetGeom());
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from this component
|
||||
*
|
||||
* \param component Component being detached
|
||||
*
|
||||
* \remark Produces a NazaraAssert if physical object is invalid
|
||||
*/
|
||||
|
||||
void PhysicsComponent3D::OnComponentDetached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<CollisionComponent3D>(component))
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid object");
|
||||
|
||||
m_object->SetGeom(std::make_shared<Nz::NullCollider3D>());
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from an entity
|
||||
*/
|
||||
|
||||
void PhysicsComponent3D::OnDetached()
|
||||
{
|
||||
if (m_object)
|
||||
{
|
||||
CopyPhysicsState(*m_object);
|
||||
m_object.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsComponent3D::OnEntityDestruction()
|
||||
{
|
||||
// Kill rigid body before entity destruction to force contact callbacks to be called while the entity is still valid
|
||||
m_object.reset();
|
||||
}
|
||||
|
||||
void PhysicsComponent3D::OnEntityDisabled()
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->EnableSimulation(false);
|
||||
}
|
||||
|
||||
void PhysicsComponent3D::OnEntityEnabled()
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->EnableSimulation(true);
|
||||
}
|
||||
|
||||
ComponentIndex PhysicsComponent3D::componentIndex;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Components/VelocityComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
ComponentIndex VelocityComponent::componentIndex;
|
||||
}
|
||||
@@ -1,241 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Entity.hpp>
|
||||
#include <NazaraSDK/BaseComponent.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::Entity
|
||||
* \brief NDK class that represents an entity in a world
|
||||
*/
|
||||
|
||||
// Must exists in .cpp file because of BaseComponent unique_ptr
|
||||
Entity::Entity(Entity&&) noexcept = default;
|
||||
|
||||
/*!
|
||||
* \brief Constructs a Entity object linked to a world and with an id
|
||||
*
|
||||
* \param world World in which the entity interact
|
||||
* \param id Identifier of the entity
|
||||
*/
|
||||
Entity::Entity(World* world, EntityId id) :
|
||||
m_world(world),
|
||||
m_id(id)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Destructs the object and calls Destroy
|
||||
*
|
||||
* \see Destroy
|
||||
*/
|
||||
Entity::~Entity()
|
||||
{
|
||||
if (m_world && m_valid)
|
||||
Destroy();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Adds a component to the entity
|
||||
* \return A reference to the newly added component
|
||||
*
|
||||
* \param componentPtr Component to add to the entity
|
||||
*
|
||||
* \remark Produces a NazaraAssert if component is nullptr
|
||||
*/
|
||||
|
||||
BaseComponent& Entity::AddComponent(std::unique_ptr<BaseComponent>&& componentPtr)
|
||||
{
|
||||
NazaraAssert(componentPtr, "Component must be valid");
|
||||
|
||||
ComponentIndex index = componentPtr->GetIndex();
|
||||
|
||||
// We ensure that the vector has enough space
|
||||
if (index >= m_components.size())
|
||||
m_components.resize(index + 1);
|
||||
|
||||
// Affectation and return of the component
|
||||
m_components[index] = std::move(componentPtr);
|
||||
m_componentBits.UnboundedSet(index);
|
||||
m_removedComponentBits.UnboundedReset(index);
|
||||
|
||||
Invalidate();
|
||||
|
||||
// We get the new component and we alert other existing components of the new one
|
||||
BaseComponent& component = *m_components[index].get();
|
||||
component.SetEntity(this);
|
||||
|
||||
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
{
|
||||
if (i != index)
|
||||
m_components[i]->OnComponentAttached(component);
|
||||
}
|
||||
|
||||
// If we are currently disabled, inform the component
|
||||
if (!m_enabled)
|
||||
component.OnEntityDisabled();
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Clones the entity
|
||||
* \return The clone newly created
|
||||
*
|
||||
* \remark The close is enable by default, even if the original is disabled
|
||||
* \remark Produces a NazaraAssert if the entity is not valid
|
||||
*/
|
||||
const EntityHandle& Entity::Clone() const
|
||||
{
|
||||
NazaraAssert(IsValid(), "Invalid entity");
|
||||
|
||||
return m_world->CloneEntity(m_id);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Detaches a component from the entity
|
||||
* \return An owning pointer to the component
|
||||
*
|
||||
* Instantly detaches a component from the entity and returns it, allowing to attach it to another entity
|
||||
*
|
||||
* \remark Unlike RemoveComponent, this function instantly removes the component
|
||||
*/
|
||||
std::unique_ptr<BaseComponent> Entity::DropComponent(ComponentIndex index)
|
||||
{
|
||||
if (!HasComponent(index))
|
||||
return nullptr;
|
||||
|
||||
// We get the component and we alert existing components of the deleted one
|
||||
std::unique_ptr<BaseComponent> component = std::move(m_components[index]);
|
||||
m_components[index].reset();
|
||||
|
||||
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
{
|
||||
if (i != index)
|
||||
m_components[i]->OnComponentDetached(*component);
|
||||
}
|
||||
m_componentBits.Reset(index);
|
||||
m_removedComponentBits.UnboundedReset(index);
|
||||
|
||||
component->SetEntity(nullptr);
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enables the entity
|
||||
*
|
||||
* \param enable Should the entity be enabled
|
||||
*/
|
||||
void Entity::Enable(bool enable)
|
||||
{
|
||||
if (m_enabled != enable)
|
||||
{
|
||||
m_enabled = enable;
|
||||
if (m_enabled)
|
||||
{
|
||||
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
m_components[i]->OnEntityEnabled();
|
||||
|
||||
OnEntityEnabled(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
m_components[i]->OnEntityDisabled();
|
||||
|
||||
OnEntityDisabled(this);
|
||||
}
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Kills the entity
|
||||
*/
|
||||
void Entity::Kill()
|
||||
{
|
||||
m_world->KillEntity(this);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Invalidates the entity
|
||||
*/
|
||||
void Entity::Invalidate()
|
||||
{
|
||||
// We alert everyone that we have been updated
|
||||
m_world->Invalidate(m_id);
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Checks if the entity has been killed this update
|
||||
* \return True if the entity is currently dying and will be dead at next world refresh
|
||||
*/
|
||||
bool Entity::IsDying() const
|
||||
{
|
||||
return m_world->IsEntityDying(m_id);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Creates the entity
|
||||
*/
|
||||
|
||||
void Entity::Create()
|
||||
{
|
||||
m_enabled = true;
|
||||
m_valid = true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Destroys the entity
|
||||
*/
|
||||
|
||||
void Entity::Destroy()
|
||||
{
|
||||
OnEntityDestruction(this);
|
||||
OnEntityDestruction.Clear();
|
||||
|
||||
// We prepare components for entity destruction (some components needs this to handle some final callbacks while the entity is still valid)
|
||||
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
m_components[i]->OnEntityDestruction();
|
||||
|
||||
// Detach components while they're still attached to systems
|
||||
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
m_components[i]->SetEntity(nullptr);
|
||||
|
||||
// We alert each system
|
||||
for (std::size_t index = m_systemBits.FindFirst(); index != m_systemBits.npos; index = m_systemBits.FindNext(index))
|
||||
{
|
||||
auto sysIndex = static_cast<Ndk::SystemIndex>(index);
|
||||
|
||||
if (m_world->HasSystem(sysIndex))
|
||||
{
|
||||
BaseSystem& system = m_world->GetSystem(sysIndex);
|
||||
system.RemoveEntity(this);
|
||||
}
|
||||
}
|
||||
m_systemBits.Clear();
|
||||
|
||||
// Destroy components
|
||||
m_components.clear();
|
||||
m_componentBits.Reset();
|
||||
|
||||
// Free every handle
|
||||
UnregisterAllHandles();
|
||||
|
||||
// Remove from every list
|
||||
for (EntityList* list : m_containedInLists)
|
||||
list->NotifyEntityDestruction(this);
|
||||
|
||||
m_containedInLists.clear();
|
||||
|
||||
m_valid = false;
|
||||
}
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/EntityList.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \brief Construct a new entity list by copying another one
|
||||
*/
|
||||
EntityList::EntityList(const EntityList& entityList) :
|
||||
m_entityBits(entityList.m_entityBits),
|
||||
m_world(entityList.m_world)
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
entity->RegisterEntityList(this);
|
||||
|
||||
if (m_world)
|
||||
m_world->RegisterEntityList(this);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Construct a new entity list by moving a list into this one
|
||||
*/
|
||||
EntityList::EntityList(EntityList&& entityList) noexcept :
|
||||
m_entityBits(std::move(entityList.m_entityBits)),
|
||||
m_world(entityList.m_world)
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
{
|
||||
entity->UnregisterEntityList(&entityList);
|
||||
entity->RegisterEntityList(this);
|
||||
}
|
||||
|
||||
if (m_world)
|
||||
{
|
||||
m_world->UnregisterEntityList(&entityList);
|
||||
m_world->RegisterEntityList(this);
|
||||
|
||||
entityList.m_world = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
EntityList::~EntityList()
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
entity->UnregisterEntityList(this);
|
||||
|
||||
if (m_world)
|
||||
m_world->UnregisterEntityList(this);
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Clears the set from every entities
|
||||
*
|
||||
* \remark This resets the implicit world member, allowing you to insert entities from a different world than previously
|
||||
*/
|
||||
void EntityList::Clear()
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
entity->UnregisterEntityList(this);
|
||||
|
||||
m_entityBits.Clear();
|
||||
|
||||
if (m_world)
|
||||
{
|
||||
m_world->UnregisterEntityList(this);
|
||||
m_world = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Inserts the entity into the set
|
||||
*
|
||||
* Marks an entity as present in this entity list, it must belongs to the same world as others entities contained in this list.
|
||||
*
|
||||
* \param entity Valid pointer to an entity
|
||||
*
|
||||
* \remark If entity is already contained, no action is performed
|
||||
* \remark If any entity has been inserted since construction (or last Clear call), the entity must belong to the same world as the previously inserted entities
|
||||
*/
|
||||
void EntityList::Insert(Entity* entity)
|
||||
{
|
||||
NazaraAssert(entity, "Invalid entity");
|
||||
|
||||
if (!Has(entity))
|
||||
{
|
||||
entity->RegisterEntityList(this);
|
||||
|
||||
m_entityBits.UnboundedSet(entity->GetId(), true);
|
||||
if (!m_world)
|
||||
{
|
||||
m_world = entity->GetWorld();
|
||||
m_world->RegisterEntityList(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EntityList& EntityList::operator=(const EntityList& entityList)
|
||||
{
|
||||
if (m_world)
|
||||
m_world->UnregisterEntityList(this);
|
||||
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
entity->UnregisterEntityList(this);
|
||||
|
||||
m_entityBits = entityList.m_entityBits;
|
||||
m_world = entityList.m_world;
|
||||
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
entity->RegisterEntityList(this);
|
||||
|
||||
if (m_world)
|
||||
m_world->RegisterEntityList(this);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
EntityList& EntityList::operator=(EntityList&& entityList) noexcept
|
||||
{
|
||||
if (this == &entityList)
|
||||
return *this;
|
||||
|
||||
if (m_world)
|
||||
m_world->UnregisterEntityList(this);
|
||||
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
entity->UnregisterEntityList(this);
|
||||
|
||||
m_entityBits = std::move(entityList.m_entityBits);
|
||||
m_world = entityList.m_world;
|
||||
|
||||
if (m_world)
|
||||
{
|
||||
m_world->UnregisterEntityList(&entityList);
|
||||
m_world->RegisterEntityList(this);
|
||||
|
||||
entityList.m_world = nullptr;
|
||||
}
|
||||
|
||||
for (const Ndk::EntityHandle& entity : *this)
|
||||
{
|
||||
entity->UnregisterEntityList(&entityList);
|
||||
entity->RegisterEntityList(this);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const EntityHandle& EntityList::iterator::operator*() const
|
||||
{
|
||||
return m_list->GetWorld()->GetEntity(static_cast<EntityId>(m_nextEntityId));
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -1,71 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Sdk.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <NazaraSDK/Algorithm.hpp>
|
||||
#include <NazaraSDK/BaseSystem.hpp>
|
||||
#include <NazaraSDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NazaraSDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NazaraSDK/Components/LifetimeComponent.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent3D.hpp>
|
||||
#include <NazaraSDK/Components/VelocityComponent.hpp>
|
||||
#include <NazaraSDK/Components/ConstraintComponent2D.hpp>
|
||||
#include <NazaraSDK/Systems/LifetimeSystem.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem3D.hpp>
|
||||
#include <NazaraSDK/Systems/VelocitySystem.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::Sdk
|
||||
* \brief NDK class that represents the software development kit, a set of tools made to ease the conception of application
|
||||
*/
|
||||
|
||||
Sdk::Sdk(Config /*config*/) :
|
||||
ModuleBase("SDK", this)
|
||||
{
|
||||
Nz::ErrorFlags errFlags(Nz::ErrorMode::ThrowException, true);
|
||||
|
||||
// SDK Initialization
|
||||
|
||||
// Components
|
||||
BaseComponent::Initialize();
|
||||
|
||||
// Shared components
|
||||
InitializeComponent<CollisionComponent2D>("NdkColl2");
|
||||
InitializeComponent<CollisionComponent3D>("NdkColl3");
|
||||
InitializeComponent<LifetimeComponent>("NdkLiftm");
|
||||
InitializeComponent<NodeComponent>("NdkNode");
|
||||
InitializeComponent<PhysicsComponent2D>("NdkPhys2");
|
||||
InitializeComponent<PhysicsComponent3D>("NdkPhys3");
|
||||
InitializeComponent<VelocityComponent>("NdkVeloc");
|
||||
InitializeComponent<VelocityComponent>("NdkCons2");
|
||||
|
||||
// Systems
|
||||
|
||||
BaseSystem::Initialize();
|
||||
|
||||
// Shared systems
|
||||
InitializeSystem<LifetimeSystem>();
|
||||
InitializeSystem<PhysicsSystem2D>();
|
||||
InitializeSystem<PhysicsSystem3D>();
|
||||
InitializeSystem<VelocitySystem>();
|
||||
}
|
||||
|
||||
Sdk::~Sdk()
|
||||
{
|
||||
// Components
|
||||
BaseComponent::Uninitialize();
|
||||
|
||||
// Systems
|
||||
BaseSystem::Uninitialize();
|
||||
}
|
||||
|
||||
Sdk* Sdk::s_instance;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/State.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::State
|
||||
* \brief NDK class that represents a state of your application
|
||||
*/
|
||||
|
||||
State::~State() = default;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Systems/LifetimeSystem.hpp>
|
||||
#include <NazaraSDK/Components/LifetimeComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
LifetimeSystem::LifetimeSystem()
|
||||
{
|
||||
Requires<LifetimeComponent>();
|
||||
}
|
||||
|
||||
void LifetimeSystem::OnUpdate(float elapsedTime)
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
||||
{
|
||||
auto& lifetime = entity->GetComponent<LifetimeComponent>();
|
||||
|
||||
if (lifetime.UpdateLifetime(elapsedTime))
|
||||
entity->Kill();
|
||||
}
|
||||
}
|
||||
|
||||
SystemIndex LifetimeSystem::systemIndex;
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Systems/ListenerSystem.hpp>
|
||||
#include <Nazara/Audio/Audio.hpp>
|
||||
#include <NazaraSDK/Components/ListenerComponent.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <cassert>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::ListenerSystem
|
||||
* \brief NDK class that represents the audio system
|
||||
*
|
||||
* \remark This system is enabled if the entity owns the trait: ListenerComponent and NodeComponent
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an ListenerSystem object by default
|
||||
*/
|
||||
|
||||
ListenerSystem::ListenerSystem()
|
||||
{
|
||||
Requires<ListenerComponent, NodeComponent>();
|
||||
SetUpdateOrder(100); //< Update last, after every movement is done
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when system is updated
|
||||
*
|
||||
* \param elapsedTime Delta time used for the update
|
||||
*/
|
||||
|
||||
void ListenerSystem::OnUpdate(float elapsedTime)
|
||||
{
|
||||
std::size_t activeListenerCount = 0;
|
||||
|
||||
Nz::Audio* audio = Nz::Audio::Instance();
|
||||
assert(audio);
|
||||
|
||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
||||
{
|
||||
// Is the listener actif ?
|
||||
const ListenerComponent& listener = entity->GetComponent<ListenerComponent>();
|
||||
if (!listener.IsActive())
|
||||
continue;
|
||||
|
||||
Nz::Vector3f oldPos = audio->GetListenerPosition();
|
||||
|
||||
// We get the position and the rotation to affect these to the listener
|
||||
const NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
Nz::Vector3f newPos = node.GetPosition(Nz::CoordSys::Global);
|
||||
|
||||
audio->SetListenerPosition(newPos);
|
||||
audio->SetListenerRotation(node.GetRotation(Nz::CoordSys::Global));
|
||||
|
||||
// Compute listener velocity based on their old/new position
|
||||
Nz::Vector3f velocity = (newPos - oldPos) / elapsedTime;
|
||||
audio->SetListenerVelocity(velocity);
|
||||
|
||||
activeListenerCount++;
|
||||
}
|
||||
|
||||
if (activeListenerCount > 1)
|
||||
NazaraWarning(Nz::NumberToString(activeListenerCount) + " listeners were active in the same update loop");
|
||||
}
|
||||
|
||||
SystemIndex ListenerSystem::systemIndex;
|
||||
}
|
||||
@@ -1,348 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NazaraSDK/World.hpp>
|
||||
#include <NazaraSDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent3D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::PhysicsSystem2D
|
||||
* \brief NDK class that represents a two-dimensional physics system
|
||||
*
|
||||
* \remark This system is enabled if the entity has the trait: NodeComponent and any of these two: CollisionComponent3D or PhysicsComponent3D
|
||||
* \remark Static objects do not have a velocity specified by the physical engine
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an PhysicsSystem object by default
|
||||
*/
|
||||
|
||||
PhysicsSystem2D::PhysicsSystem2D()
|
||||
{
|
||||
Requires<NodeComponent>();
|
||||
RequiresAny<CollisionComponent2D, PhysicsComponent2D>();
|
||||
Excludes<PhysicsComponent3D>();
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::CreatePhysWorld() const
|
||||
{
|
||||
NazaraAssert(!m_physWorld, "Physics world should not be created twice");
|
||||
|
||||
m_physWorld = std::make_unique<Nz::PhysWorld2D>();
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::DebugDraw(const DebugDrawOptions& options, bool drawShapes, bool drawConstraints, bool drawCollisions)
|
||||
{
|
||||
Nz::PhysWorld2D::DebugDrawOptions worldOptions{ options.constraintColor, options.collisionPointColor, options.shapeOutlineColor };
|
||||
|
||||
if (options.colorCallback)
|
||||
{
|
||||
worldOptions.colorCallback = [&options, this](Nz::RigidBody2D& body, std::size_t shapeIndex, void* userdata)
|
||||
{
|
||||
return options.colorCallback(GetEntityFromBody(body), shapeIndex, userdata);
|
||||
};
|
||||
}
|
||||
|
||||
worldOptions.circleCallback = options.circleCallback;
|
||||
worldOptions.dotCallback = options.dotCallback;
|
||||
worldOptions.polygonCallback = options.polygonCallback;
|
||||
worldOptions.segmentCallback = options.segmentCallback;
|
||||
worldOptions.thickSegmentCallback = options.thickSegmentCallback;
|
||||
|
||||
worldOptions.userdata = options.userdata;
|
||||
|
||||
GetPhysWorld().DebugDraw(worldOptions, drawShapes, drawConstraints, drawCollisions);
|
||||
}
|
||||
|
||||
const EntityHandle& PhysicsSystem2D::GetEntityFromBody(const Nz::RigidBody2D& body) const
|
||||
{
|
||||
auto entityId = static_cast<EntityId>(reinterpret_cast<std::uintptr_t>(body.GetUserdata()));
|
||||
|
||||
auto& world = GetWorld();
|
||||
|
||||
NazaraAssert(world.IsEntityIdValid(entityId), "All Bodies of this world must be part of the physics world by using PhysicsComponent");
|
||||
|
||||
return world.GetEntity(entityId);
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, EntityHandle* nearestBody)
|
||||
{
|
||||
Nz::RigidBody2D* body;
|
||||
bool res = GetPhysWorld().NearestBodyQuery(from, maxDistance, collisionGroup, categoryMask, collisionMask, &body);
|
||||
|
||||
(*nearestBody) = GetEntityFromBody(*body);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, NearestQueryResult* result)
|
||||
{
|
||||
Nz::PhysWorld2D::NearestQueryResult queryResult;
|
||||
if (GetPhysWorld().NearestBodyQuery(from, maxDistance, collisionGroup, categoryMask, collisionMask, &queryResult))
|
||||
{
|
||||
result->nearestBody = GetEntityFromBody(*queryResult.nearestBody);
|
||||
result->closestPoint = std::move(queryResult.closestPoint);
|
||||
result->fraction = std::move(queryResult.fraction);
|
||||
result->distance = queryResult.distance;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RaycastQuery(const Nz::Vector2f & from, const Nz::Vector2f & to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, const std::function<void(const RaycastHit&)>& callback)
|
||||
{
|
||||
return GetPhysWorld().RaycastQuery(from, to, radius, collisionGroup, categoryMask, collisionMask, [this, &callback](const Nz::PhysWorld2D::RaycastHit& hitInfo)
|
||||
{
|
||||
callback({
|
||||
GetEntityFromBody(*hitInfo.nearestBody),
|
||||
hitInfo.hitPos,
|
||||
hitInfo.hitNormal,
|
||||
hitInfo.fraction
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<RaycastHit>* hitInfos)
|
||||
{
|
||||
std::vector<Nz::PhysWorld2D::RaycastHit> queryResult;
|
||||
bool res = GetPhysWorld().RaycastQuery(from, to, radius, collisionGroup, categoryMask, collisionMask, &queryResult);
|
||||
|
||||
for (auto& hitResult : queryResult)
|
||||
{
|
||||
hitInfos->push_back({
|
||||
GetEntityFromBody(*hitResult.nearestBody),
|
||||
std::move(hitResult.hitPos),
|
||||
std::move(hitResult.hitNormal),
|
||||
hitResult.fraction
|
||||
});
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::RaycastQueryFirst(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, RaycastHit* hitInfo)
|
||||
{
|
||||
Nz::PhysWorld2D::RaycastHit queryResult;
|
||||
if (GetPhysWorld().RaycastQueryFirst(from, to, radius, collisionGroup, categoryMask, collisionMask, &queryResult))
|
||||
{
|
||||
hitInfo->body = GetEntityFromBody(*queryResult.nearestBody);
|
||||
hitInfo->hitPos = std::move(queryResult.hitPos);
|
||||
hitInfo->hitNormal = std::move(queryResult.hitNormal);
|
||||
hitInfo->fraction = queryResult.fraction;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, const std::function<void(const EntityHandle&)>& callback)
|
||||
{
|
||||
return GetPhysWorld().RegionQuery(boundingBox, collisionGroup, categoryMask, collisionMask, [this, &callback](Nz::RigidBody2D* body)
|
||||
{
|
||||
callback(GetEntityFromBody(*body));
|
||||
});
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<EntityHandle>* bodies)
|
||||
{
|
||||
std::vector<Nz::RigidBody2D*> queryResult;
|
||||
GetPhysWorld().RegionQuery(boundingBox, collisionGroup, categoryMask, collisionMask, &queryResult);
|
||||
|
||||
for (auto& body : queryResult)
|
||||
{
|
||||
bodies->emplace_back(GetEntityFromBody(*body));
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when entity is validated for the system
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
* \param justAdded Is the entity newly added
|
||||
*/
|
||||
|
||||
void PhysicsSystem2D::OnEntityValidation(Entity* entity, bool justAdded)
|
||||
{
|
||||
if (entity->HasComponent<PhysicsComponent2D>())
|
||||
{
|
||||
if (entity->GetComponent<PhysicsComponent2D>().IsNodeSynchronizationEnabled())
|
||||
m_dynamicObjects.Insert(entity);
|
||||
else
|
||||
m_dynamicObjects.Remove(entity);
|
||||
|
||||
m_staticObjects.Remove(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_dynamicObjects.Remove(entity);
|
||||
m_staticObjects.Insert(entity);
|
||||
|
||||
// If entities just got added to the system, teleport them to their NodeComponent position/rotation
|
||||
// This will prevent the physics engine to mess with the scene while correcting position/rotation
|
||||
if (justAdded)
|
||||
{
|
||||
auto& collision = entity->GetComponent<CollisionComponent2D>();
|
||||
auto& node = entity->GetComponent<NodeComponent>();
|
||||
|
||||
Nz::RigidBody2D* physObj = collision.GetStaticBody();
|
||||
physObj->SetPosition(Nz::Vector2f(node.GetPosition(Nz::CoordSys::Global)));
|
||||
//physObj->SetRotation(node.GetRotation());
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_physWorld)
|
||||
CreatePhysWorld();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when system is updated
|
||||
*
|
||||
* \param elapsedTime Delta time used for the update
|
||||
*/
|
||||
|
||||
void PhysicsSystem2D::OnUpdate(float elapsedTime)
|
||||
{
|
||||
if (!m_physWorld)
|
||||
return;
|
||||
|
||||
m_physWorld->Step(elapsedTime);
|
||||
|
||||
for (const Ndk::EntityHandle& entity : m_dynamicObjects)
|
||||
{
|
||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
PhysicsComponent2D& phys = entity->GetComponent<PhysicsComponent2D>();
|
||||
|
||||
Nz::RigidBody2D* body = phys.GetRigidBody();
|
||||
node.SetRotation(body->GetRotation(), Nz::CoordSys::Global);
|
||||
node.SetPosition(Nz::Vector3f(body->GetPosition(), node.GetPosition(Nz::CoordSys::Global).z), Nz::CoordSys::Global);
|
||||
}
|
||||
|
||||
float invElapsedTime = 1.f / elapsedTime;
|
||||
for (const Ndk::EntityHandle& entity : m_staticObjects)
|
||||
{
|
||||
CollisionComponent2D& collision = entity->GetComponent<CollisionComponent2D>();
|
||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
|
||||
Nz::RigidBody2D* body = collision.GetStaticBody();
|
||||
|
||||
Nz::Vector2f oldPosition = body->GetPosition();
|
||||
Nz::Vector2f newPosition = Nz::Vector2f(node.GetPosition(Nz::CoordSys::Global));
|
||||
|
||||
// To move static objects and ensure their collisions, we have to specify them a velocity
|
||||
// (/!\: the physical engine does not apply the speed on static objects)
|
||||
if (newPosition != oldPosition)
|
||||
{
|
||||
body->SetPosition(newPosition);
|
||||
body->SetVelocity((newPosition - oldPosition) * invElapsedTime);
|
||||
}
|
||||
else
|
||||
body->SetVelocity(Nz::Vector2f::Zero());
|
||||
|
||||
Nz::RadianAnglef oldRotation = body->GetRotation();
|
||||
Nz::RadianAnglef newRotation = node.GetRotation(Nz::CoordSys::Global).To2DAngle();
|
||||
|
||||
if (newRotation != oldRotation)
|
||||
{
|
||||
body->SetRotation(oldRotation);
|
||||
body->SetAngularVelocity((newRotation - oldRotation) * invElapsedTime);
|
||||
}
|
||||
else
|
||||
body->SetAngularVelocity(Nz::RadianAnglef::Zero());
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RegisterCallbacks(unsigned int collisionId, Callback callbacks)
|
||||
{
|
||||
Nz::PhysWorld2D::Callback worldCallbacks;
|
||||
|
||||
if (callbacks.endCallback)
|
||||
{
|
||||
worldCallbacks.endCallback = [this, cb = std::move(callbacks.endCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.preSolveCallback)
|
||||
{
|
||||
worldCallbacks.preSolveCallback = [this, cb = std::move(callbacks.preSolveCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.postSolveCallback)
|
||||
{
|
||||
worldCallbacks.postSolveCallback = [this, cb = std::move(callbacks.postSolveCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.startCallback)
|
||||
{
|
||||
worldCallbacks.startCallback = [this, cb = std::move(callbacks.startCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
worldCallbacks.userdata = callbacks.userdata;
|
||||
|
||||
m_physWorld->RegisterCallbacks(collisionId, worldCallbacks);
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RegisterCallbacks(unsigned int collisionIdA, unsigned int collisionIdB, Callback callbacks)
|
||||
{
|
||||
Nz::PhysWorld2D::Callback worldCallbacks;
|
||||
|
||||
if (callbacks.endCallback)
|
||||
{
|
||||
worldCallbacks.endCallback = [this, cb = std::move(callbacks.endCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.preSolveCallback)
|
||||
{
|
||||
worldCallbacks.preSolveCallback = [this, cb = std::move(callbacks.preSolveCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.postSolveCallback)
|
||||
{
|
||||
worldCallbacks.postSolveCallback = [this, cb = std::move(callbacks.postSolveCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.startCallback)
|
||||
{
|
||||
worldCallbacks.startCallback = [this, cb = std::move(callbacks.startCallback)](Nz::PhysWorld2D& /*world*/, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
worldCallbacks.userdata = callbacks.userdata;
|
||||
|
||||
m_physWorld->RegisterCallbacks(collisionIdA, collisionIdB, worldCallbacks);
|
||||
}
|
||||
|
||||
SystemIndex PhysicsSystem2D::systemIndex;
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Systems/PhysicsSystem3D.hpp>
|
||||
#include <Nazara/Physics3D/RigidBody3D.hpp>
|
||||
#include <NazaraSDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent3D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::PhysicsSystem
|
||||
* \brief NDK class that represents the physics system
|
||||
*
|
||||
* \remark This system is enabled if the entity has the trait: NodeComponent and any of these two: CollisionComponent3D or PhysicsComponent3D
|
||||
* \remark Static objects do not have a velocity specified by the physical engine
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an PhysicsSystem object by default
|
||||
*/
|
||||
|
||||
PhysicsSystem3D::PhysicsSystem3D()
|
||||
{
|
||||
Requires<NodeComponent>();
|
||||
RequiresAny<CollisionComponent3D, PhysicsComponent3D>();
|
||||
Excludes<PhysicsComponent2D>();
|
||||
}
|
||||
|
||||
void PhysicsSystem3D::CreatePhysWorld() const
|
||||
{
|
||||
NazaraAssert(!m_world, "Physics world should not be created twice");
|
||||
|
||||
m_world = std::make_unique<Nz::PhysWorld3D>();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when entity is validated for the system
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
* \param justAdded Is the entity newly added
|
||||
*/
|
||||
|
||||
void PhysicsSystem3D::OnEntityValidation(Entity* entity, bool justAdded)
|
||||
{
|
||||
if (entity->HasComponent<PhysicsComponent3D>())
|
||||
{
|
||||
if (entity->GetComponent<PhysicsComponent3D>().IsNodeSynchronizationEnabled())
|
||||
m_dynamicObjects.Insert(entity);
|
||||
else
|
||||
m_dynamicObjects.Remove(entity);
|
||||
|
||||
m_staticObjects.Remove(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_dynamicObjects.Remove(entity);
|
||||
m_staticObjects.Insert(entity);
|
||||
|
||||
// If entities just got added to the system, teleport them to their NodeComponent position/rotation
|
||||
// This will prevent the physics engine to mess with the scene while correcting position/rotation
|
||||
if (justAdded)
|
||||
{
|
||||
auto& collision = entity->GetComponent<CollisionComponent3D>();
|
||||
auto& node = entity->GetComponent<NodeComponent>();
|
||||
|
||||
Nz::RigidBody3D* physObj = collision.GetStaticBody();
|
||||
physObj->SetPosition(node.GetPosition(Nz::CoordSys::Global));
|
||||
physObj->SetRotation(node.GetRotation(Nz::CoordSys::Global));
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_world)
|
||||
CreatePhysWorld();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when system is updated
|
||||
*
|
||||
* \param elapsedTime Delta time used for the update
|
||||
*/
|
||||
|
||||
void PhysicsSystem3D::OnUpdate(float elapsedTime)
|
||||
{
|
||||
if (!m_world)
|
||||
return;
|
||||
|
||||
m_world->Step(elapsedTime);
|
||||
|
||||
for (const Ndk::EntityHandle& entity : m_dynamicObjects)
|
||||
{
|
||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
PhysicsComponent3D& phys = entity->GetComponent<PhysicsComponent3D>();
|
||||
|
||||
Nz::RigidBody3D* physObj = phys.GetRigidBody();
|
||||
node.SetRotation(physObj->GetRotation(), Nz::CoordSys::Global);
|
||||
node.SetPosition(physObj->GetPosition(), Nz::CoordSys::Global);
|
||||
}
|
||||
|
||||
float invElapsedTime = 1.f / elapsedTime;
|
||||
for (const Ndk::EntityHandle& entity : m_staticObjects)
|
||||
{
|
||||
CollisionComponent3D& collision = entity->GetComponent<CollisionComponent3D>();
|
||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
|
||||
Nz::RigidBody3D* physObj = collision.GetStaticBody();
|
||||
|
||||
Nz::Quaternionf oldRotation = physObj->GetRotation();
|
||||
Nz::Vector3f oldPosition = physObj->GetPosition();
|
||||
Nz::Quaternionf newRotation = node.GetRotation(Nz::CoordSys::Global);
|
||||
Nz::Vector3f newPosition = node.GetPosition(Nz::CoordSys::Global);
|
||||
|
||||
// To move static objects and ensure their collisions, we have to specify them a velocity
|
||||
// (/!\: the physical motor does not apply the speed on static objects)
|
||||
if (newPosition != oldPosition)
|
||||
{
|
||||
physObj->SetPosition(newPosition);
|
||||
physObj->SetLinearVelocity((newPosition - oldPosition) * invElapsedTime);
|
||||
}
|
||||
else
|
||||
physObj->SetLinearVelocity(Nz::Vector3f::Zero());
|
||||
|
||||
if (newRotation != oldRotation)
|
||||
{
|
||||
Nz::Quaternionf transition = newRotation * oldRotation.GetConjugate();
|
||||
Nz::EulerAnglesf angles = transition.ToEulerAngles();
|
||||
Nz::Vector3f angularVelocity((angles.pitch * invElapsedTime).ToRadians(), (angles.yaw * invElapsedTime).ToRadians(), (angles.roll * invElapsedTime).ToRadians());
|
||||
|
||||
physObj->SetRotation(oldRotation);
|
||||
physObj->SetAngularVelocity(angularVelocity);
|
||||
}
|
||||
else
|
||||
physObj->SetAngularVelocity(Nz::Vector3f::Zero());
|
||||
}
|
||||
}
|
||||
|
||||
SystemIndex PhysicsSystem3D::systemIndex;
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/Systems/VelocitySystem.hpp>
|
||||
#include <NazaraSDK/Components/NodeComponent.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NazaraSDK/Components/PhysicsComponent3D.hpp>
|
||||
#include <NazaraSDK/Components/VelocityComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::VelocitySystem
|
||||
* \brief NDK class that represents the velocity system
|
||||
*
|
||||
* \remark This system is enabled if the entity owns the traits NodeComponent and VelocityComponent
|
||||
* but it's disabled with the traits: PhysicsComponent2D, PhysicsComponent3D
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an VelocitySystem object by default
|
||||
*/
|
||||
VelocitySystem::VelocitySystem()
|
||||
{
|
||||
Excludes<PhysicsComponent2D, PhysicsComponent3D>();
|
||||
Requires<NodeComponent, VelocityComponent>();
|
||||
SetUpdateOrder(10); //< Since some systems may want to stop us
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when system is updated
|
||||
*
|
||||
* \param elapsedTime Delta time used for the update
|
||||
*/
|
||||
|
||||
void VelocitySystem::OnUpdate(float elapsedTime)
|
||||
{
|
||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
||||
{
|
||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
const VelocityComponent& velocity = entity->GetComponent<VelocityComponent>();
|
||||
|
||||
node.Move(velocity.linearVelocity * elapsedTime, velocity.coordSys);
|
||||
}
|
||||
}
|
||||
|
||||
SystemIndex VelocitySystem::systemIndex;
|
||||
}
|
||||
@@ -1,312 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NazaraSDK/World.hpp>
|
||||
#include <Nazara/Core/Clock.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <NazaraSDK/BaseComponent.hpp>
|
||||
#include <NazaraSDK/Systems/LifetimeSystem.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <NazaraSDK/Systems/PhysicsSystem3D.hpp>
|
||||
#include <NazaraSDK/Systems/VelocitySystem.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::World
|
||||
* \brief NDK class that represents a world
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Destructs the object and calls Clear
|
||||
*
|
||||
* \see Clear
|
||||
*/
|
||||
|
||||
World::~World() noexcept
|
||||
{
|
||||
// The destruct must be done in an ordered way
|
||||
Clear();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Creates an entity in the world
|
||||
* \return The entity created
|
||||
*/
|
||||
|
||||
const EntityHandle& World::CreateEntity()
|
||||
{
|
||||
EntityId id;
|
||||
EntityBlock* entBlock;
|
||||
|
||||
std::size_t freeEntityId = m_freeEntityIds.FindFirst();
|
||||
if (freeEntityId != m_freeEntityIds.npos)
|
||||
{
|
||||
// We get an identifier
|
||||
m_freeEntityIds.Reset(freeEntityId); //< Remove id from free entity id
|
||||
|
||||
id = static_cast<EntityId>(freeEntityId);
|
||||
|
||||
entBlock = &m_entities[id];
|
||||
entBlock->handle.Reset(&entBlock->entity); //< Reset handle (as it was reset when entity got destroyed)
|
||||
|
||||
m_entityBlocks[id] = entBlock;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We allocate a new entity
|
||||
id = static_cast<Ndk::EntityId>(m_entityBlocks.size());
|
||||
|
||||
if (m_entities.capacity() > m_entities.size())
|
||||
{
|
||||
NazaraAssert(m_waitingEntities.empty(), "There should be no waiting entities if space is available in main container");
|
||||
|
||||
m_entities.emplace_back(Entity(this, id)); //< We can't make our vector create the entity due to the scope
|
||||
entBlock = &m_entities.back();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Pushing to entities would reallocate vector and thus, invalidate EntityHandles (which we don't want until world update)
|
||||
// To prevent this, allocate them into a separate vector and move them at update
|
||||
// For now, we are counting on m_entities grow strategy to keep allocation frequency low
|
||||
m_waitingEntities.emplace_back(std::make_unique<EntityBlock>(Entity(this, id)));
|
||||
entBlock = m_waitingEntities.back().get();
|
||||
}
|
||||
|
||||
if (id >= m_entityBlocks.size())
|
||||
m_entityBlocks.resize(id + 1);
|
||||
|
||||
m_entityBlocks[id] = entBlock;
|
||||
}
|
||||
|
||||
// We initialize the entity and we add it to the list of alive entities
|
||||
entBlock->entity.Create();
|
||||
|
||||
m_aliveEntities.Insert(&entBlock->entity);
|
||||
|
||||
return entBlock->handle;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Clears the world from every entities
|
||||
*
|
||||
* \remark Every handles are correctly invalidated, entities are immediately invalidated
|
||||
*/
|
||||
|
||||
void World::Clear() noexcept
|
||||
{
|
||||
// Destroy every valid entity first, to ensure entities are still accessible by ID while being destroyed
|
||||
for (EntityBlock* entBlock : m_entityBlocks)
|
||||
{
|
||||
if (entBlock->entity.IsValid())
|
||||
entBlock->entity.Destroy();
|
||||
}
|
||||
m_entityBlocks.clear();
|
||||
|
||||
// Reset world for entity lists
|
||||
for (EntityList* list : m_referencedByLists)
|
||||
list->SetWorld(nullptr);
|
||||
m_referencedByLists.clear();
|
||||
|
||||
m_entities.clear();
|
||||
m_waitingEntities.clear();
|
||||
|
||||
m_aliveEntities.Clear();
|
||||
m_dirtyEntities.front.Clear();
|
||||
m_freeEntityIds.Clear();
|
||||
m_killedEntities.front.Clear();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Clones the entity
|
||||
* \return The clone newly created
|
||||
*
|
||||
* \param id Identifier of the entity
|
||||
*
|
||||
* \remark Cloning a disabled entity will produce an enabled clone
|
||||
*/
|
||||
const EntityHandle& World::CloneEntity(EntityId id)
|
||||
{
|
||||
const EntityHandle& original = GetEntity(id);
|
||||
if (!original)
|
||||
{
|
||||
NazaraError("Invalid entity ID");
|
||||
return EntityHandle::InvalidHandle;
|
||||
}
|
||||
|
||||
return CloneEntity(original);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Clones the entity
|
||||
* \return The clone newly created
|
||||
*
|
||||
* \param original Entity handle
|
||||
*
|
||||
* \remark Cloning a disabled entity will produce an enabled clone
|
||||
*/
|
||||
const EntityHandle& World::CloneEntity(const EntityHandle& original)
|
||||
{
|
||||
const EntityHandle& clone = CreateEntity();
|
||||
if (!original->IsEnabled())
|
||||
clone->Disable();
|
||||
|
||||
const Nz::Bitset<>& componentBits = original->GetComponentBits();
|
||||
for (std::size_t i = componentBits.FindFirst(); i != componentBits.npos; i = componentBits.FindNext(i))
|
||||
clone->AddComponent(original->GetComponent(ComponentIndex(i)).Clone());
|
||||
|
||||
clone->Enable();
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Refreshes the world
|
||||
*
|
||||
* This function will perform all pending operations in the following order:
|
||||
* - Reorder systems according to their update order if needed
|
||||
* - Moving newly created entities (whose which allocate never-used id) data and handles to normal entity list, this will invalidate references to world EntityHandle
|
||||
* - Destroying dead entities and allowing their ids to be used by newly created entities
|
||||
* - Update dirty entities, destroying their removed components and filtering them along systems
|
||||
*
|
||||
* \remark This is called automatically by Update and you most likely won't need to call it yourself
|
||||
* \remark Calling this outside of Update will not increase the profiler values
|
||||
*
|
||||
* \see GetProfilerData
|
||||
* \see Update
|
||||
*/
|
||||
void World::Refresh()
|
||||
{
|
||||
if (!m_orderedSystemsUpdated)
|
||||
ReorderSystems();
|
||||
|
||||
// Move waiting entities to entity list
|
||||
if (!m_waitingEntities.empty())
|
||||
{
|
||||
constexpr std::size_t MinEntityCapacity = 10; //< We want to be able to grow maximum entity count by at least ten without going to the waiting list
|
||||
|
||||
m_entities.reserve(m_entities.size() + m_waitingEntities.size() + MinEntityCapacity);
|
||||
for (auto& blockPtr : m_waitingEntities)
|
||||
m_entities.push_back(std::move(*blockPtr));
|
||||
|
||||
m_waitingEntities.clear();
|
||||
|
||||
// Update entity blocks pointers
|
||||
for (std::size_t i = 0; i < m_entities.size(); ++i)
|
||||
m_entityBlocks[i] = &m_entities[i];
|
||||
}
|
||||
|
||||
// Handle killed entities before last call
|
||||
std::swap(m_killedEntities.front, m_killedEntities.back);
|
||||
for (std::size_t i = m_killedEntities.back.FindFirst(); i != m_killedEntities.back.npos; i = m_killedEntities.back.FindNext(i))
|
||||
{
|
||||
NazaraAssert(i < m_entityBlocks.size(), "Entity index out of range");
|
||||
|
||||
Entity* entity = &m_entityBlocks[i]->entity;
|
||||
|
||||
// Destruction of the entity (invalidation of handle by the same way)
|
||||
entity->Destroy();
|
||||
|
||||
// Send back the identifier of the entity to the free queue
|
||||
m_freeEntityIds.UnboundedSet(i);
|
||||
}
|
||||
m_killedEntities.back.Clear();
|
||||
|
||||
// Handle of entities which need an update from the systems
|
||||
std::swap(m_dirtyEntities.front, m_dirtyEntities.back);
|
||||
for (std::size_t i = m_dirtyEntities.back.FindFirst(); i != m_dirtyEntities.back.npos; i = m_dirtyEntities.back.FindNext(i))
|
||||
{
|
||||
NazaraAssert(i < m_entityBlocks.size(), "Entity index out of range");
|
||||
|
||||
Entity* entity = &m_entityBlocks[i]->entity;
|
||||
|
||||
// Check entity validity (as it could have been reported as dirty and killed during the same iteration)
|
||||
if (!entity->IsValid())
|
||||
continue;
|
||||
|
||||
Nz::Bitset<>& removedComponents = entity->GetRemovedComponentBits();
|
||||
for (std::size_t j = removedComponents.FindFirst(); j != m_dirtyEntities.back.npos; j = removedComponents.FindNext(j))
|
||||
entity->DropComponent(static_cast<Ndk::ComponentIndex>(j));
|
||||
|
||||
for (auto& system : m_orderedSystems)
|
||||
{
|
||||
// Is our entity already part of this system?
|
||||
bool partOfSystem = system->HasEntity(entity);
|
||||
|
||||
// Should it be part of it?
|
||||
if (entity->IsEnabled() && system->Filters(entity))
|
||||
{
|
||||
// Yes it should, add it to the system if not already done and validate it (again)
|
||||
if (!partOfSystem)
|
||||
system->AddEntity(entity);
|
||||
|
||||
system->ValidateEntity(entity, !partOfSystem);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No it shouldn't, remove it if it's part of the system
|
||||
if (partOfSystem)
|
||||
system->RemoveEntity(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
m_dirtyEntities.back.Clear();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Updates the world
|
||||
* \param elapsedTime Delta time used for the update
|
||||
*
|
||||
* This function Refreshes the world and calls the Update function of every active system part of it with the elapsedTime value.
|
||||
* It also increase the profiler data with the elapsed time passed in Refresh and every system update.
|
||||
*/
|
||||
void World::Update(float elapsedTime)
|
||||
{
|
||||
if (m_isProfilerEnabled)
|
||||
{
|
||||
Nz::UInt64 t1 = Nz::GetElapsedMicroseconds();
|
||||
Refresh();
|
||||
Nz::UInt64 t2 = Nz::GetElapsedMicroseconds();
|
||||
|
||||
m_profilerData.refreshTime += t2 - t1;
|
||||
|
||||
for (auto& systemPtr : m_orderedSystems)
|
||||
{
|
||||
systemPtr->Update(elapsedTime);
|
||||
|
||||
Nz::UInt64 t3 = Nz::GetElapsedMicroseconds();
|
||||
m_profilerData.updateTime[systemPtr->GetIndex()] += t3 - t2;
|
||||
t2 = t3;
|
||||
}
|
||||
|
||||
m_profilerData.updateCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Refresh();
|
||||
|
||||
for (auto& systemPtr : m_orderedSystems)
|
||||
systemPtr->Update(elapsedTime);
|
||||
}
|
||||
}
|
||||
|
||||
void World::ReorderSystems()
|
||||
{
|
||||
m_orderedSystems.clear();
|
||||
|
||||
for (auto& systemPtr : m_systems)
|
||||
{
|
||||
if (systemPtr)
|
||||
m_orderedSystems.push_back(systemPtr.get());
|
||||
}
|
||||
|
||||
std::sort(m_orderedSystems.begin(), m_orderedSystems.end(), [] (BaseSystem* first, BaseSystem* second)
|
||||
{
|
||||
return first->GetUpdateOrder() < second->GetUpdateOrder();
|
||||
});
|
||||
|
||||
m_orderedSystemsUpdated = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user