// Sprite class
import Raster;
import java.awt.*;
import java.awt.image.*;

//************************************************************************
/*
  Sprite is a raster that one can 'draw'
 */
class Sprite extends Raster 
{
  // let (x,y) be the top left most position of the sprite
  protected int x, y;           // current position of sprite on playfield
  protected int frameB, frameE; // Beginning of frame; end of frame
  //************************************************************************
  public Sprite()
  {
  }
  //************************************************************************
  public Sprite(Image image)
       // initialize sprite with an image
  {
    super(image);
  }
  public Sprite(Image image, int locX, int locY) 
  {
    super(image);
    x = locX;
    y = locY;
    frameB = 0;
    frameE = width;
  }         
  //************************************************************************
  public void Draw(Raster bgnd)              // draws sprite on a Raster
  {
    /* The draw method of the sprite has to be very particular.
       Transparency must be taken care of, and also, the method must
       make sure the sprite "fits" on the background. if it doesn't 
       the excess must be "clipped" */
    
    // if the top left corner is either below or to the right of the 
    // Raster area, then nothing is drawn.

    int bgndW = bgnd.getWidth();
    int bgndH = bgnd.getHeight();

    if ((x> bgndW) || (y > bgndH))
      return;
    
    
    // loop through the relevant pixels on the background
    int xLoc, yLoc = 0;
    int bgnd_pixels[] = bgnd.getPixels();
    int sprite_pixels[] = getPixels();
    
    for(xLoc=frameB; xLoc< frameE; xLoc++)
      {
	if (((xLoc-frameB+x) >= 0) && ((xLoc-frameB+x) < bgndW))
	  for(yLoc=0; yLoc< height; yLoc++)
	    {
	      if (((yLoc+y) >= 0) && ((yLoc+y) < bgndH))
		{
		  int spritePix = sprite_pixels[xLoc+(yLoc*width)];
		  // alpha is the first 8 bits
		  int alpha = (spritePix >> 24 ) &0xff;
		  if (alpha  != 0)  // checking for transparency
		    {
		      bgnd_pixels[(xLoc-frameB+x)+(bgndW*(yLoc+y))] = spritePix;
		    }
		}
	    }
      }
  }
  
  /* set and get handlers */
  public void setX(int xLoc)
  {
    x = xLoc;
  }
  public void setY(int yLoc)
  {
    y = yLoc;
  }
  public int getX()
  {
    return x;
  }
  public int getY()
  {
    return y;
  }
}





