import java.awt.*;
import java.awt.image.*;

public class Playfield extends Raster
{	
	private int num_asprites = 0; //Keeps track of the number of sprites that we're rendering on the playfield
	private int max_asprites = 3; //The max number of sprites possible on the playfield
	private int offset = 10; //The increment/decrement value for moving the chase asprite around the playfield
	private AnimatedSprite asprites[] = new AnimatedSprite[max_asprites]; //The array of asprites
	private AnimatedSprite success_asprite; //Holds the asprite to play when he reaches the cheesypoofs
	
	//***********************************************************
	//Constructors
	//***********************************************************
    // This constructor which takes no arguments allows for future extension.
    public Playfield()
    {
    }

    // This constructor creates an uninitialized Playfield Object of a given size (w x h).
    public Playfield(int w, int h)
    {
		super(w,h);
    }

    // This constructor creates an Playfield initialized with the contents of an image.
    public Playfield(Image img)
    {
		super(img);
    }
	
	// Adds an asprite to the array of asprites to render on the playfield.  The asprite to be
	// Played when cartman gets the cheesypoofs is not to be put in this array.
	public void AddAnimatedSprite(AnimatedSprite asprite)
	{
		// Make sure that we're below the number of max_asprites
		if (num_asprites < max_asprites)
		{
			//Add it to the array and increment the number of asprites
			num_asprites += 1;
			asprites[num_asprites-1] = asprite;
		}
	}

	// Sets the success_asprite to the value passed in.  This is the asprite that's played when
	// cartman gets the cheesypoofs
	public void AddSuccessSprite(AnimatedSprite asprite)
	{
		success_asprite = asprite;
	}

	// The main draw method for drawing the sprites on the raster
	public void Draw()
	{
		AnimatedSprite curr_asprite = new AnimatedSprite(); //Temporary asprite to hold the asprites from the array
				
		//For every asprite in the array
		for(int k=0;k<num_asprites;k++)
		{
			int i = 0;
			//Check to see if we're rendering the cartman sprite, and if we've reached the cheesypoofs
			if((k == 0) && (InSuccessRange()))
				curr_asprite = success_asprite;
			else
				curr_asprite = asprites[k];
			//Set the animation frame of the current sprite to the next in it's list of frames
			curr_asprite.NextFrame();

			//For every pixel along the y-axis of the sprite
			while((i<curr_asprite.getHeight()) &&
				  (curr_asprite.loc.y+i<this.getHeight()) &&
				  (curr_asprite.loc.y+curr_asprite.getHeight()+i>0))
			{
				int j = 0;
				//For every pixel along the x-axis of the sprite
				while((j<curr_asprite.getWidth()) &&
					  (curr_asprite.loc.x+j<this.getWidth()) &&
					  (curr_asprite.loc.x+curr_asprite.getWidth()+j>0))
				{
					//Make sure that we're within the clipping bounds and that the pixel in the
					//sprite isn't transparent
					if((curr_asprite.loc.x+j >= 0) &&
					   (curr_asprite.loc.y+i >= 0) &&
					   !(curr_asprite.IsTranparent(j,i)))
						//If all of that holds, set the offset pixel in the playfield to its
						//corresponding pixel in the sprite
						this.setPixel(curr_asprite.getPixel(j,i), curr_asprite.loc.x+j, curr_asprite.loc.y+i);
					j++;
				}
				i++;
			}			
		}
		
		//Calculate the next chase sprite coordinates
		MoveAnimatedSprite();
	}
	
	//Sets the position of the center of the cheesypoofs sprite with the values passed in
	public void SetTarget(int x1, int y1)
	{
		asprites[1].loc.x = x1 - (asprites[1].getWidth()/2);
		asprites[1].loc.y = y1 - (asprites[1].getHeight()/2);
	}

	//Calculates the next x and y coords of the cartman sprite
	public void MoveAnimatedSprite()
	{		
		//Move the sprite's x direction towards the cheesypoofs
		if((asprites[0].loc.x < this.getWidth()) &&
		   (asprites[0].loc.x + asprites[0].getWidth() >= 0))
		{
			if(asprites[0].loc.x < (asprites[1].loc.x - asprites[1].getWidth()/2))
				asprites[0].loc.x += offset;
			else if(asprites[0].loc.x > (asprites[1].loc.x + asprites[1].getWidth()/2))
				asprites[0].loc.x -= offset;
		}

		//Move the sprite's y direction towards the cheesypoofs
		if((asprites[0].loc.y < this.getHeight()) &&
		   (asprites[0].loc.y + asprites[0].getHeight() >= 0))
		{
			if(asprites[0].loc.y < (asprites[1].loc.y - asprites[1].getHeight()/2))
				asprites[0].loc.y += offset;
			else if(asprites[0].loc.y > (asprites[1].loc.y + asprites[1].getHeight()/2))
				asprites[0].loc.y -= offset;
		}

		//Update the success_asprite's coords to be the same as the current cartman sprite
		success_asprite.loc.x = asprites[0].loc.x;
		success_asprite.loc.y = asprites[0].loc.y;
	}
	
	//Checks to see if cartman has currently "got" the cheesypoofs, and tells him to do
	//a little dance.
	public boolean InSuccessRange()
	{
		boolean success = false;
		if((Math.abs((asprites[1].loc.x)-(asprites[0].loc.x)) <= asprites[0].getWidth()) &&
		   (Math.abs((asprites[1].loc.y)-(asprites[0].loc.y)) <= asprites[0].getHeight()))
			success = true;
		return success;
	}

    //Method that tests if a given playfield coordinate overlaps a non-transparent pixel of
	//a sprite at its current position.
	public boolean IsNonTransparent(int x, int y)
	{
		boolean val = true;
		int curr_x = 0;
		int curr_y = 0;
		
		//Checks the pixels of the cartman and cheesypoofs sprites for non-transparecy
		for(int i=0;i<num_asprites;i++)
		{
			curr_x = asprites[i].loc.x;
			curr_y = asprites[i].loc.y;

			if((x >= curr_x) && (x <= curr_x + asprites[i].getWidth()) &&
			   (y >= curr_y) && (y <= curr_y + asprites[i].getHeight()) &&
			   (asprites[i].IsTranparent(x-curr_x,y-curr_y)))
				val = false;
		}

		//Checks the pixels of the dance sprite for non-transparecy
		curr_x = success_asprite.loc.x;
		curr_y = success_asprite.loc.y;
		if((x >= curr_x) && (x <= curr_x + success_asprite.getWidth()) &&
		   (y >= curr_y) && (y <= curr_y + success_asprite.getHeight()) &&
		   (success_asprite.IsTranparent(x-curr_x,y-curr_y)))
			val = false;

		return val;
	}
}