C#游戏引擎中的静态或非静态体类 [英] Static or non-static bodies class in C# game engine

查看:134
本文介绍了C#游戏引擎中的静态或非静态体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了一个简单的C#游戏引擎,现在正在考虑代码的OO结构。



所有身体 - 如玩家宇宙飞船,敌人宇宙飞船,小行星等 - 都放在List< Body>中,并驻留在一个名为Bodies的类中。



我的直觉说大多数会使Bodies类成为普通类并像往常一样继承它。但是,由于游戏中只有一个这样的列表,我已经将类设置为静态,然后让所有其他类通过编写类似



之类的东西来使用它。

  float  theWidth = Bodies.bodylist [ 72 ]。宽度; 





...还有





 Bodies.bodylist [ 72 ]。宽度=宽度; 







目前,我正在考虑让bodylist只能通过Bodies类中的方法进行更改,而不是直接来自其他类(封装)。我通过以下方式设法做到了这一点:



 静态 < span class =code-keyword> private  List< Body> bodylist; 

static public 列表< Body> Bodylist
{
get { return bodylist; }
private set {bodylist = value < /跨度>; }
}





......但我仍然想知道这是否是首选方式。 />


那我该怎么办?我可以删除static关键字,让其他类继承Bodies类,但在我看来,它在概念上是错误的。此外,它可能会导致问题,因为Controller类,Physics类和BoardSetup类都会对bodylist起作用。请帮帮我吧!



(我还有一个Variables类,一个Utilities类和一个Sounds类。它们也可以设置为静态或非静态,并且那里的主要推理可能是相同的,或者我错了?我认为至少Variables类必须设置为静态。)



我是什么尝试过:



我已经尝试了几种解决这个问题的方法,但不能真正解决它,因为我不知道什么是最通常首选的解决方(代码工作正常,它只是不是很好。)



我也在这里阅读OriginalGriff的优秀解释:

[ ^ ]但在该示例中,我猜母亲类应该是静态的。在任何情况下,在我的游戏中只有一个Sounds.cs,一个Bodies.cs等。

解决方案

其实比尔在上面评论中的回答回答了我的问题

Hi,

I've created a simple C# game engine and am now thinking of the OO structure of my code.

All "bodies" - like player spaceships, enemy spaceships, asteroids, etc. - are put in a List<Body>, and resides in a class called Bodies.

My gut says that most would make the Bodies class a "normal" class and inherit it as usual. However, as there will be only one such list in the game, I have set the class to static, and then let all other classes use it by writing things like

float theWidth = Bodies.bodylist[72].Width;



...and also


Bodies.bodylist[72].Width = theWidth;




At the moment, I am thinking of having the bodylist only being able to be altered through methods in the Bodies class, and not directly from other classes (encapsulation). I have managed to do that via the following:

static private List<Body> bodylist;

static public List<Body> Bodylist
{
    get { return bodylist; }
    private set { bodylist = value; }
}



...but am still wondering if this would be the preferred way.

So what should I do? I could delete the static keyword and let the other classes inherit the Bodies class, but in my mind it's conceptually wrong. Also, it might cause problems as both a Controller class, a Physics class, and a BoardSetup class all work against the bodylist. Please help me out!

(I also have a Variables class, a Utilities class, and a Sounds class. They too could be set to either static or not, and the principal reasoning would probably be the same there, or am I wrong? I think at least the Variables class MUST be set to static.)

What I have tried:

I have tried several approaches to this problem, but can't really solve it as I don't know what would be the most commonly preferred solution. (The code worked fine as it was, it just wasn't very OO.)

I also read OriginalGriff's excellent explanation here:
[^] but in that example, I guess that the "Mother" class should be static. In any case, in my game there will only be one Sounds.cs, one Bodies.cs, etc.

解决方案

Actually Bill's answer in the comment above answered my question.


这篇关于C#游戏引擎中的静态或非静态体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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