Game Development Reference
In-Depth Information
Here, the protocol Representation is defined. Any object that is to manage the UIView associated
with an actor must conform to this protocol. The idea here is that there are at least two kinds of
UIViews used to draw an actor: those based on images and those based on programmatic drawing.
There can be other ways to implement a UIView that represents an actor, such as a UIView with
sub-views. We don't cover that example in this chapter, but we do look at image-based actors in this
chapter and vector-based actors in the next chapter.
Regardless of how an actor's UIView is implemented, the class GameController is going to have
to know how to get a reference to the correct UIView and give the Representation the chance
to update how it looks. Hence, we have the two required tasks : getViewForActor:In : and
updateView:ForActor:In :.
In the preceding listing, we see a second protocol named Behavior. We are going to create some
LinearMotion that describes how an
applyToActor:From : that
GameController for every step of the game.
Actor class as reusable as possible. The two properties added and removed are used to keep
NSMutableArray containing objects that conform to the protocol
behaves. This can be as simple as describing the actor's motion or as sophisticated as artificial
intelligence. We provide a special task called addBehavior :, which is shorthand for getting the
NSMutableArray behaviors and adding a Behavior object. The addBehavior : task also gives us a
chance to lazy-load the NSMutableArray behaviors so we don't create it if we don't need it.
You can also see in Listing 6-9 that we added a variant and state property. It is very common for
actors to require these two properties, so they are included in the class Actor for simplicity. The
property variant can be used to describe actors that are the same class, behave the same, but are
perhaps different colors—for example, the different color ghosts in Pac-Man. The property state
is used to track the different states an actor may have during its lifetime. Using the ghost example
again, the states might include in jail, pursuing, and running. The last property is alpha, which is used
to describe the transparency of the actor on the screen.
The tasks defined in Listing 6-9 are basically the same as the task described in the preceding
chapter for the class Actor . The only real difference here is that when an actor is initialized, a
Representation must be passed-in. We have also added a utility task, randomPointAround:At :, which
is used to create a random point on a circle of aRadius size around point aPoint .
Implementing Actor
The implementation of the class Actor is shown in Listing 6-10.
 
Search WWH ::




Custom Search