import java.awt.*;
import java.applet.*;
import java.net.*;

/* Created by Will Logan, Sun. September 27, 1998
 * 6.837 Project #1, Sprite Demo
 * Requirements:
	-Your applet must use at least two different sprites (i.e. with different images). 
    -You must demonstrate your sprites by moving them around a playfield. 
	-Your sprites should be properly clipped to avoid the edges of the playfield. 
    -Your sprites must implement transparent pixels. 
    -Your sprites must implement and demonstrate a method that tests if a given playfield
	 coordinate overlaps a non-transparent pixel of the sprite at its current position. 
 */

public class SpriteDemo extends Applet implements Runnable
{
	//Strings for the names of the images
	private String cartman1_str = "cartman-1t.gif";
	private String cartman2_str = "cartman-2t.gif";
	private String cheesypoofs_str = "cheesypoofs.gif";
	private String cheering1_str = "cartman-cheering-1t.gif";
	private String cheering2_str = "cartman-cheering-2t.gif";
	private String bkgnd_str = "bkgnd.gif";
	//Size of the applet
	private int applet_width = 480;
	private int applet_height = 360;
	//The Playfield
	private Playfield playfield;
	//The animated sprites for the playfield
	private AnimatedSprite tracker_asprite, stationary_asprite, success_asprite;
	//The output image, prevents flickering
	Image output;
	//The bkgnd image, prevents reprocessing image every draw
	Image bkgnd;
	//The control thread for the applet
	Thread animate;
              
	//Starts the applet running
    public void start() {
        animate = new Thread(this);
        animate.start();
    }

	//Kills the thread
    public void stop() {
        animate = null;
    }

	//Renders the applet with a 100ms delay between execution cycles
	public void run() {
        while (true) {
            try {
                animate.sleep(100);
            } catch (InterruptedException e) {
            }
            renderPlayfield( );
        }
    }

	//Inits the Applet
	public void init() {
		//Resize the applet
		this.resize(applet_width,applet_height);
		//Setup the hunter :) asprite		
		tracker_asprite = new AnimatedSprite(getImage(getDocumentBase(),cartman1_str),0,applet_height/2);
		tracker_asprite.AddFrame(getImage(getDocumentBase(),cartman2_str));
		//Setup the cheesypoofs sprite
		stationary_asprite = new AnimatedSprite(getImage(getDocumentBase(),cheesypoofs_str),applet_width/2,applet_height/2);
		//Setup the asprite to play when he gets the cheesypoofs
		success_asprite = new AnimatedSprite(getImage(getDocumentBase(),cheering1_str),0,applet_height/2);
		success_asprite.AddFrame(getImage(getDocumentBase(),cheering2_str));

		//Init the Playfield with the bkgnd image and add the asprites
		bkgnd = getImage(getDocumentBase(),bkgnd_str);
		playfield = new Playfield(bkgnd);
		playfield.AddAnimatedSprite(tracker_asprite);
		playfield.AddAnimatedSprite(stationary_asprite);
		playfield.AddSuccessSprite(success_asprite);
		//Render the playfield for the first time
		renderPlayfield( );
	}

	//Sets up the output image and calls paint
	public void renderPlayfield() {
		//Reset the playfield with the bkgnd image
		playfield.fillImage(bkgnd);
		//Draw the sprites on the image and update their next positions
		playfield.Draw();
		//Grab the ouput image and paint it
		output = playfield.toImage();
		repaint();
	}

	//Overloads paint to prevent flickering by using output image
	public void paint(Graphics g) {
		g.drawImage(output, 0, 0, this);
	}

	//Updates overloads upadate to repaint
	public void update(Graphics g) {
		paint(g);
	}

	//Handler for the mouseDown event.
	//Resets the cheesypoofs image with the current mouse pos and sets cartman after it.
	public boolean mouseDown(Event e, int x, int y)
	{
		playfield.SetTarget(x,y);
		renderPlayfield();
		return true;
	}
}