HTML and CSS Reference
In-Depth Information
this.x += this.vx * dt;
this.y += this.vy * dt;
var collision = this.board.collide(this,OBJECT_PLAYER);
if(collision) {
collision.hit(this.damage);
this.board.remove(this);
}
if(this.reload <= 0 &&
Math.random() < this.firePercentage) {
this.reload = this.reloadTime;
if(this.missiles == 2) {
this.board.add(
new EnemyMissile(this.x+this.w-2,this.y+this.h/2)
);
this.board.add(
new EnemyMissile(this.x+2,this.y+this.h/2)
);
} else {
this.board.add(
new EnemyMissile(this.x+this.w/2,this.y+this.h)
);
}
}
this.reload-=dt;
if(this.y > Game.height ||
this.x < -this.w ||
this.x > Game.width) {
this.board.remove(this);
}
};
The first change affects
baseParameters
. A couple of additional defaults need to be added to the enemy
to control the likelihood of firing and the speed at which the enemy can fire:
firePercentage
and
re-
loadTime
, respectively.
firePercentage
is a number against which a random number is checked. If the
random number is less than
firePercentage
, the enemy fires one or more missiles. Because this method is
called each step frame,
firePercentage
needs to be a relatively small number to prevent the enemies from
firing constantly.
Next is
reloadTime
and
reload
, which work exactly like their
PlayerShip
counterparts, preventing
missiles from being fired in rapid succession.
The code to actually fire missiles also matches the code from the player, except that based on the number
of missiles the
Enemy
has been configured with (
1
or
2
), the code needs to check whether to send one missile
firing from the center of the enemy or two missiles firing from the left and the right side of the
Enemy
. Much
like
PlayerShip
, the
Enemy
needs be prevented from firing missiles in rapid succession. To prevent this, the