anax
anax copied to clipboard
How to manage a Component with multiple implementations?
Hello, I'd like to create a Renderable component with three implementations, Sprite, AnimatedSprite and Shape. How should I do this with anax? Ideas:
- I've tried inheriting components from an abstract class component, but my Entity System for the abstract class didn't work (did not contain any entity, however I've added an entity of the child component type).
- Create all 3 components, and in the filter of the Entity System specify Sprite OR AnimatedSprite OR Shape component. But I did not find a way to do this, I saw only require and exclude filters :/
You should just create three different components:
- Sprite: used to store information about a sprite (to draw one)
- Shape: used to store information a shape (to draw one)
- Animation: stores animation information to animation an entity
Then you should essentially make:
- an AnimationSystem to animate entities with an animation component
- a SpriteRenderSystem to render entities with a Sprite component
- a ShapeRenderSystem to render entities with a Shape component
You can see an animation example in the examples directory.
My problem with your approach is that I need to sort all entities with those components by Z index and draw them in that order. If I have 3 different Systems for rendering I can't do that. This is why I'd like to have only one System for all 3 components. Do you see any other solution for this?
You could create a GraphicComponent
like this:
class GraphicComponent : public anax::Component
{
public:
...
private:
std::unique_ptr<Sprite> m_sprite;
std::unique_ptr<Shape> m_shape;
std::unique_ptr<Animation> m_animation;
};
Then a GraphicSystem
:
class GraphicSystem : public anax::System<anax::Requires<GraphicComponent>>
{
...
};