Game Development Reference
In-Depth Information
Listing 12-21. Asteroid.m (doHit:)
-(void)doHit:(GameController*)controller{
if (level > 1){
int count = 1;
float percent = arc4random()%1000/1000.0f;
if (percent > 0.9){
count = 3;
} else if (percent > 0.5){
count = 2;
}
for (int i=0;i<count;i++){
float radius = [self radius];
float rx = arc4random()%1000/1000.0*radius*2 - radius;
float ry = arc4random()%1000/1000.0*radius*2 - radius;
CGPoint newCenter = CGPointMake(self.center.x + rx, self.center.y + ry);
Asteroid* newAst = [Asteroid asteroidOfLevel:level-1 At: newCenter];
[controller addActor:newAst];
}
}
int particles = arc4random()%4+1;
for (int i=0;i<particles;i++){
ImageRepresentation* rep = [ImageRepresentation
imageRepWithDelegate:[AsteroidRepresentationDelegate instance]];
Particle* particle = [Particle particleAt:self.center WithRep:rep Steps:25];
[particle setRadius:6];
[particle setVariant:arc4random()%AST_VARIATION_COUNT];
[particle setRotation: (arc4random()%100)/100.0*M_PI*2];
LinearMotion* motion = [LinearMotion linearMotionRandomDirectionAndSpeed];
[particle addBehavior:motion];
[controller addActor: particle];
}
[controller removeActor:self];
}
In Listing 12-21, we see the task doHit: as implemented by the class Asteroid . In this task, we
check to see if the Asteroid we are working with is above level one. If it is, then we need to create
sub- Asteroids to replace this one. In other examples, we just randomly picked a number of child
Asteroids to create. However, while play-testing Belt Commander, this was unsatisfactory because
the number of Asteroids generated was way too high. To reduce the number of Asteroids created,
a percentage was given for each possible value of count . So only 10 percent of the time an Asteroid
will create three children Asteroids ; 40 percent of the time there will be two children Asteroids ; the
rest of the time only a single Asteroid is created. In this way, the game is balanced, keeping it fun
and playable.
Search WWH ::




Custom Search