在我的游戏项目中,我使用的是MVC方案。模型和视图完全分离。观察由观察者模式驱动。所以当游戏实体发生某些事情时(创建,销毁,更新...),视图将通过监听器通知。当分配模型中的新实体时,我需要创建适当的视图模型(EntityMesh)。C++ MVC模型viewmodel“翻译”和继承
- EntityMesh是基础视图模型
- 游戏::实体是模型
这个例子模型派生树基地看起来是这样的:
- 游戏::实体 - > Game :: PhysicalEntity - > Game :: PhysicalGridEntity
- Game :: Entity - > Game :: ActorEntity - > Game :: FreeActorEntity
这就是我目前“解决”这个问题的方法......但是在将来我期待30个以上的Game :: Entity和EntityMesh的衍生物。这是非常丑陋的非OOP设计。
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;
};
任何想法如何使这更清晰和更多的面向对象不混合视图和模型?
编辑:任何暗示使用什么模式将帮助:)
如何使用访问者模式? – 2012-02-20 14:13:26