Skip to content

Commit

Permalink
Traktor: Refactored IEntityFactory to allow runtime to dynamically cr…
Browse files Browse the repository at this point in the history
…eate all factories without explicit dependency to each module.
  • Loading branch information
apistol78 committed Apr 18, 2024
1 parent bccfc47 commit 7235c99
Show file tree
Hide file tree
Showing 24 changed files with 189 additions and 49 deletions.
30 changes: 16 additions & 14 deletions code/Ai/NavMeshEntityFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -10,38 +10,40 @@
#include "Ai/NavMeshComponent.h"
#include "Ai/NavMeshComponentData.h"
#include "Ai/NavMeshEntityFactory.h"
#include "Core/Misc/ObjectStore.h"
#include "Resource/IResourceManager.h"

namespace traktor::ai
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.ai.NavMeshEntityFactory", NavMeshEntityFactory, world::AbstractEntityFactory)
T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.ai.NavMeshEntityFactory", 0, NavMeshEntityFactory, world::AbstractEntityFactory)

NavMeshEntityFactory::NavMeshEntityFactory(resource::IResourceManager* resourceManager, bool suppress)
: m_resourceManager(resourceManager)
, m_suppress(suppress)
{
}

bool NavMeshEntityFactory::initialize(const ObjectStore& objectStore)
{
m_resourceManager = objectStore.get< resource::IResourceManager >();
return true;
}

const TypeInfoSet NavMeshEntityFactory::getEntityComponentTypes() const
{
return makeTypeInfoSet< NavMeshComponentData >();
}

Ref< world::IEntityComponent > NavMeshEntityFactory::createEntityComponent(const world::IEntityBuilder* builder, const world::IEntityComponentData& entityComponentData) const
{
if (!m_suppress)
{
auto navMeshComponentData = checked_type_cast< const NavMeshComponentData* >(&entityComponentData);

resource::Proxy< NavMesh > navMesh;
if (!m_resourceManager->bind(navMeshComponentData->get(), navMesh))
return nullptr;

return new NavMeshComponent(navMesh);
}
else
return new NavMeshComponent();
auto navMeshComponentData = checked_type_cast<const NavMeshComponentData*>(&entityComponentData);

resource::Proxy< NavMesh > navMesh;
if (!m_resourceManager->bind(navMeshComponentData->get(), navMesh))
return nullptr;

return new NavMeshComponent(navMesh);
}

}
6 changes: 5 additions & 1 deletion code/Ai/NavMeshEntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ class T_DLLCLASS NavMeshEntityFactory : public world::AbstractEntityFactory
T_RTTI_CLASS;

public:
NavMeshEntityFactory() = default;

explicit NavMeshEntityFactory(resource::IResourceManager* resourceManager, bool suppress);

virtual bool initialize(const ObjectStore& objectStore) override final;

virtual const TypeInfoSet getEntityComponentTypes() const override final;

virtual Ref< world::IEntityComponent > createEntityComponent(const world::IEntityBuilder* builder, const world::IEntityComponentData& entityComponentData) const override final;

private:
Ref< resource::IResourceManager > m_resourceManager;
bool m_suppress;
bool m_suppress = false;
};

}
13 changes: 12 additions & 1 deletion code/Animation/AnimationEntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
#include "Animation/Rotator/PendulumComponentData.h"
#include "Animation/Rotator/RotatorComponentData.h"
#include "Animation/Rotator/WobbleComponentData.h"
#include "Core/Misc/ObjectStore.h"
#include "Physics/PhysicsManager.h"
#include "Resource/IResourceManager.h"

namespace traktor::animation
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.animation.AnimationEntityFactory", AnimationEntityFactory, world::AbstractEntityFactory)
T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.animation.AnimationEntityFactory", 0, AnimationEntityFactory, world::AbstractEntityFactory)

AnimationEntityFactory::AnimationEntityFactory(resource::IResourceManager* resourceManager, render::IRenderSystem* renderSystem, physics::PhysicsManager* physicsManager)
: m_resourceManager(resourceManager)
Expand All @@ -38,6 +41,14 @@ AnimationEntityFactory::AnimationEntityFactory(resource::IResourceManager* resou
{
}

bool AnimationEntityFactory::initialize(const ObjectStore& objectStore)
{
m_resourceManager = objectStore.get< resource::IResourceManager >();
m_renderSystem = objectStore.get< render::IRenderSystem >();
m_physicsManager = objectStore.get< physics::PhysicsManager >();
return true;
}

const TypeInfoSet AnimationEntityFactory::getEntityComponentTypes() const
{
TypeInfoSet typeSet;
Expand Down
10 changes: 7 additions & 3 deletions code/Animation/AnimationEntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ class T_DLLCLASS AnimationEntityFactory : public world::AbstractEntityFactory
T_RTTI_CLASS;

public:
AnimationEntityFactory() = default;

explicit AnimationEntityFactory(resource::IResourceManager* resourceManager, render::IRenderSystem* renderSystem, physics::PhysicsManager* physicsManager);

virtual bool initialize(const ObjectStore& objectStore) override final;

virtual const TypeInfoSet getEntityComponentTypes() const override final;

virtual Ref< world::IEntityComponent > createEntityComponent(const world::IEntityBuilder* builder, const world::IEntityComponentData& entityComponentData) const override final;

private:
resource::IResourceManager* m_resourceManager;
render::IRenderSystem* m_renderSystem;
physics::PhysicsManager* m_physicsManager;
resource::IResourceManager* m_resourceManager = nullptr;
render::IRenderSystem* m_renderSystem = nullptr;
physics::PhysicsManager* m_physicsManager = nullptr;
};

}
12 changes: 11 additions & 1 deletion code/Mesh/MeshEntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Core/Misc/ObjectStore.h"
#include "Mesh/MeshComponent.h"
#include "Mesh/MeshComponentData.h"
#include "Mesh/MeshParameterComponent.h"
#include "Mesh/MeshParameterComponentData.h"
#include "Mesh/MeshEntityFactory.h"
#include "Render/IRenderSystem.h"
#include "Resource/IResourceManager.h"

namespace traktor::mesh
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.mesh.MeshEntityFactory", MeshEntityFactory, world::AbstractEntityFactory)
T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.mesh.MeshEntityFactory", 0, MeshEntityFactory, world::AbstractEntityFactory)

MeshEntityFactory::MeshEntityFactory(resource::IResourceManager* resourceManager, render::IRenderSystem* renderSystem)
: m_resourceManager(resourceManager)
, m_renderSystem(renderSystem)
{
}

bool MeshEntityFactory::initialize(const ObjectStore& objectStore)
{
m_resourceManager = objectStore.get< resource::IResourceManager >();
m_renderSystem = objectStore.get< render::IRenderSystem >();
return true;
}

const TypeInfoSet MeshEntityFactory::getEntityComponentTypes() const
{
return makeTypeInfoSet< MeshComponentData, MeshParameterComponentData >();
Expand Down
4 changes: 4 additions & 0 deletions code/Mesh/MeshEntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ class T_DLLCLASS MeshEntityFactory : public world::AbstractEntityFactory
T_RTTI_CLASS;

public:
MeshEntityFactory() = default;

explicit MeshEntityFactory(resource::IResourceManager* resourceManager, render::IRenderSystem* renderSystem);

virtual bool initialize(const ObjectStore& objectStore) override final;

virtual const TypeInfoSet getEntityComponentTypes() const override final;

virtual Ref< world::IEntityComponent > createEntityComponent(const world::IEntityBuilder* builder, const world::IEntityComponentData& entityComponentData) const override final;
Expand Down
12 changes: 11 additions & 1 deletion code/Physics/World/EntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Core/Misc/ObjectStore.h"
#include "Physics/PhysicsManager.h"
#include "Physics/World/EntityFactory.h"
#include "Physics/World/JointComponent.h"
#include "Physics/World/JointComponentData.h"
Expand All @@ -15,11 +17,12 @@
#include "Physics/World/Character/CharacterComponentData.h"
#include "Physics/World/Vehicle/VehicleComponent.h"
#include "Physics/World/Vehicle/VehicleComponentData.h"
#include "Resource/IResourceManager.h"

namespace traktor::physics
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.physics.EntityFactory", EntityFactory, world::AbstractEntityFactory)
T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.physics.EntityFactory", 0, EntityFactory, world::AbstractEntityFactory)

EntityFactory::EntityFactory(
resource::IResourceManager* resourceManager,
Expand All @@ -30,6 +33,13 @@ EntityFactory::EntityFactory(
{
}

bool EntityFactory::initialize(const ObjectStore& objectStore)
{
m_resourceManager = objectStore.get< resource::IResourceManager >();
m_physicsManager = objectStore.get< PhysicsManager >();
return true;
}

const TypeInfoSet EntityFactory::getEntityComponentTypes() const
{
return makeTypeInfoSet<
Expand Down
4 changes: 4 additions & 0 deletions code/Physics/World/EntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ class T_DLLCLASS EntityFactory : public world::AbstractEntityFactory
T_RTTI_CLASS;

public:
EntityFactory() = default;

explicit EntityFactory(
resource::IResourceManager* resourceManager,
PhysicsManager* physicsManager
);

virtual bool initialize(const ObjectStore& objectStore) override final;

virtual const TypeInfoSet getEntityComponentTypes() const override final;

virtual Ref< world::IEntityComponent > createEntityComponent(const world::IEntityBuilder* builder, const world::IEntityComponentData& entityComponentData) const override final;
Expand Down
49 changes: 31 additions & 18 deletions code/Runtime/Impl/WorldServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Ai/NavMeshEntityFactory.h"
#include "Runtime/IEnvironment.h"
#include "Runtime/Impl/AudioServer.h"
#include "Runtime/Impl/PhysicsServer.h"
#include "Runtime/Impl/RenderServer.h"
#include "Runtime/Impl/ScriptServer.h"
#include "Runtime/Impl/WorldServer.h"
#include "Animation/AnimatedMeshComponentRenderer.h"
#include "Animation/AnimationEntityFactory.h"
#include "Animation/Cloth/ClothRenderer.h"
#include "Core/Log/Log.h"
#include "Core/Misc/ObjectStore.h"
#include "Core/Settings/PropertyBoolean.h"
#include "Core/Settings/PropertyFloat.h"
#include "Core/Settings/PropertyGroup.h"
#include "Core/Settings/PropertyInteger.h"
#include "Core/Settings/PropertyString.h"
#include "Mesh/MeshComponentRenderer.h"
#include "Mesh/MeshEntityFactory.h"
#include "Mesh/Instance/InstanceMeshComponentRenderer.h"
#include "Physics/PhysicsManager.h"
#include "Render/IRenderSystem.h"
#include "Resource/IResourceManager.h"
#include "Scene/SceneFactory.h"
#include "Spray/EffectEntityFactory.h"
#include "Sound/Player/ISoundPlayer.h"
#include "Spray/EffectRenderer.h"
#include "Spray/Feedback/FeedbackManager.h"
#include "Terrain/EntityFactory.h"
#include "Terrain/EntityRenderer.h"
#include "Terrain/TerrainFactory.h"
#include "Weather/WeatherFactory.h"
#include "Weather/Precipitation/PrecipitationRenderer.h"
#include "Weather/Sky/SkyRenderer.h"
#include "World/EntityFactory.h"
Expand All @@ -44,7 +42,6 @@
#include "World/Entity/DecalRenderer.h"
#include "World/Entity/ProbeRenderer.h"
#include "World/Entity/VolumetricFogRenderer.h"
#include "World/Entity/WorldEntityFactory.h"

namespace traktor::runtime
{
Expand Down Expand Up @@ -137,18 +134,34 @@ void WorldServer::createResourceFactories(IEnvironment* environment)

void WorldServer::createEntityFactories(IEnvironment* environment)
{
physics::PhysicsManager* physicsManager = environment->getPhysics() ? environment->getPhysics()->getPhysicsManager() : nullptr;
sound::ISoundPlayer* soundPlayer = environment->getAudio() ? environment->getAudio()->getSoundPlayer() : nullptr;
render::IRenderSystem* renderSystem = environment->getRender()->getRenderSystem();
resource::IResourceManager* resourceManager = environment->getResource()->getResourceManager();
// Setup object store with relevant systems.
ObjectStore objectStore;

if (environment->getPhysics())
objectStore.set(environment->getPhysics()->getPhysicsManager());
if (environment->getAudio())
objectStore.set(environment->getAudio()->getSoundPlayer());

m_entityFactory->addFactory(new animation::AnimationEntityFactory(resourceManager, renderSystem, physicsManager));
m_entityFactory->addFactory(new ai::NavMeshEntityFactory(resourceManager, false));
m_entityFactory->addFactory(new mesh::MeshEntityFactory(resourceManager, renderSystem));
m_entityFactory->addFactory(new spray::EffectEntityFactory(resourceManager, soundPlayer, m_feedbackManager));
m_entityFactory->addFactory(new terrain::EntityFactory(resourceManager, renderSystem));
m_entityFactory->addFactory(new weather::WeatherFactory(resourceManager, renderSystem));
m_entityFactory->addFactory(new world::WorldEntityFactory(resourceManager, renderSystem, false));
objectStore.set(environment->getRender()->getRenderSystem());
objectStore.set(environment->getResource()->getResourceManager());

// Create instances of all entity factories.
const TypeInfoSet entityFactoryTypes = type_of< world::IEntityFactory >().findAllOf(false);
for (const auto& entityFactoryType : entityFactoryTypes)
{
if (!entityFactoryType->isInstantiable())
continue;

Ref< world::IEntityFactory > entityFactory = dynamic_type_cast< world::IEntityFactory* >(entityFactoryType->createInstance());
if (!entityFactory)
continue;

if (!entityFactory->initialize(objectStore))
continue;

m_entityFactory->addFactory(entityFactory);
T_DEBUG(L"Entity factory \"" << type_name(entityFactory) << L"\" initialized.");
}
}

void WorldServer::createEntityRenderers(IEnvironment* environment)
Expand Down
13 changes: 12 additions & 1 deletion code/Spray/EffectEntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Core/Misc/ObjectStore.h"
#include "Resource/IResourceManager.h"
#include "Sound/Sound.h"
#include "Sound/Player/ISoundPlayer.h"
#include "Spray/Effect.h"
#include "Spray/EffectComponent.h"
#include "Spray/EffectComponentData.h"
Expand All @@ -22,14 +24,15 @@
#include "Spray/SpawnEffectEventData.h"
#include "Spray/Feedback/EnvelopeFeedbackEvent.h"
#include "Spray/Feedback/EnvelopeFeedbackEventData.h"
#include "Spray/Feedback/IFeedbackManager.h"
#include "Spray/Feedback/OscillateFeedbackEvent.h"
#include "Spray/Feedback/OscillateFeedbackEventData.h"
#include "World/IEntityBuilder.h"

namespace traktor::spray
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.spray.EffectEntityFactory", EffectEntityFactory, world::AbstractEntityFactory)
T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.spray.EffectEntityFactory", 0, EffectEntityFactory, world::AbstractEntityFactory)

EffectEntityFactory::EffectEntityFactory(
resource::IResourceManager* resourceManager,
Expand All @@ -42,6 +45,14 @@ EffectEntityFactory::EffectEntityFactory(
{
}

bool EffectEntityFactory::initialize(const ObjectStore& objectStore)
{
m_resourceManager = objectStore.get< resource::IResourceManager >();
m_soundPlayer = objectStore.get< sound::ISoundPlayer >();
m_feedbackManager = objectStore.get< IFeedbackManager >();
return true;
}

const TypeInfoSet EffectEntityFactory::getEntityEventTypes() const
{
return makeTypeInfoSet<
Expand Down
4 changes: 4 additions & 0 deletions code/Spray/EffectEntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ class T_DLLCLASS EffectEntityFactory : public world::AbstractEntityFactory
T_RTTI_CLASS;

public:
EffectEntityFactory() = default;

explicit EffectEntityFactory(
resource::IResourceManager* resourceManager,
sound::ISoundPlayer* soundPlayer,
IFeedbackManager* feedbackManager
);

virtual bool initialize(const ObjectStore& objectStore) override final;

virtual const TypeInfoSet getEntityEventTypes() const override final;

virtual const TypeInfoSet getEntityComponentTypes() const override final;
Expand Down
Loading

0 comments on commit 7235c99

Please sign in to comment.