import Raster;
import Playfield;
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.lang.Math;

/* Mouse controls Dog moves.  Dog scares cat, and cat scares mouse. */
public class SpriteDemo extends Applet implements Runnable {
  private static final String BGFile = "bg.jpg";
  private static final int NumSprites = 3;
  private static final String DogFile = "dog.gif";
  private static final String CatFile = "cat.gif";
  private static final String MouseFile = "mouse.gif";
  
  Playfield playfield;
  Sprite dog, cat, mouse;
  Sprite sprites[] = {dog, cat, mouse};
  
  Image output;
  Thread animate;
  // private stuff
  
  public void init() {
    //playfield size = (567, 572)
    playfield = new Playfield(getImage(getCodeBase(), BGFile), NumSprites); 
    dog = new Sprite(getImage(getCodeBase(), DogFile), 300, 300);
    cat = new Sprite(getImage(getCodeBase(), CatFile), 100, 100);
    mouse = new Sprite(getImage(getCodeBase(), MouseFile), 50, 70);
    
    playfield.addSprite(0, dog);
    playfield.addSprite(1, cat);
    playfield.addSprite(2, mouse);

    renderPlayfield();
  }
  
  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) {
      }
      renderPlayfield( );
    }
    
  }
  
  public void renderPlayfield() {
    
    playfield.Draw();
    output = playfield.toImage();
    repaint();
  }
  
  public void paint(Graphics g) {
    g.drawImage(output, 0, 0, this);
  }
  
  public void update(Graphics g) {
    paint(g);
  }
  
  public boolean mouseDown(Event e, int x, int y) {
    TransPix(x,y);
    dog.x = x;
    dog.y = y; 
    renderPlayfield();
    
    if(dog.getDistance(cat) < 100) {
      cat.x += ((cat.x-dog.x) > 0) ? (75-(cat.x-dog.x)) : (-75+(cat.x-dog.x));
      cat.y += ((cat.y-dog.y) > 0) ? (75-(cat.y-dog.y)) : (-75+(cat.y-dog.y));
      renderPlayfield();
    }
    
    if(cat.getDistance(mouse) < 100) {
      mouse.x += ((mouse.x-cat.x) > 0) ? (75-(mouse.x-cat.x)) : (-75+(mouse.x-cat.x));
      mouse.y += ((mouse.y-cat.y) > 0) ? (75-(mouse.y-cat.y)) : (-75+(mouse.y-cat.y));
      renderPlayfield();
    }
    return true;
  }
  
  
  /* returns whether of not a Sprite's opaque section occupies (x,y) */
  public void TransPix(int x, int y) {
    int spritePix, alpha;
    
    if ((x >= dog.x) && (x <= dog.x+dog.getWidth()-1) && 
	(y >= dog.y) && (y <= dog.y+dog.getHeight()-1)) {
      spritePix = dog.getPixel(x-dog.x,y-dog.y);
      alpha = (spritePix >> 24 ) & 0xff;
      if (alpha  != 0) {
	getAppletContext().showStatus("Arf!");
      }
    }
    else if ((x >= cat.x) && (x <= cat.x+cat.getWidth()-1) && 
	     (y >= cat.y) && (y <= cat.y+cat.getHeight()-1)) {
      spritePix = cat.getPixel(x-cat.x,y-cat.y);
      alpha = (spritePix >> 24 ) & 0xff;
      if (alpha  != 0) {
	getAppletContext().showStatus("Meow!");
      }
    }
    else if ((x >= mouse.x) && (x <= mouse.x+mouse.getWidth()-1) && 
	     (y >= mouse.y) && (y <= mouse.y+mouse.getHeight()-1)) {
      spritePix = mouse.getPixel(x-mouse.x,y-mouse.y);
      alpha = (spritePix >> 24 ) & 0xff;
      if (alpha  != 0) {
	getAppletContext().showStatus("Squeak!");
      }
    }
    else {
      getAppletContext().showStatus("No sprite here.");
    }
  }
  


  /* returns whether of not a Sprite's opaque section occupies (x,y) */
  public Sprite getSprite(int x, int y) {
    int spritePix, alpha;
    
    for(int i=0; i<NumSprites; i++) {
      if ((x >= sprites[i].x) && (x <= sprites[i].x+sprites[i].getWidth()-1) && 
	  (y >= sprites[i].y) && (y <= sprites[i].y+sprites[i].getHeight()-1)) {
	return sprites[i];
      }
    }
    return sprites[1];
  }
  
}

