views:

132

answers:

2

I'm developing a puzzle game application - watch on youtube - for iPhone, and the actual "in-game" part is almost done. It is a separate Class (subclass of UIView) what initializes with a puzzle clue, puzzle pieces, and is ready to send a message for somebody if the puzzle has solved ("completeness" check invoked on every touchesEnded).

Now I'm troubled how to design the whole application pattern programatically.

The game needs a main menu view, a puzzle selector view, fromwhich I can "create" puzzleLevel instances, I have to store the actual puzzle data in a separate class (I suppose), maybe in archive files, and need a preferences view, inwhich I can change the "global" variables that every puzzleLevel instance should use (angular snap values, skins, etc.).

I can feel that I have to do something with the main viewController what controls all the views I've mentioned above, but I don't know how to do it exactly. Where should I store global variables? Where should I store puzzle data? How should I report the "puzzle completeness", and who for should I report? How should I design the view hierarchy?

I'm wondering if somebody could show me some concept, or just a link where I can get along. I'm interestend in concepts mainly, the actual coding part can be "googled" after.

+2  A: 

Typically my games have an App object at the top, which owns one of several AppStates (menu, selector, preferences, etc) and switches between them as required, in a pretty typical usage of the State pattern. These states handle their own rendering and input and store whatever resources they need. The App object also owns any global application-wide settings and objects that are shared across the states (eg. rendering, sound). These may be passed in to the states individually, or the states could request the relevant interfaces back from the App at some point.

One of the AppStates will be the game playing state, and that will contain the definition of the current puzzle, plus the current state of this play session (eg. how completed it is). I would tend to still have a separate Game class that is owned by the relevant GamePlayingState, since the former would contain only game logic information and the latter handles input/output.

Kylotan
Thanks for the answer.Is it somethig like I use an instance of a custom compactTangramViewController:UIViewController class (to be the App Object), with instance variables (@property-s) of all the views, and the pref-variables?It is not clear, how to "wire up" all together.How can a state request the puzzle selector view to come-in (how can it tell the controller to get the selector-view in)? How can a puzzle selector tell the controller to init a new puzzle? How can a puzzle get the actual - e.g. - angleSnapDegrees from the controller?...
Geri
...Should I subclassing everything from an AppController:NSObject?A Puzzle selector should be a puzzleSelector:AppController?Or an instance variable of the AppController?I'm fine with tackling problems within a class, but organizing them seems a bit more difficult to me yet.All-in-all:Could you post me a sketch of the class hierarchy/inheritance of the idea?
Geri
May I write these to a new question? ...
Geri
Somthing like this > http://gpwiki.org/index.php/State_pattern ?
Geri
Moreover, - http://www.codeproject.com/KB/architecture/statepattern3.aspx - like this? Seems promising, even if I have no idea how to implement all of these... Now comes the reading part...
Geri
If a viewController runs an NSTimer, it can collect/read any data from it's subviews, and redirect/update/etc. them as it needed.Maybe it can be a solution, but I hope there are smarter ways to do this (an "invoking on user-interactions only"-way). And I still have no idea about the global variables.
Geri
A dialogue with myself... :)
Geri
I can't give you Objective-C advice, just the general overview of the structure. You asked for design patterns, so make sure you're familiar with them - the State pattern is a commonly used one and you can read about it in several places.
Kylotan
Ya, ok, thanks anyway (I've tagged my question iPhone and Objective-C). But I think there is a more Objective-C suitable method, hope I'll find it. Anyway, understanding/hearing about the state pattern wasn't worthless, for sure.
Geri
A: 

I think I should use the NSNotification class. It is simply set up a "listener" in the object (viewController) that contains the subviews, then subviews can send notifications to the controller. Then the notification handler can invoke any methods.

viewController part:

-(void) viewDidLoad
{   
//Set up a listener.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"finishedCurrentLevel" object:nil];   
...
}

-(void) notificationHandler: (NSNotification*) notification
{
//Notification handling.
if ([notification name] == @"finishedCurrentLevel") [self finishedCurrentLevel];
}

-(void) finishedCurrentLevel
{
//View managing code here...
}

The notification, the listening and the "responds" for the notifications set up this was. The actual notifying goes like this (can be executed from any subviews):

[[NSNotificationCenter defaultCenter] postNotificationName:@"finishedCurrentLevel" object:nil];

It solves my "communication" problem, I think.

About globals I simply created a separate globals.m file with the coressponding globals.h without defining any class. They just "attach" some extern variables, so I can reach them from any file having globals.h imported.

Geri