Game Development Reference
In-Depth Information
Listing 12-18. Viper.h
enum{
VPR_STATE_STOPPED = 0,
VPR_STATE_UP,
VPR_STATE_DOWN,
VPR_STATE_COUNT
};
@interface Viper : Actor <ImageRepresentationDelegate, LinearMotionDelegate>{
LinearMotion* motion;
long lastStepDamageWasModified;
}
@property (nonatomic) float health;
@property (nonatomic) float maxHealth;
@property (nonatomic) long lastShot;
@property (nonatomic) long stepsPerShot;
@property (nonatomic) BOOL shootTop;
@property (nonatomic) int damage;
+(id)viper:(GameController*)gameController;
-(void)setMoveToPoint:(CGPoint)aPoint within:(GameController*)gameController;
-(void)incrementHealth:(float)amount;
-(void)decrementHealth:(float)amount;
-(void)incrementDamage:(GameController*)gameController;
-(void)decrementDamage:(GameController*)gameController;
@end
In Listing 12-18, we see the header file for the class
Viper
. In this file, we see that we define an
enum
with three states. This is very much like the
enums
we have declared in the past to specify the state
of an
Actor
. Looking at the properties, we can see that we have a property for
health
and
maxHealth
.
We also have a property called
lastShot
, which will be used to keep track of whether it is time to
shoot another
Bullet
in conjunction with the property
stepsPerShot
. The
BOOL
property
shootTop
is
used to keep track if the next
Bullet
should come from the top of the
Viper
or not. The last property
damage
indicates the damage value of the next
Bullet
.
The tasks in Listing 12-18 are pretty mundane. There are four tasks for incrementing and
decrementing heal
t
h and
damage
. Each keeps the value of
health
and
damage
within a reasonable
range. The interesting task is
setMoveToPoint:within:
, as shown in Listing 12-19.
Listing 12-19. Viper.m (setMoveToPoint:within:)
-(void)setMoveToPoint:(CGPoint)aPoint within:(GameController*)gameController{
if (motion){
[[self behaviors] removeObject: motion];
}
CGPoint point = CGPointMake([self center].x, aPoint.y);