import com.cult3d.*;
import com.cult3d.world.*;

/**
  * The opacity range is between 0.0 and 1.0
*/
public class MaterialTest extends Thread implements Cult3DScript
{
	// Change the object name to them you which to
	// change opacity on
	CultObject box1 = new CultObject("Box01");
	CultObject box2 = new CultObject("Box02");
	CultObject box3 = new CultObject("Box03");
	CultObject box4 = new CultObject("Box04");

	Material temp = new Material(true);

	// floats to store the values of RGB colors
	float red;
	float green;
	float blue;

	boolean side = true;

	// standard constructor, is run once per loading of Cult3D scene.
	public MaterialTest()
	{
		int i = box1.getMaterialCount();

		if(i > 0)
		{
			Material m = box1.getMaterialAt(0);
			RGB rgb = m.getDiffuseColor();
			red   = rgb.R;
			green = rgb.G;
			blue  = rgb.B;

			// start a thread
			start();

		}
		else
		{
			System.out.println("No material found!!");
		}

		temp.setTransparancy(0.5f);
	}

	/**
	  * This method is called from the designer.
	  * Do the actually fading.
	*/
	public void step(String s)
	{

		if(side)
		{
			// increasing opacity counter
			if(red <= 0.97f)
			{
				red += 0.03f;
			}
			else
			{
				side = !side;
			}
		}
		else
		{
			// decreasing opacity counter
			if(red >= 0.03f)
			{
				red -= 0.03f;
			}
			else
			{
				side = !side;
			}
		}

		// change the opacity on materials
		RGB rgb = new RGB(red,green,blue);

		temp.setDiffuseColor(rgb);
		temp.setTransparancy(0.0f);
		box1.setMaterialAt(0, temp, true, true);

		temp.setTransparancy(red);
		box2.setMaterialAt(0, temp, false, true);

		temp.setTransparancy(1f-red);
		box3.setMaterialAt(0, temp, false, true);
	}

	public void cult3dDestroy()
	{
	}
}
