I incorporated ECS to my graphics engine and I have a problem with my Renderer needing the camera which is an entity and the process is a bit bizarre. Originally, I had a (and still do) Scene which had a reference to everything and the Renderer used that, but now with ECS the Scene no longer has game objects the Renderer needs to get that through the EntityManager so the Renderer now needs both these things.
My hack I guess was to have a reference of the camera in the Scene but the Renderer still needs to get it's components.
struct Scene {
Entity camera;
ShaderProgram shaderProgram;
};
Simulation::Simulation(Scene& scene, EntityManager& entityManager)
{
// Loads scene
Entity entity = entityManager.createEntity("Camera");
entityManager.addComponent<CameraComponent>(entity, new CameraComponent());
scene.camera = entity;
}
void Renderer::Draw(const Scene& scene, EntityManager& entityManager, const Resources& resources)
{
Entity cameraEntity = scene.camera;
CameraComponent& cameraComp = entityManager.getComponent<CameraComponent>(cameraEntity);
TransformComponent& transformComp = entityManager.getComponent<TransformComponent>(cameraEntity);
glm::mat4 view = glm::lookAt(transformComp.translation, transformComp.translation + cameraComp.forward, cameraComp.up);
}
Perhaps the Scene could also keep the EntityManager so that they don't have to be passed separately and then use utility functions, but I'm not sure how I'd implement them.