import java.lang.System;
import java.awt.Image;
import java.awt.Rectangle;

class Sprite extends Raster {
  public static final int HORIZONTAL = 1, VERTICAL = 2, NONE = 0;
  

  int x, y;         // position of sprite on playfield
  //  int width, height;  // Size of sprite's frame

  public Sprite() {
    this(0, 0);
  }

  public Sprite(int w, int h) {
    super(w, h);

    x = y = 0;
    width = w; height = h;
  }

  public Sprite(Image image) {
    super(image);

    x = y = 0;
    width = getWidth();  
    height = getHeight();
  }
  
  public Rectangle getBounds() {
    return new Rectangle(x, y, width, height);
  }
  
  public int intersects(Sprite s) {
    Rectangle irect = getBounds().intersection(s.getBounds());
    boolean result = false;
    
    // Check all pixels in the intersection. A transparent pixel can overlap
    for (int y=irect.y; y < irect.y+irect.height; y++)
      for (int x=irect.x; x < irect.x+irect.width; x++)
	if (getPixel(x-this.x,y-this.y) >> 24 != 0 &&
	    s.getPixel(x-s.x, y-s.y) >> 24 != 0) 
	  { result = true; break; }

    if (!result) 
      return NONE;

    if (irect.width >= irect.width) return VERTICAL;
    return HORIZONTAL;
  }

  public void Draw(Raster bgnd) {
    int pel;
    int xlimit = bgnd.getWidth();
    int ylimit = bgnd.getHeight();
    for (int yloc = 0; yloc < height && y+yloc < ylimit; yloc++) {
      if (y+yloc < 0) continue;
      for (int xloc = 0; xloc < width && x+xloc < xlimit; xloc++) {
	if (x+xloc < 0) continue;
	pel = getPixel(xloc, yloc);
	// Transparency. Alpha is in high 8 bits of the pixel
	if (pel >> 24 != 0) // Fully transparent
	  bgnd.setPixel( pel, x+xloc, y+yloc);
      }
    }
  }


}
