// 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.ui.*;
import danger.app.*;
import danger.audio.*;

public class HeroImage implements Resources
{
	public	HeroImage()
	{
		int mImages[] = {	ID_ship_f01, ID_ship_f02, ID_ship_f03, ID_ship_f04, 
							ID_ship_f05, ID_ship_f06, ID_ship_f07, ID_ship_f08, 
							ID_ship_f09, ID_ship_f10, ID_ship_f11, ID_ship_f12,
							ID_ship_f13, ID_ship_f14, ID_ship_f15, ID_ship_f16,
							ID_ship_f17, ID_ship_f18, ID_ship_f19, ID_ship_f20,
							ID_ship_f21, ID_ship_f22, ID_ship_f23, ID_ship_f24
							};

		int mThrustImages[] = {	ID_thrust_f01, ID_thrust_f02, ID_thrust_f03, ID_thrust_f04, 
							ID_thrust_f05, ID_thrust_f06, ID_thrust_f07, ID_thrust_f08, 
							ID_thrust_f09, ID_thrust_f10, ID_thrust_f11, ID_thrust_f12,
							ID_thrust_f13, ID_thrust_f14, ID_thrust_f15, ID_thrust_f16,
							ID_thrust_f17, ID_thrust_f18, ID_thrust_f19, ID_thrust_f20,
							ID_thrust_f21, ID_thrust_f22, ID_thrust_f23, ID_thrust_f24
							};

		int mCrashImages[] = {	kExplode_f02, kExplode_f03, kExplode_f04, 
								kExplode_f05, kExplode_f06, kExplode_f07, kExplode_f08, 
								kExplode_f09, kExplode_f10, kExplode_f11, kExplode_f12,
								kExplode_f13, kExplode_f14, kExplode_f15, kExplode_f16,
								kExplode_f17, kExplode_f18, kExplode_f19, kExplode_f01
								};

		for (int count = 0; count < TOTAL_HERO_FRAMES; count++)
		{
			mHero[count] = rocks.mMe.getBitmap(mImages[count]);
			mHeroThrust[count] = rocks.mMe.getBitmap(mThrustImages[count]);
		}

		for (int count = 0; count < TOTAL_HERO_CRASH_FRAMES; count++)
		{
			mHeroCrash[count] = rocks.mMe.getBitmap(mCrashImages[count]);
		}
		mImagesReady = true;
	}

	private void drawHero(Pen inPen, int cx, int cy, int frame)
	{
		if (mThrust == false)
		{
			inPen.drawBitmap(cx, cy, mHero[frame]);
		}
		else
		{
			inPen.drawBitmap(cx, cy, mHeroThrust[frame]);
		}
	}

	public	void draw(Pen inPen, int cx, int cy, int frame)
	{
		if (mCrashed == false)
		{
			drawHero(inPen, cx, cy, frame);
		}
		else
		{
			if (mCrashFrame < TOTAL_HERO_CRASH_FRAMES)
			{
				if (mCrashFrame < (TOTAL_HERO_CRASH_FRAMES/2))
				{
					drawHero(inPen, cx, cy, frame);
				}
				inPen.drawBitmap(cx + CRASH_OFFSET_X, cy + CRASH_OFFSET_Y, mHeroCrash[mCrashFrame]);
				mCrashFrame++;
			}
		}	
	}

	public int getSizeX()
	{
		int	max = 0;
		int size = 0;

		if (mCachedSizeX == -1)
		{
			for (int count = 0; count < TOTAL_HERO_FRAMES; count++)
			{
				size = mHero[count].getWidth();
				if (max < size)
				{
					max = size;
				}
			}
			mCachedSizeX = max;
		}
		return mCachedSizeX;
	}

	public int getSizeY()
	{
		int	max = 0;
		int size = 0;

		if (mCachedSizeY == -1)
		{
			for (int count = 0; count < TOTAL_HERO_FRAMES; count++)
			{
				size = mHero[count].getHeight();
				if (max < size)
				{
					max = size;
				}
			}
			mCachedSizeY = max;
		}
		return mCachedSizeY;
	}

	public Rect getRectForHero(int frame)
	{
		if (mImagesReady == true)
		{
			if (mCachedHeroRect == null)
			{
				mCachedHeroRect = new Rect();
			}
			mCachedHeroRect.setTo(0, 0, getSizeX(), getSizeY());
			return mCachedHeroRect;
		}
		return null;
	}

	public void setThrust(boolean on)
	{
		mThrust = on;
	}

	public void setCrash(boolean crashed)
	{
		mCrashFrame = 0;
		mCrashed = crashed;
	}

	public static final int HERO_MAX_SIZE	=	27;
	public static final int	TOTAL_HERO_FRAMES = 24;
	public static final int HERO_ANGLE_PER_FRAME = 360 / TOTAL_HERO_FRAMES;

	boolean				mImagesReady = false;
	static Bitmap		mHero[] =		{null, null, null, null, null, null, null, null,
										null, null, null, null, null, null, null, null,
										null, null, null, null, null, null, null, null};

	static Bitmap		mHeroThrust[] = {null, null, null, null, null, null, null, null,
										null, null, null, null, null, null, null, null,
										null, null, null, null, null, null, null, null};
	Rect				mCachedHeroRect = null;
	int					mCachedSizeX = -1;
	int					mCachedSizeY = -1;

	boolean				mCrashed;
	int					mCrashFrame;
	boolean				mThrust;

	static final int	TOTAL_HERO_CRASH_FRAMES	= 19;
	static final int	CRASH_OFFSET_X = -9;
	static final int	CRASH_OFFSET_Y = -9;
	static Bitmap		mHeroCrash[] =	{null, null, null, null, null, null, null, null,
										null, null, null, null, null, null, null, null,
										null, null, null};

 	static int			mHeroMissileOffsetX[] = {14, 15, 17, 18, 21, 18, 23, 5, 16, 15, 19, 17, 13, 13, 10, 11, 8, 
											7, 7, 7, 6, 10, 8, 10};
	static int			mHeroMissileOffsetY[] = {8, 10, 8, 11, 10, 12, 14, 11, 17, 16, 21, 21, 19, 20, 20, 17, 19, 
											16, 14, 12, 9, 10, 5, 5};
}


