package examples.collision;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.net.URL;

import fang.*;


/**Demonstrates how to test for collisions.
 * A sprite moves from the left side of the
 * screen to the right side of the screen
 * until it intersects the target.  When they
 * collide, the target is removed from the canvas.
 * @author Jam Jenkins
 */
public class Collision extends GameLoop
{
	/**the moving sprite*/
	private Sprite bullet;
	/** the stationary sprite*/
	private Sprite target;

	/**makes and adds the sprites
	 * to the canvas*/
	public void startGame()
	{
		makeSprites();
		addSprites();
		setHelp("resources/CollisionHelp.txt");
	}

	/**initializes the sprites and
	 * adds a tracker to the bullet*/
	private void makeSprites()
	{
		target=new RectangleSprite(1, 1);
		target.setScale(0.1);
		target.setLocation(0.9, 0.5);

		bullet=new OvalSprite(1, 1);
		bullet.setScale(0.1);
		bullet.setLocation(0.1, 0.5);

		Point2D.Double velocity=
		    new Point2D.Double(0.1, 0);
		ProjectileTracker tracker=
		    new ProjectileTracker(velocity);

		bullet.setTracker(tracker);
	}

	/**adds the sprites to the canvas*/
	private void addSprites()
	{
		canvas.addSprite(bullet);
		canvas.addSprite(target);
	}

	/**tests for collision at each frame.
	 * If the bullet and the target 
	 * intersect, then the target is 
	 * removed from the canvas.
	 * @see fang.FrameAdvancer#advanceFrame()*/
	public void advanceFrame(double timePassed)
	{
		if(bullet.intersects(target) &&
		        canvas.containsSprite(target))
		{
			canvas.removeSprite(target);
			canvas.removeSprite(bullet);
		}
	}

	public static void main(String[] argv)
	{
		new Collision().runAsApplication();
	}
}

