#include scener
#include sound
#include input

export ON_CREATE;
export ON_LBUTTONDOWN;
export ON_TIME;


int time = -1;
int max_line = 0;
float cur_pos = 0.0;
float line_pos = 180.0;
float wait = 0;

GAME_OBJECT * p_tex[1000];

void ON_CREATE()
{
	int i;
	char cname[16];
	LINK * p_link;
	vec3_t pos;
	GAME_OBJECT * p_tw;
	TEXT_WINDOW *p_wnd;
	IFPlane *p_plane;
	vec3_t color;

	color.Assign( 0.68, 1, 0);
	for ( i = 0; i < 1000; i++ )
	{
		sprintf( cname, "credits%.3d", i );
		p_link = scene.FindObjectByName( cname );
		p_tex[i] = p_link;
		if ( p_link )
		{
			p_tw = p_tex[i];
			max_line = i;
			p_tw->GetBonePosition( 0, pos );
			line_pos += pos.y*2;
			pos.x += 135;
			pos.y = line_pos;
			p_tw->SetBonePosition( 0, pos );
			p_link = p_link->GetChild();
			if ( p_link->GetObjectClass() == 6 ) // OC_TEXT_WINDOW
			{
				p_wnd = p_link;

				line_pos += p_wnd->GetCharSize();
		//		if ( i < 2 )
		//			p_wnd->SetActive( true );
				p_wnd->SetColor( color );
			}
			else
			if ( p_link->GetObjectClass() == 7 ) // OC_INTERFACE_PLANE
			{
				p_plane = p_link;
				line_pos += p_plane->size.y;
			}
		}
		else
			line_pos += 0;
	}
	cur_pos = line_pos;

	self->CreateTimer( 0, -1 );
}

void ON_LBUTTONDOWN()
{
	scene.Delete(255);
	scene.Load("Start.XML");
}

void ON_TIME( int timernum )
{
	float delta;
	int oldtime;
	int i;
	vec3_t pos;
	GAME_OBJECT * p_tw;
	
	if(time == -1)
	time = scene.GetTime();

	oldtime = time;
	time = scene.GetTime();

	delta = time - oldtime;
	delta *= 0.0268;

        if(wait > 0)
        {  
        wait -= delta;
        delta = 0;
        }

        cur_pos -= delta;

	for ( i = 0; i < 1000; i++ )
	{
		p_tw = p_tex[i];
		if ( p_tw )
		{
			p_tw->GetBonePosition( 0, pos );
			pos.y -= delta;
			p_tw->SetBonePosition( 0, pos );
		}
	}

	if ( cur_pos < -30 )
	{
		ON_LBUTTONDOWN();
	}
}
