Game Development Reference
In-Depth Information
As you can see, the header for the class
GameController
extends the class
UIViewController
. The
UIView
named
actorsView
is an
IBOutlet
and is the
UIView
that contains the views for each actor in
the game. We don't want to use the Property
view
as the root view for the actors because we may
want to have other views associated with a
GameController
, such as a background image or other
UIViews
placed on top of the game. We will take advantage of this setup in a future chapter. For
now, all you need to know is that the
UIView actorsView
is a sub-view of the Property
view
and will
contain all of the
actor
views.
In this listing, you also can see the now familiar
CADisplayLink
, as well as various collections.
The NSMutableSet actor stores all of the actors in the game. The
NSMutableDictionary
actorClassToActorSet
is used to keep track of specific types of actors. The two
NSMutableSets
,
actorsToBeAdded
and
actorsToBeRemoved
, are used to keep track of actors that are created during a
single step of the game and actors that should be removed after the current step.
The last field declared is the BOOL
workComplete
. This is used for debugging and will be explained
later in this chapter, when we look at how the
CADisplayLink
is used. In addition to the fields
declared in Listing 6-1, we see four properties. The first property,
stepNumber
, keeps track of the
number of steps that have passed since the game started.
CGSize gameAreaSize
is the size of the
game area in game coordinates. The BOOL
isSetup
keeps track of whether the
GameController
has
been set up yet. The last property,
sortedActorClasses
, is used to tell the
GameController
which
types of actors it should keep track of.
Setting Up
GameController
The use of all of the fields and properties in Listing 6-1 will be made clear as you inspect the
implementation of the class
GameController
. Let's start with the task
doSetup
, shown in Listing 6-2.
Listing 6-2. GameController.m (doSetup)
-(BOOL)doSetup
{
if (!isSetup){
gameAreaSize = CGSizeMake(1024, 768);
actors = [NSMutableSet new];
actorsToBeAdded = [NSMutableSet new];
actorsToBeRemoved = [NSMutableSet new];
stepNumber = 0;
workComplete = true;
displayLink = [CADisplayLink displayLinkWithTarget:self selector:
@selector(displayLinkCalled)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[displayLink setFrameInterval:1];
isSetup = YES;