// Annamaria Cherubin: last edited on 9-27-1998
// 6.837 - Project #1
// Sprite.java

package proj1;
import java.awt.*;
import java.awt.image.*;

public class Sprite extends Raster {
  int x, y;            // current position of spite on playfield
  int width, height;   // size of a sprite's frame

  // Constructor
  public Sprite(Image image) {
    // effects: initialize sprite with an image.
    super(image);
    width = getWidth();
    height = getHeight();
    x = 0;
    y = 0;
    
  }

  // Methods
  public void Draw(Raster bgnd) {
    // effects: draws a Sprite on a Raster (the background) - must handle 
    //          transparent pixels, and it must also handle all cases where 
    //          the sprite overhangs the playfield.
    int w = width;
    int h = height;
    
    // Check if the Sprite is in the Playfield
    if (x + width > bgnd.getWidth())
      w = bgnd.getWidth() - x;
    if (y + height > bgnd.getHeight())
      h = bgnd.getHeight() - y;

    for (int i = x; i < x + w; i++) {
      for (int j = y; j < y + h; y++) {
	// Check if the pixel is transparent
	int pixel = getPixel(i-x, j-y);
	if ((getPixel(i, j) >>> 24) != 0)
	  bgnd.setPixel(pixel, i, j);
      }
    } 
  } // end Draw

} // end class Sprite
