Hello,
Before we begin, this is purley a learning exercise, and my game does not require this at all.
I am using an entity component system ECS framework, entityX, in my game. My spaceships can shoot bullets at enemy spaceships, and I want to use a quadtree to reduce collision checks.
In entityX, anytime you want to obtain an entity with a certain component, you use
ComponentHandle<Body> body;
for (Entity entity : m_entityManager.entities_with_components(body))
{ body->position .... }
the EntityManager m_entityManager is responsible for finding and returing the correct entity ID's.
I am using a Quadtree which works with SFML, and trying to adapt it to work with entities. I have created a QuadTreeCollisionSystem, which has a private member of a Quadtree m_quadtree; I fill the quadtree with my entities using
ex::ComponentHandle<Renderable> renderable;
for (ex::Entity entity : entities.entities_with_components(renderable))
{
m_quadtree.insertObject(entity);
}
And this works, but I have run into a problem at the stage where the entity positions are checked to see which quad they intersect with. In the original system, the function reads:
bool Quadtree::intersects(Boundable* object)
{
return mAABB.intersects(object->getBoundingRect());
}
where mAABB is a sf::FloatRect;
In the ECS approach, I do not have direct access to all of the objects like this. I must use
for (ex::Entity ent : m_entityManager.entities_with_components(renderable))
This is where I am stuck. My quadtree has no member "m_entityManager". I would like to initilize an "ex::EntityManager &m_entityManager" once with the consutrctor, but the quadtree creates a lot of instances of itself, and it does not feel right adding that in to everywhere I see a "new Quadtree.. e.g.
m_children.push_back(new Quadtree(sf::FloatRect(x, y, width, height), this));
A few questions:
1. Am I going the right way about interfaction a quadtree with ECS entities?
2. How do I go about priming a reference variable once when I create the Quadtree in QuadTreeCollisionSystem, but not every other time a Quadtree is called (within its own member functions)? Is this something to do with static member variables? If so, how should this be done? I have tried a few things but to no avail.
Thanks for all your help.