Best architecture for a game

Hello,

I’ve developed some games in cocos2d and now i’m creating my first game in cocos2d-x.
I’m usually divide my code in folders related to the class types: scenes, nodes, sprites, and a last folder called models where i put all the classes that dont inherit nothing from cocos2d library (like code for save data in the phone, code related to monetization etc).

What’s the best architecture to organize the game developing? What’s the architecture that you use guys?

Regards

What is best is probably a personal preference. We typically divide all different subsystems into different folders in the classes folder, like input, game logic, resource management, platform specific etc…

Hello Digiment,

Can u describe a bit more your architecture? By what you’ve said, i think that i’ll like this design way.

Regards

I don’t really divide to much. Perhaps if a piece of the app has a number of components, I might. Otherwise I just toss everything into a single folder and name my classes accordingly so everything lines up. I do divide my Resources up into folders, however.

I know that this is a theme guided by personal preference. But what i’m trying to find is a good practice to do that, to have a game well organized.

What more folders you considerer important in addition to these: input, game logic, resource management, platform specific ?

Regards

I’m guilty of huge sprawling codebases with many custom UI components, so I separate mine more aggressively. I’d focus on letting the needs of your game/app dictate your structure – you don’t want to overdo it.

Hi mistic,

this also depends a bit on how you create your game, i.e. what funcationality and features you add on top of Cocos2d-x (not talking about modifying Cocos2d-x code). We for example implement our own input system which we wrap around the cocos2d-x input mechanism. In addition, over the years we have developed a number of internal tools which we have adapted to the Cocos2d-x engine and henece have our own animation system, texture management etc which are all wrapped around the Cocos2d-x engine. If you are not too sure on how to do this and how you divide your code into different subsystems, my suggestion is to not put to much thought down into it and instead focus on developing the game :slight_smile:

However, the bottom line is that we try to decouple as much as possible and divide everything into different subsystems where it is applicable to do so.

Cheers

I have folders for:
Entities - these are my ‘top level’ classes e.g. PlayerEntity, BaddyEntity, CollectableEntity all inheriting for a BaseEntity
Layers - These are all my layers (duh!) I have a World layer which draws the Physics world (I use Box2d) a BackgroundLayer that handles pretty backgrounds, a TouchLayer that handles the touch events, a HUD layer and a couple of layers for menus and debugging stuff
GameObjects : these are all my objects that are used by Entities. I have PhysicalObject and VisibleObject classes - the former exists in the physics world (essentially wrappers for Box2d objects) while the latter contains a sprite and animation data. Using these allows me to separate main game logic from the implementation - essentially I could replace Box2d in the PhysicalObject class and the rest of the game would be none the wiser.
GameControl : I modify the AppDelegate to instantiate a singleton GameController that lives here. This class controls the main game logic. This folder also contains a Utils class containing a whole bunch of utility functions like converting between Physical World and Screen coordinates, and a Globals.cpp that is used to contain global game information (such as a reference to each layer, a collection of current Entities, the current location within the World etc.)
I also have a separate Sounds folder where any sound logic goes - this will (when I make it more multi platform) be where the different sound engine implementations are abstracted.
I think it’;s a very personal thing, but I try to look at it outside of the Cocos2d-x framework - so it would be structured similarly regardless if I changed to some other engine (not that I would, Cocos2d-x rocks!)