#include "scripts/map/map_jam_common.cpp"

enum eTIDs
{
	TID_Warp_Entry = 1,
	TID_Warp_Exit,
	TID_Warp_Save,
	TID_BirdEvent_1,
	TID_Bird1_1,
	TID_Bird1_2,
	TID_Bird2, // event and the bird itself

	TID_Credits_Warp, // 8
	TID_Credits_Event
}

//==============================================================================
//    Level Startup
//==============================================================================

$script 0
{
	JAM::SetupMap();
	
	// allow birds to pass over cliffs
	SetDropoff( TID_Bird1_1 );
	SetDropoff( TID_Bird1_2 );
	SetDropoff( TID_Bird2 );
	// start level tick
	Game.CallDelayedMapScript( 1, null, 0 );
}

void SetDropoff( int TID )
{
	kActor@ A = World.GetActorByTID( TID );
	if ( A !is null )
		A.ClipFlags() |= CF_DROPOFF;
}

//==============================================================================
//    Level Tick
//==============================================================================

$script 1
{
	for ( int i=0; i<2; ++i )
	{
		kVec3 v;
		if ( Camera.Active() )
			v = Camera.origin;
		else
		{
			v = Player.Actor().Origin();
			v.z += Player.Actor().ViewHeight();
		}
		v += kVec3(0,1,0).Randomize(1) * Math::RandRange( 128, 768 );
		// only spawn if roughly within level bounds
		if ( v.x > -110*GAME_SCALE && v.x < 130*GAME_SCALE &&
			v.y > -139*GAME_SCALE && v.y < 101*GAME_SCALE &&
			v.z > GAME_SCALE )
				Game.SpawnFx( "fx/pollen.kfx", v, -1 );
	}
	$restart;
}

//==============================================================================
//    Bird Event 1
//
// birds need individual TIDs so they can have cliff flags changed individually
// so the event needs to be a level script, so it can trigger them both
//==============================================================================

$script 4
{
	if ( instigator is null || instigator.Flags() & AF_ENTEREDAREAEVENT != 0 )
		return;
	World.TriggerActorsByTID( instigator, TID_Bird1_1 );
	World.TriggerActorsByTID( instigator, TID_Bird1_2 );
}

//==============================================================================
//    Credits
//==============================================================================

$script 9
{
	if ( instigator is null || instigator.Flags() & AF_ENTEREDAREAEVENT != 0 )
		return;

	Game.PrintLine( "by Smoke39", 0, 999999 );
	Game.PrintLine( " ", 1, 999999 );
	Game.PrintLine( "<Jungle Sanctuary>", 2, 999999 );

	Camera.StartCinematic( CMF_LOCK_PLAYER | CMF_NO_LETTERBOX | CMF_NO_INITIAL_FADEOUT );
	Camera.fov = 74.0f;
	Camera.SetFinalView( 0 );

	// monkey
	kVec3 look1( -405, 358, 819 );
	// bellow bullet box
	kVec3 look2 = kVec3( 174, -153, 358 ) + kVec3( 0, 0, -75 );
	// above bullet box
	kVec3 cam1( 175, -155, 370 );
	// far side of bridged columns
	kVec3 cam2 = kVec3( 610, 365, 900 ) + kVec3( -25, -25, -50 );
	float fov1 = 5;
	float fov2 = 74;
	float t = 0;
	while ( true )
	{
//		float f = ( 1 - Math::Cos( Math::pi*t ) ) / 2;
		float f = Math::Cos( Math::pi*t );
		if ( t > 0.5f )
		{
			f += 1;
			f *= f / 2;
			f = 1-f;
		}
		else
		{
			f = 1-f;
			f *= f / 2;
		}

		kVec3 v = cam1;
		v.Lerp( cam2, f );
		Camera.SetEyeView( v );

		kVec3 look = look1;
		look.Lerp( look2, f );
		Camera.SetFocusView( look );

		Camera.fov = Math::Lerp( fov1, fov2, f );

		delay(0);

		// don't move or cancel until camera fades in fully
		if ( Camera.CinematicState() < 3 )
			continue;
		// done, or cancelled
		if ( t >= 1 || Camera.UserInterrupted() )
			break;
		t += GAME_DELTA_TIME/10;
		if ( t > 1 )
			t = 1;
	}

	for ( t=0; t<1 && !Camera.UserInterrupted(); t+=GAME_DELTA_TIME )
		delay(0);

	JAM::WarpToNextCreditsMap();
	//PlayLoop.StartWarp( instigator, 10000, 4 );
}
