#TouhouDanmakufu[Stage]
#Title[Reimu]
#Text[One way or another, this will end in heavens ... Don't try this, seriously]
#BackGround[User(.\img\stage.png,0,-0.5)]
#Player[FREE]
#ScriptVersion[2]

script_stage_main {
	//let header = GetCurrentScriptDirectory~".\images\header.png";
	let name= GetCurrentScriptDirectory~".\system\name.png";
	let csd = GetCurrentScriptDirectory;
	let FORE = csd~".\fore2.png";
	let SpellBG01 = csd~"img/Satori/SpellBG01.png";
	let SpellBG02 = csd~"img/Satori/SpellBG02.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;
	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;}
	}






	@Initialize {
		
		LoadGraphic(FORE);
		SetRateScoreSystemEnable(false);
		LoadGraphic(SpellBG01);
		LoadGraphic(SpellBG02);
		StageTask;
		
	}

	@MainLoop {
	
	if(foreg==0){Foreground;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>=0&&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 {
		
			
	}

	let bgalpha = 0;
let bgangle = 0;
let bgscroll = 0;

@BackGround
	{
	if (bgalpha < 255){bgalpha += 5;}
	bgangle += 0.3;
	bgscroll += 1;

	SetAlpha(bgalpha);
	SetGraphicScale(2,2);
	SetColor(255,255,255);

	SetTexture(SpellBG01);
SetGraphicRect(0,0,512+bgscroll,512);

	DrawGraphic(GetCenterX,GetCenterY);
SetGraphicScale(1,1);
	SetTexture(SpellBG02);
	SetGraphicRect(0,0,256,256);
	SetGraphicAngle(0,0,bgangle);
	DrawGraphic(GetCenterX,GetCenterY);
	}






   task particles(x,y,scale){
	let particles = GetCurrentScriptDirectory~"\system\eff0.png";
	LoadGraphic(particles);
	let obj = Obj_Create(OBJ_EFFECT);
	let alp = 255;
	let tr=0;
	let num1;
	let num2;
	let color1=rand_int(50,10);
	let color2=rand_int(80,10);
	let color3=rand_int(100,200);
	let y2=0;
	let randnum=rand_int(1,4);
	let yr=rand(3,7);
	let an=rand(1,3);
	let r=rand(1.5,3);
		if(randnum==1){num1=0;num2=8}
		if(randnum==2){num1=8;num2=16}
		if(randnum==3){num1=16;num2=24}
		if(randnum==4){num1=24;num2=32}
	ObjEffect_SetRenderState(obj,ADD);
	ObjEffect_SetTexture(obj,particles);
	ObjEffect_SetScale(obj,scale,scale);	
	ObjEffect_SetLayer(obj,1);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(obj, 4);
	Obj_SetPosition(obj,x,y-y2);
	while(! Obj_BeDeleted(obj)) {
		Obj_SetPosition(obj,x,y+y2/r);
		if(Obj_GetX(obj)<=GetCenterX){
		
			x-=rand(0,0.9);
		}
		if(Obj_GetX(obj)>GetCenterX){
			x+=rand(0,0.9);
		}
		ObjEffect_SetVertexXY(obj, 0, -11, -11);
		ObjEffect_SetVertexXY(obj, 1, 11, -11);
		ObjEffect_SetVertexXY(obj, 2, -11, 11);
		ObjEffect_SetVertexXY(obj, 3, 11, 11);

		
		ObjEffect_SetVertexUV(obj, 0, num1,10);
		ObjEffect_SetVertexUV(obj, 1, num2, 10);
		ObjEffect_SetVertexUV(obj, 2, num1, 20);
		ObjEffect_SetVertexUV(obj, 3, num2, 20);
		ascent(i in 0..4){ObjEffect_SetVertexColor(obj,i,alp,color1-tr,color2-tr,color3);}
		tr-=2;
		y2+=yr;
		an++;
		ObjEffect_SetAngle(obj,an,an,an);
		ObjEffect_SetScale(obj,scale,scale);
		alp-=0.1;
		if(alp<=0){alp=0;}
		if(tr<0){tr=0;} 
		if(Obj_GetY(obj)>=GetClipMaxY+20){Obj_Delete(obj);}
		yield;
	}

    }



	@DrawBottomObject {
	}

	

}