// Sprite class

import java.awt.*;
import java.awt.image.*;
import MyRaster.*;
import java.applet.*;

class Sprite extends MyRaster {
    int x, y;   // sprite's position (anchored to sprite's center) on
                //  playing field
    int destX, destY;   // destination coordinates
    double ySpeed, xSpeed, speed;
    final int clipMargin = 5;

    ////////////////////////// constructors /////////////////////
    public Sprite() {
		//{{INIT_CONTROLS
		//}}
	}
    public Sprite( Image img ) {    // initialize sprite with an image
        super( img );   // use parent class's constructor here
        x = 0;
        y = 0;
        speed = 0.0;
    }
    
    ////////////////////////// methods /////////////////////
    public void SetPosition( int ix, int iy ) {
        // set current coordinates on background, relative to Sprite
        //  center
        x = ix-width/2;
        y = iy-height/2;
    }
    
    public void SetSpeed( double a ) { speed = a; }
    
    public void SetDestination( int ix, int iy ) {
        // Set destination coordinates on background, relative to 
        //  Sprite center.
        // Also set speed for Sprite to be moving in terms of x and y
        //  components.
        destX = ix-width/2;
        destY = iy-height/2;

        // calculate xSpeed and ySpeed
        //   Note that everything is kept positive here because it is more
        //   convenient to test for when the sprite has overrun its
        //   destination.
        double deltaX = Math.abs((double)destX-x );
        double deltaY = Math.abs((double)destY-y );
        double bearing;
        if( destX == x ) {      // so we don't get divide by zero
            ySpeed = speed;
            xSpeed = 0;
        } else {                // normal case
            bearing = Math.atan( deltaY/deltaX );
            xSpeed = speed * Math.cos( bearing );
        }
        if( destY == y ) {      // so we don't get divide by zero
            ySpeed = 0;
            xSpeed = speed;
        } else {                // normal case
            bearing = Math.atan( deltaY/deltaX );
            ySpeed = speed * Math.sin( bearing );
        }
    }

    // draws sprite on a raster
    public void Draw(Raster bg){
        // bg is a background raster
        int i;          // pixel's linear location in sprite
        int ci;         // pixel's linear location on bg
        int cX, cY;     // pixel's xy location on bg
        
        i = 0; ci = 0;
        // for each pixel in sprite, calculate pixel's linear location
        //  on bg and write
        for( cY = y; cY < (y+height); cY++ ) {
            for( cX = x; cX < (x+width) && (ci < bg.size()) && (i < size()); cX++ ) {
                if( // clip sprite?
                    (cX > clipMargin) && (cX < (bg.width-clipMargin)) &&
                    (cY > clipMargin) && (cY < (bg.height-clipMargin)) &&
                    // and not transparent? 
                    //  I use pure white as transparent pixel
                    (pixel[i] != 0xffffffff) ) {
                    // copy pixel
                    ci = cY * bg.width + cX;
                    bg.pixel[ci] = pixel[i];
                }
                i++; ci++;
            }
        }
        // ** debug - puts a white dot at center
        //bg.pixel[y * bg.width + x] = 0xffffffff;
    }
    
    // move sprite towards the set destination
    public void UpdateLocation() {
        int dy, dx;
        dy = (int)(ySpeed + 0.5);
        dx = (int)(xSpeed + 0.5);
        if( destY > y ) {
            y += dy;
            if( y > destY ) y = destY;  // reached destination
        } else {
            y -= dy;
            if( y < destY ) y = destY;  // reached destination
        }
        if( destX > x ) {
            x += dx;
            if( x > destX ) x = destX;  // reached destination
        } else {
            x -= dx;
            if( x < destX ) x = destX;  // reached destination
        }
    }
        
		//{{DECLARE_CONTROLS
		//}}
}