Okay, I dunno how that will work in a post. Everything is not totally MVC since Cocos2D is not really designed for it.
It's basically designed around the schematic there:
http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/images/app-archa2.gif
In my architecture, the model doesn't notify the view of changes since it's a game and I have game loops for that.
I have menus that are scenes. and a gamecontroller that is basically a scene too. The game controller (scene) has two properties and that's all:
@interface GameController : Scene <GameUIDelegate>{
GameModel *model;
GameView *view;
}
One method for init:
- (id) initWithLevelNumber:(NSUInteger) levelNumber;
And a few method so that the view can notify the controller:
- (void) saveGame;
- (void) timeOver;
- (void) quitLevel;
- (void) pauseGame;
...
The model also has a notification for saving on quit (basically calls pause and savegame:
- (void) applicationWillTerminate:(NSNotification *) notification;
Finally the controller has a game loop where it checks for collisions, spawns entities, counts score, moves entities ...
The view has a delegate (the controller, for passing touches) a reference to the model and of course layers and an array of sprites, animations, the pause menu, buttons etc.
When the user pauses or quits or does a touch, the view processes the event in a form that can be understood by its delegate and messages back. Only the controller changes the model or switches scene, the view only displays graphics and processes touches.
The view also has a game loop that goes through each entity in the model and updates its sprites accordingly.
The model is basically a bunch of arrays with NSCoding and a few floats and int with NScoding. No methods there apart from the initalisation for the entities:
- (id) initWithLevelNumber: (NSUInteger) levelNum;
- (void) addEnt:(Ent*) ent position:(CGPoint) position;
- (void) removeEnt:(Ent*) ent;
- (BOOL) archiveToFile:(NSString *)archivePath;
+ (id) unarchiveFromFile:(NSString *) archivePath;
Note that the position of the entities is not the position of the sprites. That's one of the advantages of MVC, the model and controller do all the work in cartesian coords, much simpler, and the view projects in isometric.
I am not very happy that the menus and the game controller have to switch scenes themselves. It would be nicer if we has a Worflow Manager or something that would manage all the scene flow that is a delegate to all the scenes.
I am contemplating making a MVC fork of Cocos if nobody is intersted in having Cocos for MVC compliant like Cocoa.