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


public class RockImage implements Resources
{
	public	RockImage()
	{
		setupRockImages();
	}

	public	RockImage(int scale)
	{
		int count;

		mRockType = scale;
  		switch(scale)
  		{
  			case ROCK_SMALL:
  				for (count = 0; count < TOTAL_ROCK_FRAMES; count++)
  				{
					if (sRocksSmall[count] == null)
					{
	     				sRocksSmall[count] = sRocksLarge[count].scaleTo(ROCK_MAX_SIZE / 3, ROCK_MAX_SIZE / 3);
	    				sRocksSmall[count].setTransparentColor(sRocksLarge[count].getTransparentColor());
					}
  				}
  				break;
  			case ROCK_MED:
  				for (count = 0; count < TOTAL_ROCK_FRAMES; count++)
  				{
					if (sRocksMed[count] == null)
					{
	    				sRocksMed[count] = sRocksLarge[count].scaleTo( (ROCK_MAX_SIZE * 2) / 3, (ROCK_MAX_SIZE * 2) / 3);
		 				sRocksMed[count].setTransparentColor(sRocksLarge[count].getTransparentColor());
					}
  				}
  				break;
  			case ROCK_LARGE:
  				for (count = 0; count < TOTAL_ROCK_FRAMES; count++)
  				{
  					sRocksLarge[count] = rocks.mMe.getBitmap(sRockIDs[count]);
				}
  				break;
		}
  	}

	public static void setupRockImages()
	{
		if (sRockImages[ROCK_LARGE] == null)
		{
			sRockImages[ROCK_LARGE] = new RockImage(ROCK_LARGE);	// large must be first, because all the others are scaled from LARGE.
		}
		if (sRockImages[ROCK_MED] == null)
		{
			sRockImages[ROCK_MED] = new RockImage(ROCK_MED);
		}
		if (sRockImages[ROCK_SMALL] == null)
		{
			sRockImages[ROCK_SMALL] = new RockImage(ROCK_SMALL);
		}
		if (sRockStar == null)
		{
			sRockStar = rocks.mMe.getBitmap(ID_star);
		}
	}

	public static RockImage getRockImage (int scale)
	{
		return sRockImages[scale];
	}

	public static RockImage getSmallerRockImage(RockImage image)
	{
		if (image.mRockType - 1 < 0) 
		{
			return null;
		}

		return getRockImage(image.mRockType - 1);
	}

	public int getValueOfRock()
	{
		int value = 0;
		switch (mRockType)
		{
			case ROCK_LARGE:
				value = rocks.kLARGE_ROCK_VALUE;
				break;
			case ROCK_MED:
				value = rocks.kMED_ROCK_VALUE;
				break;
			case ROCK_SMALL:
				value = rocks.kSMALL_ROCK_VALUE;
				break;
		}
		return value;
	}

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

		for (int count = 0; count < TOTAL_ROCK_FRAMES; count++)
		{
			switch (mRockType)
			{
				case ROCK_LARGE:
					size = sRocksLarge[count].getWidth();
					break;
				case ROCK_MED:
					size = sRocksMed[count].getWidth();
					break;
				case ROCK_SMALL:
					size = sRocksSmall[count].getWidth();
					break;
			}
			if (max < size)
			{
				max = size;
			}
		}
		return size;
	}
	public int getSizeY()
	{
		int	max = 0;
		int size = 0;

		for (int count = 0; count < TOTAL_ROCK_FRAMES; count++)
		{
			switch (mRockType)
			{
				case ROCK_LARGE:
					size = sRocksLarge[count].getHeight();
					break;
				case ROCK_MED:
					size = sRocksMed[count].getHeight();
					break;
				case ROCK_SMALL:
					size = sRocksSmall[count].getHeight();
					break;
			}
			if (max < size)
			{
				max = size;
			}
		}
		return size;
	}

	public	void draw(Pen inPen, int cx, int cy, int index, boolean crash)
	{
		if (crash)
		{
			inPen.drawBitmap(cx + sRockCrashOffsetsX[mRockType], cy + sRockCrashOffsetsY[mRockType], sRockStar);
		}
		else
		{
			switch (mRockType)
			{
				case ROCK_LARGE:
					inPen.drawBitmap(cx, cy, sRocksLarge[index]);
					break;
				case ROCK_MED:
					inPen.drawBitmap(cx, cy, sRocksMed[index]);
					break;
				case ROCK_SMALL:
					inPen.drawBitmap(cx, cy, sRocksSmall[index]);
					break;
			}
		}
	}


	public void getRectForFrame(int index, Rect dest)
	{
		switch (mRockType)
		{
			case ROCK_LARGE:
				dest.setTo(0, 0, sRocksLarge[index].getWidth(), sRocksLarge[index].getHeight());
				break;
			case ROCK_MED:
				dest.setTo(0, 0, sRocksMed[index].getWidth(), sRocksMed[index].getHeight());
				break;
			case ROCK_SMALL:
				dest.setTo(0, 0, sRocksSmall[index].getWidth(), sRocksSmall[index].getHeight());
				break;
			default:
				dest.setTo(0, 0, 0, 0);
				break;
		}
	}

	public static final int		TOTAL_ROCK_FRAMES = 19;
	public static final int		ROCK_SMALL = 0;
	public static final int		ROCK_MED = 1;
	public static final int		ROCK_LARGE = 2;

	static final int			ROCK_MAX_SIZE	=	32;
	static final int			ROCK_FRAME_RATE	=	1;

	static final int			sRockIDs[] = 
										{
							ID_rock_f01, ID_rock_f02, ID_rock_f03, ID_rock_f04, ID_rock_f05, ID_rock_f06, 
							ID_rock_f07, ID_rock_f08, ID_rock_f09, ID_rock_f10, ID_rock_f11, ID_rock_f12, 
							ID_rock_f13, ID_rock_f14, ID_rock_f15, ID_rock_f16, ID_rock_f17, ID_rock_f18, 
							ID_rock_f19
										};


	static final int			sRockCrashOffsetsX[] = {1, 7, 13};
	static final int			sRockCrashOffsetsY[] = {1, 7, 13};

	static RockImage			sRockImages[] = {null, null, null};

	static Bitmap				sRocksLarge[] = {null, null, null, null, null, null, null, null, null, null, 
											   null, null, null, null, null, null, null, null, null};

	static Bitmap				sRocksMed[] = {null, null, null, null, null, null, null, null, null, null, 
											   null, null, null, null, null, null, null, null, null};

	static Bitmap				sRocksSmall[] = {null, null, null, null, null, null, null, null, null, null, 
											   null, null, null, null, null, null, null, null, null};

	static Bitmap				sRockStar = null;

	int							mRockType;		// 0, 1, 2

}



