I've got component class:
class Component
{
public:
Component();
...
};
then my various components derive from the base Component
class
class VelocityCom : public Component
{
public:
VelocityCom();
...
};
and then an entity class that holds the components and is able to return a specific component for manipulation
class Entity
{
public:
Entity();
std::unique_ptr<Component> getComponent(...);
private:
std::vector<Component> components;
};
The problem that I'm having with this approach lies withing the act of getting a specific component. Let's say I want to get a pointer to the velocity component. I can't just have whateverEntity.getComponent(Types::Velocity);
because the method returns a Component
and not a VelocityCom
. I thought about casting the Component
pointer to a VelocityCom
pointer but it seems that a lot of people don't like the idea of downcasting. So my question is what is a good way to get a specific component in an entity component system? Is downcasting okay in this situation or should I change my implementation to where I don't have to?