Firing the Beam
The FireBeam thread is started from updateScene() like so:
new FireBeam(intercept, this, laser, explsClip, turnAngle).start();
A basic question is why use a thread? One answer is that by passing the job of beam delivery and explosion to a new thread, the ShootingBehaviour object is free to do other tasks. For example, it could show a puff of animated smoke rising from the gun's cone or have the cone recoil slightly. Shooter3D doesn't do these things, as that would further complicate the example. At present, the threaded implementation allows updateScene() to process a user's new pick selection once the finishedShot Boolean has been set to true
near the end of run() in the FireBeam thread:
public void run()
{
laser.shootBeam(intercept);
shooter.setFinishedShot(); // beam has reached its target
explsClip.showExplosion(turnAngle, intercept); // boom!
}The call to setFinishedShot() sets finishedShot to true, which permits updateScene() to respond to user clicks and, simultaneously, the explosion for the current beam will be initiated from FireBeam. This improves the responsiveness of the application since the explosion animation lasts one to two seconds.
However, there's a problem: what if the explosion animation for the beam (i.e., the current call to showExplosion()) doesn't finish before the FireBeam thread for the next beam calls showExplosion() again? The worst that happens is an interruption to the explosion animation and the truncation of the playing of the sound. ...