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.