/****************************************************
 *    6.837 Project 1:  Sprite-based Applets        *
 *    Created by:  Rielyn Sarabia                   *
 *    September 28, 1998                            *
 ****************************************************/

package project1;

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

class Sprite extends Raster {
  //Sprite is a Raster that is drawn on another Raster, called a playfield

  int x, y;           // current position of sprite on playfield
  int width, height;  // size of a sprite's frame
  
  public Sprite () {}  //empty constructor
  
  public Sprite(Image image) {       
    //initialize sprite with an image
    //default location of this is (0,0)
    super(image);
    this.width=super.width;
    this.height=super.height;
    x=0;
    y=0;
  }
  
  public Sprite(Image image, int x, int y) {
    //initializes sprite with an image and a location (x,y)
    super(image);
    this.width=super.width;
    this.height=super.height;
    this.x=x;
    this.y=y;
  }    

  public void setX (int x) {
    //sets the x-coordinate of this
    this.x = x;
  }
  public void setY (int y) {
    //sets the y-coordinate of this
    this.y = y;
  }

  public void setWidth (int w) {
    //sets the width of this
    width = w;
  }
  public void setHeight (int h) {
    //sets the height of this
    height = h;
  }

  public void Draw(Raster bgnd) {              // draws sprite on a Raster
    //draws this onto bgnd
    //things to consider:
    //     clipping due to overhang
    //     transparent pixels
    int end_x = (bgnd.getWidth() > x+width) ? x+width : bgnd.getWidth();
    int end_y = (bgnd.getHeight() > y+height) ? y+height : bgnd.getHeight();
    for (int i=x; i<end_x; i++) {
      for (int j=y; j<end_y; j++) {
	if (this.getPixel(i-x, j-y) >> 24 != 0)  //if alpha byte is 0, pixel is transparent
	  bgnd.setPixel(this.getPixel(i-x, j-y), i, j);
      }
    }
  }    
  
    } //end class Sprite
