Hi All,
I am learning to implement a component based gameObject design to my game engine. Currently, I have a TransformComponent which inherits from Component. My GameObjects's contain a map of Components, and AddComponent and GetComponent member functions. My Game class contains a std::vector<std::shared_ptr<GameObject>> m_gameObjects;
I have successfully created a 'Player' and added it to my vector of GameObjects like so:
auto playerTransformComponent = std::make_shared<TransformComponent>();
Vector2 v;
v.x = 1;
v.y = 1;
playerTransformComponent->position = v;
auto player = std::make_shared<GameObject>();
player->AddComponent(COMPONENT_TRANSFORM, playerTransformComponent);
m_gameObjects.push_back(player);
First of all, am I going about things the right way?
Now, I would like to add a System, that controls logic. Note, I do not want to make a full blown entity component system (ecs) design. I want to be able to code all of this myself, not use an already made framework.
What I have come up with is a MovementSystem that Inherits from System, and implements update as follows:
void MovementSystem::update(float dt)
{
TransformComponent* playerTransformComponent = static_cast<TransformComponent*>(m_gameObjects[0]->GetComponent(COMPONENT_TRANSFORM).get());
auto playerPosX = playerTransformComponent->position.x;
auto playerPosY = playerTransformComponent->position.y;
}
Note that I am just testing it on ONE Game object (the player), hence I use m_gameObjects[0].
The problem is, my MovementSystem does not know of m_gameObjects, since that exists in the Game class. (identifier "m_gameObjects" is undefined).
So it seems I need some sort of GameObjectManager (yes... the dreaded manager). I am a bit stuck with options to proceed from here. Are there any design patterns that come to mind? Or will something like passing the MovementSystem a pointer to my gameObjects work fine?
Thanks