And why do you think it's not a good solution?
To make it a bit nicer you could add a property to the entity and check that.
I think it's not a good solution because I would have to change the drawChildren function, and add a parameter like a child, and then draw only one child by one.
The drawChildren will become a drawChild.
Inside the draw function I compare the position in the y axis between children and the parent, then I draw the parent and the current child like this:
Inside the draw() function
Compare the the position between the parent and all his children individually
for(auto child : mChildren){
if(parent.position().y < child.position().y)
{
drawChild(child)
drawCurrent()
}
else
{
drawCurrent()
drawChild(child)
}
}
However, with this solution I draw the parent x times depending on how many children it has.
Let's say that we have a parent, with 5 children.
The parent will be drawn 5 times, because for each child we draw.
There's a way to do, but I only have words to explain it, don't know how to implement it.
The solution looks like this for me :
two child containers :
std::vector<std::unique_ptr<SceneNode>> mFrontChildren
std::vector<std::unique_ptr<SceneNode>> mBackChildren
two drawChildren function:
drawFrontChildren(...)
drawBackChildren(...)
Inside the update function:
for(auto child : mChildren){
if(parent.position().y < child.position().y)
{
mBackChildren.push_back(std::move(child));
}
else
{
mFrontChildren.push_back(std::move(child));
}
}
Inside the draw function
drawBackChildren(...)
drawCurrent(...)
drawFrontChildren(...)
I need two separate functions for front and back children, the problem is, how to dynamically switch children from front and behind?
With the example of the weapon, sometimes it's in front of the player, sometimes it's behind him.
I don't really know how to implement this kind of logique.