Game Development Reference
In-Depth Information
float offset;
if (shootTop){
offset = −15;
} else {
offset = 15;
}
CGPoint bCenter = CGPointMake(center.x + 20, center.y + offset);
CGPoint bToward = CGPointMake(gameSize.width, bCenter.y);
Bullet* bullet = [Bullet bulletAt:bCenter TowardPoint:bToward From:self];
[bullet setDamage:damage];
[gameController addActor:bullet];
shootTop = !shootTop;
lastShot = stepNumber;
}
if (stepNumber % 60 == 0){
[self incrementHealth:1];
}
if (lastStepDamageWasModified + 10*60 == stepNumber){
[self decrementDamage:gameController];
}
}
In Listing 12-20, we see the task
step:
, which is called for every step of the game. We do a number
of things in this task; primarily, we figure out if a
Bullet
should be shot and, if so, shoot it. But we
also regenerate our health and keep track of whether we are still under the influence of a
Powerup
.
To figure out if we should fire a
Bullet
, we see if
stepNumber
minus
lastShot
is greater than
stepsPerShot
minus
damage
. In this way, we fire more often if
damage
is higher, which happens when a
Powerup
has given the Viper extra damage.
To fire a
Bullet
, we figure out if the
Bullet
should come from the top or the bottom of the ship. Then
it is just a matter of creating the
CGPoints
that describe its motion. Lastly, we clean up by flipping the
value of
shootTop
and set
lastShot
to
stepNumber
.
In Listing 12-20, we also increase our
health
. This is done by calling
incrementHealth
every 60
frames. Calculating if we should decrement our damage is done by checking when our
damage
was
last increased and calculating if 600 frames (~10 seconds) have passed.
All in all, very little is required to get our Viper class to do what we want. Let's move on the Asteroid class.
The Asteroid Class
The Asteroids are the primary opponents in Belt Commander (presumably, the word
belt
in the title
refers to an asteroid belt). These Actors start on the right and travel to the left. When they reach the
left side, they start over again on the right. We have used the variations of the Asteroid class several
times in this topic, and they are well documented. However, the feature that makes the Asteroid
interesting is how it breaks up into multiple, smaller asteroids while shooting Particles. This code
was modified from previous examples, and is shown in Listing 12-21.