// Copyright 2001-2003, Danger, Inc.  All Rights Reserved.
// This file is subject to the Danger, Inc. Sample Code License,
// which is provided in the file SAMPLE_CODE_LICENSE.
// Copies are also available from http://developer.danger.com/
package com.danger.rocks;

import danger.app.*;
import danger.audio.*;
import danger.ui.*;

import java.util.LinkedList;


/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
public class RockObject
{
	//~ Static fields/initializers ---------------------------------------------

	static final int TOTAL_CRASH_FRAMES = 4;

	//~ Instance fields --------------------------------------------------------

	// data
	RockImage    mRockImage  = null;
	MotionObject mMotion     = null;
	Rect		 mImageRect  = new Rect();
	LinkedList   mRockLink;
	boolean		 mCrashed;
	int			 mCrashFrame;

	//~ Constructors -----------------------------------------------------------

	/**
	 * Creates a new RockObject object.
	 *
	 * @param x DOCUMENT ME!
	 * @param y DOCUMENT ME!
	 * @param angle DOCUMENT ME!
	 * @param fspeed DOCUMENT ME!
	 * @param bounds DOCUMENT ME!
	 * @param rock DOCUMENT ME!
	 */
	public RockObject(int x, int y, int angle, int fspeed, Rect bounds,
		RockImage rock)
	{
		mMotion     = new MotionObject(x, y, angle, fspeed, bounds,
				RockImage.TOTAL_ROCK_FRAMES, RockImage.ROCK_FRAME_RATE,
				rock.getSizeX(), rock.getSizeY());
		mRockImage = null;

		if (mMotion != null)
		{
			mRockImage = rock;
			setCrash(false);
			mMotion.setCurrentFrame(mMotion.getFrameFromAngle());
		}
	}

	//~ Methods ----------------------------------------------------------------

	/**
	 * DOCUMENT ME!
	 *
	 * @param inPen DOCUMENT ME!
	 */
	public boolean draw(Pen inPen)
	{
		boolean remove = false;
		
		if (mRockImage != null)
		{
			int x = mMotion.getX();
			int y = mMotion.getY();
			int f = mMotion.getCurrentFrame();

			if (mCrashFrame < (TOTAL_CRASH_FRAMES / 2))
			{
				mRockImage.draw(inPen, x, y, f, false);
			}

			if (mCrashed)
			{
				mRockImage.draw(inPen, x, y, f, true);

				if (mCrashFrame < TOTAL_CRASH_FRAMES)
				{
					mCrashFrame++;
				}
				else
				{
					// we hit the end, so delete from list
					if (mRockLink != null)
					{
						remove = true;
						mRockLink = null;
					}
				}
			}

			if (rocks.mDEBUG == true)
			{
				Rect r = getRect();

				if (r.left > r.right)
				{
					int a = r.left;
					r.left	    = r.right;
					r.right     = a;
				}

				if (r.bottom < r.top)
				{
					int a = r.bottom;
					r.bottom     = r.top;
					r.top		 = a;
				}

				inPen.drawRect(r);
			}
		}

		return remove;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public Rect getRect()
	{
		mRockImage.getRectForFrame(mMotion.getCurrentFrame(), mImageRect);
		mImageRect.translate(mMotion.getX(), mMotion.getY());

		return mImageRect;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param list DOCUMENT ME!
	 */
	public void hit(LinkedList list)
	{
		mRockLink = list;
		setCrash(true);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param missile DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean collidesWith(MissileObject missile)
	{
		boolean hit = false;

		if (mCrashed == false)
		{
			Rect rect = getRect();
			hit = rect.contains(missile.mMotion.getX(), missile.mMotion.getY());
		}

		return hit;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param crash DOCUMENT ME!
	 */
	public void setCrash(boolean crash)
	{
		mCrashFrame     = 0;
		mCrashed	    = crash;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isCrashed()
	{
		return mCrashed;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param w DOCUMENT ME!
	 */
	public void update(RockWindow w)
	{
		// invalidate old position
		//w.InvalidateThisRect(getRect());
		mMotion.Update();

		// invalidate new position
		//w.InvalidateThisRect(getRect());
	}
}
