Code Design for my game

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
st0ph
Posts: 27
Joined: Sat Aug 04, 2012 3:00 pm

Code Design for my game

Post by st0ph »

Hello Irrlichters
For my final project this year, my professor suggested making a FPS game in which all its levels will happen in our school, I liked his suggestion and immediatly started working with my classmate, he worked on 3D modeling, we modeled our school with Blender, also an animated mesh for students
we looked over all the game engines and finaly we decided to use Irrlicht, and IrrKlang for sound
and for now I did some coding for the game, but it's all messy and unorganized :cry:
therefore I'm looking for some suggestion on how the code architecture should be, in a way that it handle the game events (graphics, sound, collision ..) and can send messages between the diffrent objects of the game ^^

I'll be very grateful for your help :roll:
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Re: Code Design for my game

Post by teto »

I hope your university isn't in the US 'cos if there is another killing like in columbine, your game might be pointed out.
Using trunk with mingw/gcc 4.6, Windows 7 64 bits driver opengl
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Re: Code Design for my game

Post by sudi »

+Just remember to use design patterns....
+and if someone else reads your code and is going WTF....its probably not that good of a design^^ same goes for you when reading old code.
+another good hint is...if you have to scroll through a function its probably to big and the code could be redesigned.
+next thing is use stack variables when possible and not the heap.
+use defined data structures! Not losely coupled data in functions or classes.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Code Design for my game

Post by CuteAlien »

The main trouble you will have is probably that it will take more time than you expect. So the architecture has to be simple - you won't have to think too much about it being extensible and not even too much about resource de-allocation (just load one level and keep it in memory). Think first about the menu-states you need, for example you might have 3: main menu, in-game and score-screen (or death-screen - or maybe just show score in main-menu). Using one state-variable for that is sufficient, but make sure you set it with a setter (setGameState()), so you can put all stuff that needs to be changed when switching between states in there (it's basically a very, very simple state-machine without any complicated overhead).

Then work with one global application class. It's has easy access to other classes (like Irrlicht). Other classes you will need are - one per game-state (main menu, ingame). Each of those state-classes has an own event-receiver. You can either switch that when switching between states or you have one global event-receiver in your app-class which depending on the state-variable calls the event-receiver of that state-class.

One class for player input (keystates, mouse-state).

One class for your avatar (movement-speed, lives, weapon-state, maybe animation-state (living/dying/dead)).

One class for your enemy objects (put a few static things in your levels which will just shoot straight as soon as the player is within range, it's the easiest to start with and it's not likely you will find much time to implement more complicated enemies. If you find more time just add a enemy-behaviour variable in that and make your update do different stuff depending on that).

One for logging (a class for which you have one global object with a simple name like LOG which has a function to print out stuff in debug). You can use Irrlicht's logging class for a start, I just mention it because people starting out often don't realize how useful logging is :-)

Your application class has an init function (load the level, create all the objects). It has an update function (call update functions for the other classes like ingame and menus). And it has a function to destroy everything again at the end.

Put the game-logic in an own ingame class. It has for example a startGame() which is called when the state is switched to ingame. It has an update function (called from the updated function of the application class) where does stuff like collision-checks and maybe remove lives from your avatar and plays some sound-samples while doing so. And once player-lives are out it calls the applications classes setState and put's it into a end-game screen (or back to main-menu).

Generally - for a first project it's fine if it works. Everything else you learn over time :-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Dareltibus
Posts: 115
Joined: Mon May 17, 2010 7:42 am

Re: Code Design for my game

Post by Dareltibus »

Why no one make a irrlicht game-example using the above CuteAlien's suggestion?

A lot can be learned from his suggestions^^
st0ph
Posts: 27
Joined: Sat Aug 04, 2012 3:00 pm

Re: Code Design for my game

Post by st0ph »

@teto: actually I'm from Morocco and my game is an educational game, there will be no violence, at least for now :twisted:

@Sudi: thanks, those are some really good advices, I'll keep them in mind ^^

@cuteAlien: your comment is completly appreciated and very helpful, but as @Dareltibus it would be very clear if there is a concrete version/game/template that explains what you said in an easy way !

if there is any help or propositions I'll be very grateful :roll:
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Code Design for my game

Post by randomMesh »

You could have a look at the code in my signature. It's not the best, but maybe you can get some ideas for an OOP based game design. Feel free to steal all the code you need. Good luck!
"Whoops..."
st0ph
Posts: 27
Joined: Sat Aug 04, 2012 3:00 pm

Re: Code Design for my game

Post by st0ph »

thanks @randomMesh, your code was realy helpful, I will mention you in my thesis, and I'll send a copy of my game when I finished if you want, of course :)
Post Reply