In my game project I am using MVC scheme. Model and View are completely separated. View is driven by observer pattern. So when something happens on game entity(create, destroy, update,…) view will be notified thru listener. When new entity in model is allocated then i need to create proper viewmodel(EntityMesh).
- EntityMesh is base for viewmodel
- Game::Entity is base for model
for this example model derivation tree looks like this:
- Game::Entity -> Game::PhysicalEntity -> Game::PhysicalGridEntity
- Game::Entity -> Game::ActorEntity -> Game::FreeActorEntity
This is how i currently “solve” this problem…but in future i expect 30+ derivatives of Game::Entity and EntityMesh. And this is very ugly and non OOP design.
EntityMesh* WorldView::_translate(Game::Entity* const ent)
{
//TODO: finish translator...
std::cout << "TRANSLATING!: " << typeid(ent).name() << std::endl;
Game::PhysicalGridEntity* pgent = dynamic_cast<Game::PhysicalGridEntity*>(ent);
if(pgent)
{
std::cout << "CREATING: PhysicalGridEntity" << std::endl;
return new PhysicalGridEntityMesh(this, pgent);
}
else
{
Game::PhysicalEntity* pent = dynamic_cast<Game::PhysicalEntity*>(ent);
if(pent)
{
std::cout << "CREATING: PhysicalEntity" << std::endl;
return new PhysicalEntityMesh(this, pent);
}
else
{
Game::FreeActorEntity* faent = dynamic_cast<Game::FreeActorEntity*>(ent);
if(faent)
{
std::cout << "CREATING: FreeActorEntity" << std::endl;
return new FreeActorEntityMesh(this, faent);
}
}
}
return NULL;
};
any idea how to make this more clear and more OOP without mixing View and Model?
EDIT: Any hint what pattern to use would help 🙂
First of all, you don’t have a controller component. Where is you business logic that connects model and view? Second, I’m not sure what you are trying to do with all these ifs, but maybe factory/visitor patterns will help you.