public class Ship extends VisibleObject {
...
// power of our shots
int _power;
// maximum power
static final int MAXPOWER=3;
public Ship(MyGame world, KeyboardStatus kbd) throws Exception {
...
_power=0;
}
public void powerUp() {
_power++;
if (_power>MAXPOWER) _power=MAXPOWER;
}
...
}
Our ship starts with power zero (single shot) and goes to a maximum power of three (quadruple shots).
Next, we change the fire() method so it adds the correct number of bullets.
public class Ship extends VisibleObject {
...
public void fire() {
Bullet b = new Bullet(this);
_main._fire.play();
_main._bullets.add( b );
if (_power>0 && 0!=(_power&1)) {
b.x-=3;
b = new Bullet(this);
b.x+=3;
_main._bullets.add(b);
}
if (_power>1) {
b = new Bullet(this);
b.x -= 16;
b.y += 3;
_main._bullets.add(b);
b = new Bullet(this);
b.x += 16;
b.y += 3;
_main._bullets.add(b);
}
}
If power is zero then only the first add() is executed. Beyond that, if the number of bullets is even then we add a second bullet in the center. Beyond two bullets (if _power>1), we add a pair of more spaced bullets that seem to come from the wings of the ship.
And finally, when we get a powerup (this is detected in playLevel()), we need to call the new powerUp method.
..
// number of times we're hit by an enemy bullet this frame
int hitCount=0;
collision = com.jpemartin.jgt.CollisionDetection
.findCollisionListSlow(_enemyBullets, _shipList);
if (null != collision && !collision._a.isEmpty()) {
for (VisibleObject hit : collision._a) {
if (hit instanceof Bullet) hitCount++;
else if (hit instanceof Powerup) {
System.out.println("bonus!");
_ship.powerUp(); // <---------- this line is new
hit._alive = false;
}
}
}
Next: (later)