#TouhouDanmakufu[Stage]
#Title[ReimuGinko]
#Text[Battle with the bull maiden !]
#Player[FREE]
#ScriptVersion[2]

script_stage_main {
	//let header = GetCurrentScriptDirectory~".\images\header.png";
	let name= GetCurrentScriptDirectory~".\system\name.png";
	let csd = GetCurrentScriptDirectory;
	let BG = csd~".\bg.png";
	let FORE = csd~".\fore2.png";
	let UFO = csd~".\ufo.png";
	let c = 0;
	let a = 0;
	let foreg=0;
	let trigger=false;
	let pointnorm=50;
	let start=0;
	let partframe=0;
	let NameOff=0;
	//let headertime=460;
	let scalex=4;
	let alps=255;
	let scaley=0;
	let SP = GetCurrentScriptDirectory~"Enemy\sp\sp.png";
   let m = -1400;
   let n = -700;
   let o = -1000;
   let l = 0;
   let f2 = 0;
   let f3 = 0;
   let f4 = 0;
   let f5 = 0;
   let f6 = 0;
   let f7 = 0;
   let f = 0;
   let nbg = GetCurrentScriptDirectory ~ "ef\nbg1.png";
   let nbg2 = GetCurrentScriptDirectory ~ "ef\nbg2.png";
   let nbg3 = GetCurrentScriptDirectory ~ "ef\nbg3.png";
   let ST = GetCurrentScriptDirectory ~ "mu\stbgm.png";
   let BO  = GetCurrentScriptDirectory ~ "Boss\mu\bgm.png";
   let depth_buffer_enable = false;
   
	task StageTask
	{	
	
	yield;

		function Wait(let frame)
		{
			loop(frame){yield};
		}
		function WaitForZeroEnemy()
		{
			while(GetEnemyNum() != 0){yield;}
		}

	#include_function ".\StageF.txt";
		stageF1C;
		Clear;


	}
	task Foreground{
	let obj=Obj_Create(OBJ_EFFECT);
	Obj_SetX(obj,192);
	Obj_SetY(obj,192);
	ObjEffect_SetLayer(obj,8);
	ObjEffect_SetTexture(obj,FORE);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetRenderState(obj,ALPHA);
	ObjEffect_SetAngle(obj,0,0,0);

let s = 1;
ObjEffect_SetScale(obj,s,s);

ObjEffect_CreateVertex(obj,4);
ObjEffect_SetVertexUV(obj,0,0,0);
ObjEffect_SetVertexUV(obj,1,640,0);
ObjEffect_SetVertexUV(obj,2,640,480);
ObjEffect_SetVertexUV(obj,3,0,480);

ObjEffect_SetVertexXY(obj,0,-192,-192);
ObjEffect_SetVertexXY(obj,1,448,-192);
ObjEffect_SetVertexXY(obj,2,448,288);
ObjEffect_SetVertexXY(obj,3,-192,288);

	loop{yield;}
	}

task CountScore(i){
loop{if(GetPoint >=i){break}yield;}
	let obj=Obj_Create(OBJ_EFFECT);
	let s = 1.5;
	Obj_SetX(obj,510+60*cos(36*i));
	Obj_SetY(obj,90+60*sin(36*i));
	ObjEffect_SetLayer(obj,8);
	ObjEffect_SetTexture(obj,UFO);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetRenderState(obj,ADD);
	ObjEffect_SetAngle(obj,0,0,0);


ObjEffect_SetScale(obj,s,s);

ObjEffect_CreateVertex(obj,4);
ObjEffect_SetVertexUV(obj,0,100,0);
ObjEffect_SetVertexUV(obj,1,200,0);
ObjEffect_SetVertexUV(obj,2,200,60);
ObjEffect_SetVertexUV(obj,3,100,60);

ObjEffect_SetVertexXY(obj,0,0,0);
ObjEffect_SetVertexXY(obj,1,50,0);
ObjEffect_SetVertexXY(obj,2,50,30);
ObjEffect_SetVertexXY(obj,3,0,30);

	
}	
	
task CountLife(i){
	let obj=Obj_Create(OBJ_EFFECT);
	let s = 3/3;
	Obj_SetX(obj,-10);
	Obj_SetY(obj,5+30*s*i);
	ObjEffect_SetLayer(obj,8);
	ObjEffect_SetTexture(obj,UFO);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetRenderState(obj,ALPHA);
	ObjEffect_SetAngle(obj,0,0,0);


ObjEffect_SetScale(obj,s,s);

ObjEffect_CreateVertex(obj,4);
ObjEffect_SetVertexUV(obj,0,0,0);
ObjEffect_SetVertexUV(obj,1,100,0);
ObjEffect_SetVertexUV(obj,2,100,60);
ObjEffect_SetVertexUV(obj,3,0,60);

ObjEffect_SetVertexXY(obj,0,0,0);
ObjEffect_SetVertexXY(obj,1,50,0);
ObjEffect_SetVertexXY(obj,2,50,30);
ObjEffect_SetVertexXY(obj,3,0,30);

loop{if(GetPlayerLife<=i){Obj_Delete(obj);}yield;}	
}

task CountBomb(i){
	loop{let obj=Obj_Create(OBJ_EFFECT);
	let s = 3/3;
	Obj_SetX(obj,-10);
	Obj_SetY(obj,420-30*s*i);
	ObjEffect_SetLayer(obj,8);
	ObjEffect_SetTexture(obj,UFO);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetRenderState(obj,ALPHA);
	ObjEffect_SetAngle(obj,0,0,0);


ObjEffect_SetScale(obj,s,s);

ObjEffect_CreateVertex(obj,4);
ObjEffect_SetVertexUV(obj,0,200,0);
ObjEffect_SetVertexUV(obj,1,300,0);
ObjEffect_SetVertexUV(obj,2,300,60);
ObjEffect_SetVertexUV(obj,3,200,60);

ObjEffect_SetVertexXY(obj,0,0,0);
ObjEffect_SetVertexXY(obj,1,50,0);
ObjEffect_SetVertexXY(obj,2,50,30);
ObjEffect_SetVertexXY(obj,3,0,30);

		
	loop{yield;if(GetPlayerBomb<i){Obj_Delete(obj);}
	if(OnPlayerMissed()){Obj_Delete(obj);break;}}}
	}




	@Initialize {
		LoadGraphic(BG);
		LoadGraphic(FORE);
		LoadGraphic(UFO);
		LoadGraphic(nbg);
      LoadGraphic(nbg2);
      LoadGraphic(nbg3);
      LoadGraphic(SP);
		SetRateScoreSystemEnable(false);
		
		StageTask;
		
	}

	@MainLoop {
	
	if(foreg==0){ExtendPlayer(3);Foreground;ascent(i in 1..8){CountLife(i)};ascent(i in 1..4){CountBomb(i)};ascent(i in 1..11){CountScore(i)};foreg=1}
	yield;
		partframe++;
		if(partframe%5==0 && OnEnemySpell==false){
			//particles(rand(GetClipMinX,GetClipMaxX),GetClipMinY,rand(0.1,0.5));
		}
	
		if(trigger==false){c +=0.05;}else if(trigger==true){c-= 0.05}
		if(c >= 80) {trigger=true}
		if(c<= -80){trigger=false}
		
		
		a += 5;					
		if(a >= 512) {				
			a -= 512;				
		}

	          SetNormPoint(pointnorm);
	        if(GetPoint>=2000&&pointnorm==50){
	            ExtendPlayer(1);
	            pointnorm = 100;
	        }
	        if(GetPoint>=100&&pointnorm==100){
	            ExtendPlayer(1);
	            pointnorm = 200;
	        }
	        if(GetPoint>=200&&pointnorm==200){
	            ExtendPlayer(1);
	            pointnorm = 9999;
	        }


	}

	@Finalize {
		DeleteGraphic(csd~".\images\stage02a.png");	
		DeleteGraphic(csd~".\images\stage02b.png");	
		DeleteGraphic(csd~".\images\stage02c.png");
			
	}

	@BackGround {


		WriteZBuffer(depth_buffer_enable);
      UseZBuffer(depth_buffer_enable);  

      SetViewTo(0, -60, 80);
      SetViewFrom(400, 80, 35);     

	  SetAlpha(255);
        
	  
      SetAlpha(255);
      SetTexture(nbg);
      SetGraphicRect(-10000, -10000, 100000, 1000000);
      SetGraphicAngle(90, 0, 0);
      SetGraphicScale(1,1);
      DrawGraphic3D(0, -168, f);

      
	  
	  f=(f+5)%225;
	 
	  
      SetAlpha(200);
      



if (f2 == 400){
m = -1400;
f2 = 0;
}
if (f3 == 200){
n = -1400;
f3 = -200;
}
if (f4 == 300){
o = -1400;
f4 = -100;
}
f2++;
f3++;
f4++;
f5 += 0.4;
if(f5<90){SetViewFrom(400, 80, 45)}
if(f5>=90){SetViewFrom(400+30*cos(f5), 80+30*cos((f5+90)/2), 45+10*cos((f5+180)/3));}
	}




	

	

}