Game Development Reference
In-Depth Information
-(void)step:(GameController*)controller
: The task
step:
is called once per
game step by the
GameController
. Subclasses of
Actor
can provide custom
logic in their implementation of this task.
-(BOOL)overlapsWith: (Actor*) actor
: The task
overlapsWith
: is used to test if
an
Actor
occupies any of the same space as another
Actor
. This can be used to
implement simple collision detection.
-(void)addBehavior:(NSObject<Behavior>*)behavior
: The task
addBehavior
: is
a utility method for adding
Behaviors
to an
Actor
in a single step.
Representation
Each
Actor
has to describe how the
GameController
should render it. The protocol
Representation
(defined in Actor.h) describes how a
GameController
can get and update a
UIView
for each
Actor
. There are two concrete implementations of
Representation
:
ImageRepresentation
and
VectorRepresentation
. The class ImageRepresentation uses
UIImages
and
UIImageViews
to create
a
UIView
suitable for presenting the
Actor
. The class
VectorRepresentation
draws the
Actor
dynamically using Core Graphics. The details for how
ImageRepresentation
are covered in Chapter 6,
while the implementation of
VectorRepresentation
is covered Chapter 7. There are two tasks defined
by the protocol:
-(UIView*)getViewForActor:(Actor*)anActor In:(GameController*)
aController
: This task is called once shortly after an
Actor
is added to a
GameController. It is responsible for creating and returning a
UIView
suitable for
rendering the
Actor
.
-(void
)
updateView:(UIView*)aView ForActor:(Actor*)anActor
In:(GameController*)aController
: This task is called whenever an
Actor's
needsViewUpdated
property is true. This task should make any changes to the
UIView
required to make it accurately represent the
Actor
.
Behavior
When creating a game, a lot of different Actors will require very similar code to implement. While it
is definitely possible to construct a class hierarchy that provides just the right code for each class, I
find this cumbersome. One solution is the Behavior protocol and the classes that implement it. Each
instance of the protocol Behavior is responsible for applying some logic to an Actor, one per step.
The protocol Behavior defines a single task:
-(void)applyToActor:(Actor*)anActor In:(GameController*)gameController;
The task
applyToActor:In:
is implement by the classes that conform to this
protocol to provide some sort of behavior to the Actors they are attached
to. There are several implementations of Behavior included in the game Belt
Commander; those are:
LinearMotion
: The class
LinearMotion
is used to move an Actor in a straight line
during the game. The class provides several utility constructors to make it easy
to define the motion based on a direction or a target point.