anax icon indicating copy to clipboard operation
anax copied to clipboard

How to manage a Component with multiple implementations?

Open patrikturi opened this issue 9 years ago • 3 comments

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 :/

patrikturi avatar Oct 09 '15 17:10 patrikturi

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.

miguelmartin75 avatar Oct 13 '15 03:10 miguelmartin75

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?

patrikturi avatar Oct 17 '15 09:10 patrikturi

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>>
{
    ...
};

misopeth avatar Jan 23 '16 01:01 misopeth