Game Development Reference
In-Depth Information
Listing 7-17. Asteroid.m (asteroid:)
+(id)asteroid:(GameController*)acontroller{
CGSize gameSize = [acontroller gameAreaSize];
CGPoint gameCenter = CGPointMake(gameSize.width/2.0, gameSize.height/2.0);
float directionOffScreen = arc4random()%100/100.0 * M_PI*2;
float distanceFromCenter = MAX(gameCenter.x,gameCenter.y) * 1.2;
CGPoint center = CGPointMake(gameCenter.x + cosf(directionOffScreen)*distanceFromCenter,
gameCenter.y + sinf(directionOffScreen)*distanceFromCenter);
return [Asteroid asteroidOfLevel:4 At:center];
}
In Listing 7-17, we see the task
asteroid:
, which creates a new
Asteroid
of size 4 by calling the
other constructor. Most of the work being done is simply finding the starting point for the
Asteroid
off screen. This is identical to the code used to find a point for the
Powerup
actor in Chapter 6. The
details can be found in Figure
7-3
. The second constructor for
Asteroid
is shown in Listing 7-18.
Listing 7-18. Asteroid.m (asteroidOfLevel:At:)
+(id)asteroidOfLevel:(int)aLevel At:(CGPoint)aCenter{
ImageRepresentation* rep = [ImageRepresentation
imageRepWithDelegate:[AsteroidRepresentationDelegate instance]];
[rep setBackwards:arc4random()%2 == 0];
if (aLevel >= 4){
[rep setStepsPerFrame:arc4random()%2+2];
} else {
[rep setStepsPerFrame:arc4random()%4+1];
}
Asteroid* asteroid = [[Asteroid alloc] initAt:aCenter WithRadius:4 + aLevel*7
AndRepresentation:rep];
[asteroid setLevel:aLevel];
[asteroid setVariant:arc4random()%AST_VARIATION_COUNT];
[asteroid setRotation: (arc4random()%100)/100.0*M_PI*2];
float direction = arc4random()%100/100.0 * M_PI*2;
LinearMotion* motion = [LinearMotion linearMotionInDirection:direction AtSpeed:1];
[motion setWrap:YES];
[asteroid addBehavior:motion];
return asteroid;
}
In Listing 7-18, we see the main constructor of the class
Asteroid
. We also see that we create the
Asteroid
with a radius based on the value of
aLevel
. In this way, we have progressively smaller