Excercise 5-9.
  1. In order for our defenses to actually hit enemy ships we need to implement some form of collision detection. Start by declaring a method HitTest on TEnemyShip with arguments X,Y.  This method should return True if the coordinates X and Y are within the rectangle defined by the position of the enemy ship and its width and height. Also create a boolean variable called Hit on TEnemyShip that is True when the ship has been hit.
  2. Now go to TFlare.StepAndDraw. Loop through the Enemies array in the EnemyShipClass unit (you will need to add this unit to the implementation uses clause), and call HitTest for each enemy. Create a Boolean variable called Hit on TFlare and assign the return value of HitTest to it.  If a collision occurs then exit the loop early since there is no reason to test for collisions with other ships.
  3. Back in the main project source file, modify DrawEnemies and DrawAllFlares so that enemies and flares are removed from the list when hit is true. Keep in mind that in OnEnterFrame, DrawAllFlares is called before DrawEnemies. Since a hit is detected in DrawAllFlares we should consider moving the code that destroys enemies above the Enemy.StepAndDraw call in DrawEnemies since an enemy may have been hit after the previous call to DrawEnemies.
  4. Of course, having the flare and enemy disappear is not very exciting. Let's use the flare effect to create an explosion. We'll start by creating a flares that radiate outward from the center of the ship. Pick some number of fragments (say 30 or 40) and create a loop that creates flares at the enemy's position with radial velocities of random magnitudes. The intensity should be around 60. The creation of these flares should occur inside of TFlare once a hit is detected. This makes sure they are created before DoConvolutionFilter (otherwise they would be ignored). Create a method on TFlare called ExplodeFlare which takes an argument Pos : TVector2D. Then call this method when a hit occurs inside of StepAndDraw. Also, and this is very important, we should create a Boolean variable called CanHit on TFlare that is set to True by default. Modify the collision detection segment of the code so that collision detection is done only if CanHit is True. Then set this to False when you create the explosion flares.
  5.   While this creates a cool effect. The explosions quickly fill up the screen. It would be better if the flares burnt out and then were removed from the Flares list. To do this, create a Boolean variable called BurnOut. Set this to False by default but set it to True for the explosion flares. Then in StepAndDraw, decrease the intensity by 5 on each step if BurnOut is True. Also for BurnOut flares we do not want to draw flare points.