import java.awt.*;

class Sprite extends Raster {
  public int x, y;
  // dx and dy are velocity variables
  // I decided to move velocity into the sprite
  public int dx, dy;
  
  public Sprite() {}
  public Sprite(Image image){
    super(image);
    this.x=0;
    this.y=0;
    this.dx=0;
    this.dy=0;
  }
  public Sprite(int w, int h, int x, int y){
    super(w,h);
    this.x=x;
    this.y=y;
    this.dx=0;
    this.dy=0;

  }
  public Sprite(Image image, int x, int y){
    super(image);
    this.x=x;
    this.y=y;
    this.dx=0;
    this.dy=0;
  }
  public Sprite(Image image, int x, int y, int dx, int dy){
    super(image);
    this.x=x;
    this.y=y;
    this.dx=dx;
    this.dy=dy;
  }

  public void Draw(Raster bgnd){
    int i,j;
    int pix;
    //System.out.println("This sprite thinks it's drawing");
    for(i=x; i<(x+width); i++){
      for(j=y; j<(y+height); j++){
	if (i<0 | j<0 | i>=bgnd.width | j>=bgnd.height)
	  continue;
	pix = getPixel(i-x, j-y);
	if ((pix >>> 24) != 0)
	  bgnd.setPixel(pix, i, j);
      }
    }        
  }
  public void Step(){
    // This updates x and y by dx and dy.
    //System.out.println("This sprite thinks it's stepping");
    this.x += this.dx;
    this.y += this.dy;
  }

  // the bounceFoo methods change the velocity of the sprite.
  public void bounceVert(){
    dy = -dy;    
  }
  public void bounceHoriz(){
    dx = -dx;
  }
  public void bounceBottom(){
    if (dy>0)
      dy = -dy;
  }
  public void bounceTop(){
    if (dy<0)
      dy = -dy;
  }
  
  public void bounceLeft(){
    if (dx>0)
      dx = -dx;
  }
  public void bounceRight(){
    if (dx<0)
      dx = -dx;
  }

  public int TestCollision(int i, int j){
    // tests to see if there's a 'collision' with bgnd coordinate i,j
    if ((i<x) | (i>x+width-1) | (j<y) | (j>y+height-1)){
      // if the sprite's not even there, then return 0
      return(0);
    } else {
      // ok, it's in the sprite. Return alpha; if it's transparent it's 0
      // anyway
      return(getPixel(i-x, j-y) >>> 24);
    }
  }
}


