import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import Raster;
import java.net.*;

public class Project1 extends Applet implements Runnable{

  // Variables
  Playfield background;
  AnimatedSprite ghost;
  AnimatedSprite frog;
  Image output;
  Thread animate;

  public void init() {
    // get images, put in rasters
    Image ghostly = getImage(getDocumentBase(), "ghostly.gif");
    ghost = new AnimatedSprite(ghostly, 20, 4);
    Image frogger = getImage(getDocumentBase(), "frogger.gif");
    frog = new AnimatedSprite(frogger, 5, 2);
    Image bkgrnd = getImage(getDocumentBase(), "bkgrnd.jpg");
    background = new Playfield(bkgrnd, 2);
    background.addSprite(0, ghost);
    background.addSprite(1, frog);
    ghost.setPosition(50, 50);
    ghost.setDxDy(5, 5);
    ghost.addTrack(0, new int[] {1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2});
    ghost.addTrack(1, new int[] {11, 12, 13, 12, 11, 12, 13, 12, 11,
				   12, 13, 12});
    ghost.addTrack(2, new int[] {4, 5, 6, 7, 8, 9, 10});
    ghost.addTrack(3, new int[] {14, 15, 16, 17, 18, 19, 20});
    ghost.setNewRaster(1);
    ghost.setTrack(0);
    frog.setPosition(200,200);
    frog.addTrack(0, new int[] {1, 2, 1, 4, 1, 2, 1, 5});
    frog.addTrack(1, new int[] {1, 3, 1});
    frog.setNewRaster(1);
    frog.setTrack(0);
    //background.Draw();
    //output = background.returnImage();
    //render();
    start();
  } // end init

  public void start() {
    animate = new Thread(this);
    animate.start();
  }

  public void stop() {
    animate = null;
  }

  public void run() {
    while (true) {
      try {
	animate.sleep(100);
      } catch (InterruptedException e) {
      }
      render();
    }
  }

  public void render() {
    // more stuff to go here
    /* 
	Here is a listing of what was to go into this render() method:
	Cases for each sprite, including
	    - whether or not the ghost reached the edge of the screen,
		where he would splat (tracks 2 and 4) and reverse direction
	    - whether the ghost's x-position was near the frog's x-position,
		where the frog would shoot it's tongue (track 2), but only
		if it were not in the jumping frame.
	    - would like to have included alpha-channelling the ghost to make
		it translucent, but that was a last item idea. 
    */
    background.Draw();
    output = background.toImage();
    repaint();
  }

  public void onMouseClick(int a, int b) {
    int diffx = java.lang.Math.abs(a - ghost.getX());
    if (a < ghost.getX())
      diffx = -diffx;
    int diffy = java.lang.Math.abs(a - ghost.getY());
    if (b < ghost.getY())
      diffy = -diffy;
    ghost.setDxDy(java.lang.Math.round((new Float(diffx / 8)).floatValue()),
		  java.lang.Math.round((new Float(diffy / 8)).floatValue()));
  }

  public boolean mouseDown(Event e, int x, int y) {
    onMouseClick(x, y);
    render();
    return true;
  }

  public void paint(Graphics g) {
    g.drawImage(output, 0, 0, this);
  }

  public void update(Graphics g) {
    paint(g);
  }

} // end Project1
