So I'm developing my first game engine and I've hit a wall. Currently I have an Entity base class that has a vector of component pointers which will hold pointers to component objects within some system manager classes (graphisManager, PhysicsManager, etc.). Here is my current Entity header (stripped down to focus on main issue):
Entity.h
class Component;
namespace BlazeGameWorld
{
class Entity
{
public:
BlazeFramework::Math::Vector2D position;
protected:
Vector<Component*> components;
static BlazeGraphics::GraphicsManager graphicsManager;
static BlazePhysics::PhysicsManager physicsManager;
static BlazeInput::InputManager inputManager;
//....other managers
private:
///////////////////////////////////////////////////////////////////////
public:
Entity();
~Entity();
virtual bool Initialize();
virtual bool Shutdown();
virtual void Update() = 0;
void AddComponent(Component* p_component);
//Part of the broadcast messaging system for components to be able
//to talk to one another in a decoupled way.
void SendMessage(uint messageID);
protected:
private:
};
}
As you can see, the idea is to have static SystemManager classes which will manage pointers to the actual components on the heap. Here is a rough Header for the potential PhysicsManager class (and it's similar for the other Manager classes):
PhysicsManager.h
class PhysicsComponent;
namespace BlazePhysics
{
class PhysicsManager
{
public:
protected:
int numPhysicsComponents;
private:
Vector<PhysicsComponent*> physicsComponents;
/////////////////////////////////////////////////////////////
public:
PhysicsManager();
~PhysicsManager();
bool Initialize();
bool Shutdown();
void Update();
template <typename PhysicsComponentType>
PhysicsComponentType* CreatePhysicsComponent();
private:
};
//template definitions
template <typename PhysicsComponentType>
PhysicsComponentType* PhysicsManager::CreatePhysicsComponent()
{
PhysicsComponentType* physicsComponent = new PhysicsComponentType
physicsComponents.push_back(physicsComponent);
return physicsComponents.at(numPhysicsComponents++);
}
}
So I can store all different physicsComponent pointers in the PhysicsManger vector (pointers to CollisionComponents, PositionComponents, etc). The issue is if I wanted to call a method specific to a specific physics component, I can't compile. For example, if (in the update loop for PhysicsManager) I wanted to update a collisionComponent's CheckCollision() method each frame I can't just say in a for loop physicsComponents.at(i).CheckCollision
because the compiler doesn't know at compile time what a CollisionComponent is. Is there a way to maybe deduce the type of the component in the array first and then if it matches CollisionComponent call the CheckCollision method? Or is there a better to implement this since this seems kind of clunky?