package ps1;

import java.awt.*;
import java.awt.image.*;

 
class Sprite extends Raster {
  /**
   * The Sprite class extends the Raster class and
   * enbables us to create and draw sprites. This class 
   * also includes accessors to set the location of the sprite 
   * <P> 6.837 Problem set #1
   * @author Christina Chu
   */
  
  public int x=0, y=0;             // initial position of sprite on playfield
                                   // default for testing is 0,0
  
    /**
     *  This constructor, which takes no arguments,
     *  allows for future extension.
     */

  public Sprite() {    
    super();
  }

  /**
   *  This constructor creates an uninitialized
   *  Sprite Object of a given size (w x h) by super
   *  
   */
  
  public Sprite(int w, int h) {
    super();
  }
  
  /**
   *  This constructor creates a Sprite initialized
   *  with the contents of an image. Width and Height 
   *  are initialized here.
   */  
  
  public Sprite(Image img) {          // initialize sprite with an image
    try {
      PixelGrabber grabber = new PixelGrabber(img, 0, 0, -1, -1, true);
      if (grabber.grabPixels()) {
	width = grabber.getWidth();
	height = grabber.getHeight();
	pixel = (int []) grabber.getPixels();
      }
    } catch (InterruptedException e) {
      System.err.println("Unable to grab image in Raster constructor"+e);
    } catch (NullPointerException e) {
      System.err.println("Unable to grab image in Raster constructor"+e);
    }    
  }

  /**
   * This method sets the x,y location of the sprite
   *
   */
  
  public void setLocation(int x, int y) {
    this.x = x;
    this.y =y;
  }


  /** This method draws a sprite on a Raster at position x,y  and does not
    * draw transparent pixels. For each pixel, we check if the position
    * in the sprite is on the Raster. If it is, then we do a simple 
    * pixel value check if the Sprite pixel is transparent or not. If the
    * sprite pixel is non transparent we set this pixel at location x, y on 
    * the Raster background.
    * @param root, the Component which is a super type of the applet passed in.
    * @param bgnd, the Raster background on which to draw the Sprite.    
    */

  public void Draw(Component root, Raster bgnd) {           
    int x1=x, y1=y;
    int xval = x1+width;
    int yval = x1+height;
    for(int j=0; j < pixel.length; j++) {                  
      xval = x1+width;  yval= y1+height;
      if ( x1 < bgnd.width && x1 > 0 ) { 
	if (y1 < bgnd.height && y1 >0) {
	  try {
	    //test pixel. for transparency.		
	    int pix = pixel[Math.abs(y1-y)*width+ Math.abs(x1-x)];	  
	    int p = pix >> 24;
	    if (p != 0) {	    
	      boolean b = bgnd.setPixel(pixel[j],x1,y1);
	    }	  
	  } catch (ArrayIndexOutOfBoundsException e) {
	    System.err.println("ArrayIndexOutOfBoundsException " +e);
	  }
	}
      }
      x1++;     
      if (Math.abs(x1-x) >= width) { x1=x; y1++;}      
      if ((y1-y) >= height) { j = pixel.length;}       // if height is exceeded, then force loop to end.
    } // end of for loop    
    Image im = bgnd.toImage(root);    
  } // end of Draw


  /** This method draws a sprite on a Raster at position x,y and does not
    * draw transparent pixels. This image is flipped vertically.
    * @param root, the Component which is a super type of the applet passed in.
    * @param bgnd, the Raster background on which to draw the Sprite.
    */

  public void FlipDraw(Component root, Raster bgnd) {           
    // This method is never instantiated. It was created as
    // as experiement

    int x1=x, y1=y;
    int xval = x1+width;
    int yval = x1+height;
    for(int j=0; j < pixel.length; j++) {                  
      xval = x1+width;  yval= y1+height;
      if ( x1 < bgnd.width && x1 > 0 ) { 
	if (y1 < bgnd.height && y1 >0) {
	  try {
	    // test pixel. for transparency.		
	    int pix = pixel[Math.abs(y1-y)*width+ Math.abs(x1-x)];	  
	    int p = pix >> 24;
	    if (p != 0) {	    
	      int newx = x+width-Math.abs(x1-x);
	      boolean b = bgnd.setPixel(pixel[j],newx,y1);
	    }	  
	  } catch (ArrayIndexOutOfBoundsException e) {
	    System.err.println("ArrayIndexOutOfBoundsException " +e);
	  }
	}
      }
      x1++;     
      if (Math.abs(x1-x) >= width) { x1=x; y1++;}      
      if ((y1-y) >= height) { j = pixel.length;}       // if height is exceeded, then force loop to end.
    } // end of for loop    
    Image im = bgnd.toImage(root);    
  } // end of Draw

}



