Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

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


...and also


C#
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:

C#
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.
Posted
Updated 29-Jul-17 6:29am
v4
Comments
BillWoodruff 28-Jul-17 22:12pm    
An interesting question since, usually, a "game" application is going to put speed/performance of its visual elements in response to user interaction ahead of everything else ... of course that "typical" game is also going to be using some raster rendering engine, like Unity.

Are you writing this game with WinForms ? or, WPF ?

See if you get any ideas from this example of a static "factory" class:

https://www.codeproject.com/Answers/1197724/Csharp-why-I-need-to-use-type-gettype-when-I-make
petter2012 29-Jul-17 4:17am    
Hi Bill,

Thanks for your (as always) well-put and informative reply. I found your reply in the link to be very interesting, though I think I might stick to the static class, as it does work after all, and isn't really abusing the OO concept (or so I think). I learned to program before OOP became a standard, and having worked with mainly WinForms and traditional ASP.NET, I have never had to really dig in to the more elaborate parts of the OO architecture.

Glad to see your comment on the game engine project. I thought that there would be little interest in (yet) another free 2D game engine. However, this game engine is NOT about speed/performance really. Instead, it's written for an educational setting (with no focus on OO obviously :) ). It's extremely flexible, as everything can control everything. For example, in my game called "Mario in Moria", Super Mario fires blue fireballs on goblins. The longer the fireballs get away from Mario, the paler they get (transparency changes). The damage to the goblin is in turn related to the opacity of the fireball. Also, since all objects have "hitpoints" (to use an old D&D term), everything can be damaged if so wanted. Thus, if Mario gets stuck in the Moria dungeon, he can always fire lots of fireballs to the stone wall to eventually destroy it.

A few months ago, my students (with very little programming knowledge) created all kinds of games including gravity, such as a soccer game, a few games where falling objects should be rescued, a space shooter, a "Pingu" game, and a great ninja fighter game. In the Ninja game, there were a lot of things going on (they too could fire shots), but performance was never a problem.

I plan to write an article about this game engine here on CodeProject, sometime after summer. The game engine was first written for UWP, but as we hadn't Win 10 installed at the university, I changed it to WPF. By design, there is really very little framework or language-specific code, so I guess that, for example, a Java developer could translate this very easily to Java, or perhaps just use things like the (rather basic) Physics file for inspiration.

In addition to education, I also think that this game engine could come in handy for game designers who may work with other people who do the real coding. In such a scenario, this game engine could be suitable for prototyping, especially for games that aren't traditional in type (since its best feature is its flexibility). Since the code is so short and comparatively easy, there would be no abandonware risk - it's not difficult to add one's own physics code or the like. BTW, I hope that others will be wanting to add their own methods to this game engine, so that it can thrive and become more powerful than what I can hope to accomplish myself.
BillWoodruff 29-Jul-17 12:42pm    
Hi, Petter, I'm glad you found my comment useful ! Back in 1983 I created the first computer education program for the French-American Bi-Lingual School, in San Francisco: I shared your experience of wonder and delight at the creativity of the students :)

Now that I have a better sense of what you are doing, I may post another response here based on the idea (bias ?) that for a prototyping app OOP design is a very good thing. cheers, Bill
petter2012 30-Jul-17 19:01pm    
Hi again Bill,

Thanks for sharing your story about your teaching experiences.

I am now in the process of writing the documentation for the game engine, and just covered the connection to the OO paradigm. I have a modest suggestion: why not take part in this project and make this the go-to game engine for educational settings? :) I used to teach my students the Physics Helper wrapper (by Andy Beaulieu) and the underlying Farseer physics engine, but this engine is so much easier to work with, yet more flexible. In fact, I covered the entire engine code in a two-hour lecture. What it doesn't have is elaborate collision detection, but then again, that isn't really the point of this particular game engine.

It's really coming along well, and if you want to I can send you a pre-release zip file of my most recent game produced with it.

/Petter

1 solution

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900