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



class Sprite extends Raster
{
    int x, y;           // current position of sprite on playfield  
    boolean dead;
    Raster deadpic;
    
    /**
     *  Constructor takes as arguments two images:
     *  One for the alive state and one for the dead state,
     *  as two integers, designating the size of the playfield
     *  in which it is acceptable to put the sprite upper left
     *  corner
     */
     
    public Sprite(Image imgalive, Image imgdead, int a, int b)
    {
        super(imgalive);
        deadpic = new Raster(imgdead);
        //  Next line places the sprite at coordinates that
        //  are integer multiples of 50.  i.e. at one of the
        //  discrete locations on our gameboard.
        x = 50*(int)(a * Math.random()/50);
        y = 50*(int)(b * Math.random()/50);
        //  Starts off alive
        dead = false;
    }
    
    ////////////////////////// Methods //////////////////////////
    
    /**
     *  This method is used to move the sprites, but
     *  if a sprite moves off the screen, it wraps to
     *  the opposite edge
     */
     
    public void moveWrap(Raster bg, int dx, int dy)
    {
        // Don't move dead sprites
        if (dead == false)
        {
            // First move...
            x += dx;
            y += dy;
            // then wrap...
            while (x < 0) {x += bg.width;}
            while (x >= bg.width) {x -= bg.width;}
            while (y < 0) {y += bg.height;}
            while (y >= bg.height) {y -= bg.height;}
        }
    }
    
    /**
     *  This method is used to move the sprites, but
     *  there is no wrapping
     */
    public void move(Raster bg, int dx, int dy)
    {
        // Don't move dead sprites
        if (dead == false)
        {
            // Just move
            x += dx;
            y += dy;
        }
    }
    
    /**
     *  This method is used to get one sprite to move towards
     *  another in step sizes of d pixels
     */
    public void follow(Sprite s, Raster bg, int d)
    {
        // Don't move dead sprites or
        // follow dead sprites
        if ((dead == false) && (s.dead == false))
        {
            // Check which directions to move in, but
            // don't follow a sprite off the edge of the
            // screen.
            if ((x < s.x) && ((x + width + d)<= bg.width))
                move(bg,d,0);
            else if ((x > s.x) && ((x - d) >= 0))
                move (bg,-d,0);
            if ((y < s.y) && ((y + height + d) <= bg.height))
                move(bg,0,d);
            else if ((y > s.y) && ((y - d) >= 0))
                move (bg,0,-d);
        }
    }
    
    /**
     *  This method "kills" the sprite.  It sets the boolean
     *  flag to true.  It also replaces the pixel array in its
     *  parent Raster, with the pixel array of the deadstate,
     *  created in the constructor
     */
    public void die()
    {
        // Don't kill an already dead sprite
        if (dead == false)
        {
            // Set flag...
            dead = true;
            // Change picture...
            for (int i = 0; i < deadpic.size(); i++)
            {
                pixel[i] = deadpic.pixel[i];
            }
        }
    }
    
    /**
     *  This method tells the sprite to draw itself on
     *  the background
     */
    public void Draw(Raster bgnd)
    {
        int pix, alpha;
        int clipx, clipy;
     
        // Iterate through each pixel...
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                // Detemine position of pixel on background...
                clipx = i + x;
                clipy = j + y;
                // Get the pixel from the Sprite...
                pix = getPixel(i,j);
                // Extract the alpha..
                alpha = pix >> 24;
                // Only draw non-transparent pixels that are in the
                // bounds of the Raster...
                if ((alpha != 0) && (clipx >=0) && (clipx < bgnd.width) && (clipy >= 0) && (clipy < bgnd.height))
                    bgnd.setPixel(pix,clipx,clipy);
            }
        }
    }
}