package java.game.vehicles.Ford_Mustang_GT_2005;

import java.game.*;
import java.game.parts.*;
import java.util.resource.*;
import java.io.*;

public class Ford_Mustang_GT_2005_R_wing extends Part
{
	float	wing_state = 0.0;	// 0=in.0, 1.0=out, >0.0 && <1.0=moving //
	Chassis	chassis;
	final static float	CHECK_SPEED_INTERVAL = 0.500;
	final static float	RAISE_WING_INTERVAL = 0.050;

	public Ford_Mustang_GT_2005_R_wing ( int id )
	{
		super( id );
		carCategory = PACKAGE;
		name = "Rear wing";

		addTimer( CHECK_SPEED_INTERVAL, 1001 ); // wing mover, checks the car's speed //
	}

	public void handleEvent( GameRef obj_ref, int event, int param )
	{
		
		{
			if( event == EVENT_TIME )
			{
				if ( !chassis )
					chassis = getChassis();
				float kmphSpeed = (chassis)?(chassis.getVel().length() * 3.6):(0.0);

				setSlotPos( 1111, new Vector3(0.000, wing_state * -0.056, 0.000), new Ypr(0.000, 0.000, 0.000) );

				switch( param )
				{
					case 1001: // wing mover //
						if ( kmphSpeed > 100.0 && wing_state == 0.0 )
						{
							removeTimer( 1001 );
							addTimer( RAISE_WING_INTERVAL, 10011 ); // open the wing //
						}
						else
						if ( kmphSpeed < 80.0 && wing_state == 1.0 )
						{
							removeTimer( 1001 );
							addTimer( RAISE_WING_INTERVAL, 10012 ); // close the wing //
						}
						else
							addTimer( CHECK_SPEED_INTERVAL, 1001 ); // check again in a half second, please... //
						break;
					case 10011: // wing opening //
						if ( wing_state < 1.0 )
						{
							wing_state += RAISE_WING_INTERVAL;
							addTimer( RAISE_WING_INTERVAL, 10011 ); // keep moving //
						}
						else
						{
							removeTimer( 10011 );
							addTimer( CHECK_SPEED_INTERVAL, 1001 ); // go back to speed check loop //
							wing_state = 1.0;
						}
						break;
					case 10012: // wing closing //
						if ( wing_state > 0.0 )
						{
							wing_state -= RAISE_WING_INTERVAL;
							addTimer( RAISE_WING_INTERVAL, 10012 ); // keep moving //
						}
						else
						{
							removeTimer( 10012 );
							addTimer( CHECK_SPEED_INTERVAL, 1001 ); // go back to speed check loop //
							wing_state = 0.0;
						}
						break;
				}
			}
		}
	}
}
