import java.applet.*;
import java.awt.*;
import Raster;

/********************************************************
 * Sprite controls movement and rendering of a single
 * image
 *********************************************************/
class Sprite extends Raster {
  int x, y;           // current position of sprite on playfield
  int width, height;  // size of a sprite's frame

  // initialize sprite with an image and position
  public Sprite(Image image){
    super(image);
    // initialize size;
    width = super.width;
    height = super.height;
    // initialize position
    x = 0;
    y = 0;
  }

  // change position of sprite on playfield relative
  // to its current position
  public void moveRel(int shiftx, int shifty) {
    x += shiftx;
    y += shifty;
  }

  // returns whether mouse pointer is over an opaque pixel of sprite
  public boolean isOverSprite(int mousex, int mousey) {
    // if not within sprite frame...
    if (mousex-x < 0 || mousey-y < 0 || mousex-x >= width ||
	mousey-y >= height) {
      return false;
    }
    else
      // return true if pixel is opaque
      return (this.getPixel (mousex-x, mousey-y) >> 24) != 0;
  }

  // draws sprite on a Raster
  public void Draw(Raster bgnd){
    int curx, cury, curpix;

    // set limits of area to draw
    int startx = 0, starty = 0;
    int endx = width, endy = height;
    if ((startx + x) < 0) startx = -x;
    if ((starty + y) < 0) starty = -y;
    if ((endx + x) >= bgnd.width) endx = bgnd.width - x;
    if ((endy + y) >= bgnd.height) endy = bgnd.height - y;

    for (curx = startx; curx < endx; curx++)
      for (cury = starty; cury < endy; cury++) {
	curpix = this.getPixel(curx, cury);
	// if not transparent
	if ((curpix >> 24) != 0)
	  // set sprite pixel
	  bgnd.setPixel(curpix, curx+x, cury+y);
      }

  }
}






