我正在用c++开发一款2d格斗游戏(出于学习目的),我很难弄清楚如何正确地实现游戏逻辑。为了快速了解我当前的体系结构,我有充当数据持有者的组件类,我有“系统”,它们只是设计用来操作这些组件的函数。我有一个scene类,它包含一组当前在游戏中的战斗机,这个场景被传递给各个子系统,然后这些子系统可以自由地作用于战斗机组件,更新战斗机状态:
//Add a fighter object to array of fighters and set starting position
scene.CreatePlayerFighter(160.0f, 0.0f);
scene.CreateAIFighter(80.0f, 45.0f);
gameWolrd.Init(scene);
Renderer.Init(scene, window);
AI.Init(scene);
//etc...
//Game loop
while (true)
{
Input.Update(scene);
Physics.Update(scene);
AI.Update(scene);
//etc....
window.ClearBuffers();
Renderer.Update(scene, colorShaderProgram);
window.SwapBuffers();
}同样,在每个子系统内(渲染器、AI、输入等)所有的fighter组件都被传递到系统函数中,并返回新的值,然后再插入到fighter中:
void Physics::Update(Scene& scene)
{
for (Fighter& fighter : scene.fighters)
{
//Update fighter position based on fighter's current velocity which has been set by input
TransformComponent newFighterPosition = System::MoveFighter(fighter.GetComponent<TransformComponent>(), fighter.GetComponent<VelocityComponent>());
//Insert new TransformComponent to update fighter's position
fighter.Insert<TransformComponent>(newFighterPosition);
}
}这种当前的架构具有松散耦合的优点,因为我可以非常容易地添加和删除系统,而不会直接影响fighter类或它的组件。问题是,所有的东西都是无可救药的连续的,因为我的场景被一个接一个地传递到每个子系统来更新战斗机。我之所以提到这个问题,是因为我的一个想法是实现游戏逻辑层,让更高级别的类直接调用特定的游戏引擎系统函数,比如physics.MoveFighter(TransformComponent, VelocityComponent, float amountToMove);,我可以在其中添加额外的参数,让游戏逻辑层级别的用户有更多的控制权。当然,这样做意味着系统函数将以游戏逻辑的用户认为合适的任何顺序调用和调用。有没有办法仍然以这种方式实现游戏逻辑层,并可能将所有调用排队并重新排序,以便在游戏引擎中正确运行?或者有没有更好的方法来尝试在我当前的架构中实现游戏逻辑?
发布于 2017-09-12 23:19:32
还要确保您不想坚持使用现有的实体系统,如entityx。
https://stackoverflow.com/questions/45882808
复制相似问题