C ++游戏,类设计和职责 [英] C++ game, class design and responsibilities

查看:105
本文介绍了C ++游戏,类设计和职责的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚读了一些在输入主题时出现的相关问题,因此我不再重复。

I've just read some related questions that came up when I typed the subject, so I'll try not to repeat those.

我最近开始重新访问一个学习项目我开始了大约两三年前 - 一个C大型的Mega Man引擎端口。是的,我使用翻录的精灵。我还使用一个游戏引擎库来绘制,音乐和输入。

I've recently started revisiting a learning project I started about two or three years ago - a C++ port of a Mega Man engine. Yes I used ripped sprites. I also am using a game engine library for drawing, music, and input.

我的原代码是暴行的。虽然它可以(但很少)被称为OO,它完全错过了这一点。我开始添加的东西,如接口和切出了很多重复的代码。有些事情我不确定,因为游戏设计有时非常复杂。

My original code was atrocious. While it could (but barely) be called OO, it missed the point entirely. I've starting adding things like interfaces and cut out a lot of repetitive code. There are some things I'm not sure about, because game design gets very complex at times.

代表我的游戏库的对象目前是全局的(我知道全局变量通常是坏的),因为许多对象可以依靠它在这里和那里的东西,如加载他们的艺术或音乐。在不将五十个参数传递给将直接使用它的所有事物的情况下,将该对象拉出全局范围的最好方法是什么?

The object that represents my game library is currently global (I know globals are usually bad) because many objects may rely on it here and there for things like loading their art or music. What's the best way to go about pulling that object out of global scope, without having to pass fifty parameters to everything that would otherwise use it directly?

下一个问题:大家都知道,巨人射了很多小白色的射弹。目前,Player对象负责他发射的Projectile对象,更新它们的位置等(字面上,在Player :: Update()方法中为每个镜头调用一次Projectile :: Update()方法)。这是错误的方法吗?我的第一个改进是所有这些对象实现一个DrawObject接口,所以我的游戏可以绘制一切。对更新做同样的事情意味着我控制远离Player的射弹,并给它一些更广泛的游戏对象。我对此犹豫的原因是,它感觉像上帝的对象反模式。还是我误会说反模式?还有额外的复杂性 - 如果他们离开可见屏幕,射弹死亡,所以任何调用更新弹丸将要求调用者访问屏幕对象。

Next question: As we all know, Mega Man shoots lots of little white projectiles. Currently, the Player object is responsible for the Projectile objects he fires, updating their position and such (literally, calling the Projectile::Update() method once for each shot, inside the Player::Update() method). Is this the wrong way to do it? My first improvement was have all of these objects implement a DrawnObject interface, so that my game can just draw everything. Doing the same thing for Updates would mean I take control of the projectiles away from Player and give it to some broader Game object. The reason I'm hesitant about this is that it feels like the God object antipattern. Or am I misunderstanding said antipattern? There is still additional complexity involved - the projectiles die if they leave the visible screen, so any call to update the projectile would require the caller to have access to the screen object.

这就是现在,我会回来更多的问题,当我到达他们。

That's all for now, I'll come back with more problems when I reach them. End of first post!

推荐答案

对于全局类,我将使用单例,然后调用Game :: GetInstance (),它将返回一个指向全局类的指针。

As far as making a class global I would use a singleton, then just call Game::GetInstance() which would return a pointer to the global class.

对于粒子,我一直处理游戏的方式是创建一个管理所有对象的类。在我的游戏主循环中,我将调用类UpdateObjects函数,它将通过它存储的项目列表,并调用每个更新函数。与Render和Collision相同。

As far as the particles, the way I've always handled games was to make a class that manages all the objects. In my games main loop I would call that classes UpdateObjects function which would go through a list of items it has stored and call each of there Update functions. Same thing with Render and Collision.

这篇关于C ++游戏,类设计和职责的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆