//====================================================================
// Main Stage
//====================================================================
//--------------------------------------------------------------------
// Danmakufu Stage loading
//--------------------------------------------------------------------

#TouhouDanmakufu[Stage]
#ScriptVersion[2]
#Title[Lost in the Bamboo world! Or Forest...]
#Text[I recommend playing as Marisa for this one.]
#BackGround[User]
//BGM[.\Sound\BGM\BGM.mp3]
#Player[FREE]

//--------------------------------------------------------------------
// start the script
//--------------------------------------------------------------------

script_stage_main{

//--------------------------------------------------------------------
// Include stuff
//--------------------------------------------------------------------

	#include_function ".\Stage\TStage.dnh"
	#include_function ".\System\Font.dnh"

//--------------------------------------------------------------------
// Variables
//--------------------------------------------------------------------

	/** Enemy boss file */
	let BossMokou = GetCurrentScriptDirectory() ~ "Mokou0.dnh";
	/** Stage count */
	let StageCount = 0;
	/** Stage Frame */
	let ImageFrame = GetCurrentScriptDirectory() ~ "Image\Frame.png";
	/** Music directory */
	let BGM = GetCurrentScriptDirectory() ~ "Sound\BGM\BGM.mp3";	
	/** A string */
	let TString = "";
	/** Alpha~! */
	let Alpha = 110;
	/** pOS STUFF */
	let PosX = GetCenterX() - 160;
	let PosY = GetCenterY() + 210;
	/** String array */
	let stringarray=["0","1","2","3","4","5","6","7","8","9"];

//--------------------------------------------------------------------
// @Initialize and stuff
//--------------------------------------------------------------------

	@Initialize{
		ExpertEx( false, 3, 5 );
		LoadGraphic( ImageFrame ); LoadMusic( BGM );
		SetRateScoreSystemEnable( false );
		StageTask; DrawFrame; CommonInit;
	}

	@MainLoop{ 
		BGMainLoop;
		yield; 
	}
	@Finalize{}
	@BackGround{
		DrawBackground( StageCount );
		StageCount ++;
	}

	/**
	*	Summon Mokou~ yay!
	*/
	task StageTask{
		loop( 120 ){ yield; }
		DrawBGM();
		PlayMusic( BGM );
		loop( 60 ){ yield; }
		CreateEnemyBossFromFile( BossMokou, GetClipMinX() - 160, GetClipMinY(), 0, 0, 0 );
		WaitForZeroEnemy(); loop( 150 ){ yield; }
		Clear;
	}

	/**
	*	Durr
	*/
	task DrawBGM{
		_DrawBGM;
		TString = "Reach for the Moon ~ Immortal Stroke";
		loop( 120 ){
			DrawTexText( TString, PosX, PosY, 0.53, 5, Alpha );
			yield;
		}
		TString = "";
	}

	/**
	*	Draw the BGM name
	*/
	task _DrawBGM{
		yield;
		PosX = GetCenterX() - 161;
		yield;
		PosX = GetCenterX() - 162;
		yield;
		PosX = GetCenterX() - 163;
		yield;
		PosX = GetCenterX() - 164;
		yield;
		PosX = GetCenterX() - 165;
		yield;
		PosX = GetCenterX() - 166;
		yield;
		PosX = GetCenterX() - 167;
		yield;
		PosX = GetCenterX() - 168;
		yield;
		PosX = GetCenterX() - 169;
		yield;
		PosX = GetCenterX() - 170;
		yield;
		PosX = GetCenterX() - 171;
		yield;
		PosX = GetCenterX() - 172;
		yield;
		PosX = GetCenterX() - 173;
		yield;
		PosX = GetCenterX() - 174;
		yield;
		PosX = GetCenterX() - 175;
		yield;
		PosX = GetCenterX() - 176;
		yield;
		PosX = GetCenterX() - 177;
		yield;
		PosX = GetCenterX() - 178;
		yield;
		PosX = GetCenterX() - 179;
		yield;
		PosX = GetCenterX() - 180;
	}

	/**
	*	Wait for the existing enemy to be shot down.
	*/
	function WaitForZeroEnemy(){
		while( GetEnemyNum != 0 ){ yield; }
	}

	/**
	*	Draw the black STG_Frame
	*/
	task DrawFrame{
		yield;
		let obj = Obj_Create( OBJ_EFFECT );
		ObjEffect_SetRenderState( obj, ALPHA );
		ObjEffect_SetTexture( obj, ImageFrame );
		ObjEffect_SetLayer( obj, 8 );
		ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLEFAN );
		ObjEffect_CreateVertex( obj, 4 );
		ObjEffect_SetVertexXY( obj, 0, 0, 0 );
		ObjEffect_SetVertexXY( obj, 1, 640, 0 );
		ObjEffect_SetVertexXY( obj, 2, 640, 480 );
		ObjEffect_SetVertexXY( obj, 3, 0, 480 );	
		ObjEffect_SetVertexUV( obj, 0, 640, 0 );
		ObjEffect_SetVertexUV( obj, 1, 1280, 0 );
		ObjEffect_SetVertexUV( obj, 2, 1280, 480 );
		ObjEffect_SetVertexUV( obj, 3, 640, 480 );
		ascent( let i in 0 .. 4 ){ ObjEffect_SetVertexColor( obj, i, 255, 255, 255, 255 ); }
		Obj_SetPosition( obj, 0, 0 );
	}

	/**
	*	Int string!
	*/
	function RealToIntString(let num){
        	real lp=floor(log10(num))+1;
        	real lp1=lp;
        	if(lp==0){result="0"}else{
        		result = "";
        		loop(lp){
        			result=result~ stringarray[(num%(10^lp1)-num%(10^(lp1-1)))/(10^(lp1-1))];
        			lp1--;
        		}
        	}
        }

}

