#include".\StageFunctions.txt"

task charge(x,y,time){
		let dplus = 0;
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		LoadTexture(GetCurrentScriptDirectory~"img/charge.png");
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"img/charge.png");
		ObjPrim_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj,4);
		ObjPrim_SetVertexUVT(obj,0,0,0);
		ObjPrim_SetVertexUVT(obj,1,0,296);
		ObjPrim_SetVertexUVT(obj,2,296,0);
		ObjPrim_SetVertexUVT(obj,3,296,296);
		ObjPrim_SetVertexPosition(obj,0,-148,-148,0);
		ObjPrim_SetVertexPosition(obj,1,-148,148,0);
		ObjPrim_SetVertexPosition(obj,2,148,-148,0);
		ObjPrim_SetVertexPosition(obj,3,148,148,0);
		Obj_SetRenderPriorityI(obj,24);
		ObjRender_SetBlendType(obj,BLEND_ALPHA);
		ObjRender_SetAlpha(obj,100);
		ObjRender_SetPosition(obj,ex,ey,0);
		loop(15){maplecharge(); yield;}
		loop(75){
			ObjRender_SetPosition(obj,ex,ey,0);
			ObjRender_SetScaleXYZ(obj,1-dplus,1-dplus,1-dplus);
			dplus+=1/75;
			yield;
			if(!objEnemy){
			Obj_Delete(obj);
			}
			}
		Obj_Delete(obj);
		}

task SpellName(text,TR,TG,TB,BR,BG,BB){
let objTxt = ObjText_Create();
let c = 0;
ObjText_SetText(objTxt,text);
ObjText_SetHorizontalAlignment(objTxt,ALIGNMENT_LEFT);
ObjText_SetFontSize(objTxt,12);
ObjText_SetFontColorTop(objTxt,TR,TG,TB);
ObjText_SetFontColorBottom(objTxt,BR,BG,BB);
ObjText_SetFontBorderType(objTxt,BORDER_FULL);
ObjText_SetFontBorderColor(objTxt,0,0,0);
ObjText_SetFontBorderWidth(objTxt,2);
loop(120){
	ObjRender_SetPosition(objTxt,0,GetStgFrameHeight-c,0);
	c+=((GetStgFrameHeight-32)/120);
	yield;
	}
	
loop{
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		Obj_Delete(objTxt);
		}
	yield;
}
}
task score(SScore,TR,TG,TB,BR,BG,BB){
SetCommonData("Score",SScore);
let objTxt = ObjText_Create();
let l = GetPlayerLife;
let b = GetPlayerSpell;
let l2;
let b2;
let n;
ObjText_SetHorizontalAlignment(objTxt,ALIGNMENT_LEFT);
ObjText_SetFontSize(objTxt,14);
ObjText_SetFontColorTop(objTxt,TR,TG,TB);
ObjText_SetFontColorBottom(objTxt,BR,BG,BB);
ObjText_SetFontBorderType(objTxt,BORDER_FULL);
ObjText_SetFontBorderColor(objTxt,0,0,0);
ObjText_SetFontBorderWidth(objTxt,2);
ObjRender_SetPosition(objTxt,0,48,0);
loop{
	l2 = GetPlayerLife;
	b2 = GetPlayerSpell;
	n = "Score: " ~ IntToString(SScore);
	ObjText_SetText(objTxt,n);
	if(l2!=l || b2!=b){ 
		SScore=0;
		break;
	}
	if(SScore>0){
		SScore-=1/30;
	} else{
		SScore = 0;
		break;
	}
	SetCommonData("Score",SScore);
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		SetCommonData("scorecheck",1);
		Obj_Delete(objTxt);
		}
	yield;
	}
	n = "Score: " ~ IntToString(SScore);
	ObjText_SetText(objTxt,n);
loop{
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		SetCommonData("scorecheck",2);
		Obj_Delete(objTxt);
		}
	yield;
	}
}

task SubSpellName(text,TR,TG,TB,BR,BG,BB,T){
let objTxt = ObjText_Create();
let c = 0;
ObjText_SetText(objTxt,text);
ObjText_SetHorizontalAlignment(objTxt,ALIGNMENT_LEFT);
ObjText_SetFontSize(objTxt,14);
ObjText_SetFontColorTop(objTxt,TR,TG,TB);
ObjText_SetFontColorBottom(objTxt,BR,BG,BB);
ObjText_SetFontBorderType(objTxt,BORDER_FULL);
ObjText_SetFontBorderColor(objTxt,0,0,0);
ObjText_SetFontBorderWidth(objTxt,2);
ObjRender_SetPosition(objTxt,0,60,0);
loop(255){
	ObjRender_SetAlpha(objTxt,c);
	c++;
	yield;
	}
loop(T-510){yield;}
loop(255){
	ObjRender_SetAlpha(objTxt,c);
	c--;
	yield;
	}
		Obj_Delete(objTxt);
}

task cutin(image,l,t,r,d){
let slide = 0;
let alpha = 0;
let objBack = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(objBack,image);
Obj_SetRenderPriorityI(objBack,50);
DelaySE(GetMainStgScriptDirectory~"ZUN/se_cat00.wav",0,9,0);
loop(25){
	ObjSprite2D_SetSourceRect(objBack,l,t,r,d);
	ObjSprite2D_SetDestRect(objBack,-150+slide,50,-150+r+slide,50+d);
	yield;
	slide+=8;
	}
loop(75){
	ObjSprite2D_SetSourceRect(objBack,l,t,r,d);
	ObjSprite2D_SetDestRect(objBack,-150+slide,50,-150+r+slide,50+d);
	yield;
	slide+=0.5;
}
loop(50){
	ObjSprite2D_SetSourceRect(objBack,l,t,r,d);
	ObjSprite2D_SetDestRect(objBack,-150+slide,50,-150+r+slide,50+d);
	yield;
	slide+=8;
}
Obj_Delete(objBack);
}
task Magiccircle(objA,a){
let n = 0;
let c = 0;
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(obj,magic);
Obj_SetRenderPriorityI(obj,28);
ObjRender_SetBlendType(obj,BLEND_ALPHA);
ObjSprite2D_SetSourceRect(obj,0,0,400,400);
ObjRender_SetColor(obj, 100, 100, 200);
ObjSprite2D_SetDestRect(obj,-200,-200,+200,+200);			
	while(!Obj_IsDeleted(obj)){
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0||Obj_IsDeleted(objEnemy)){
			ObjRender_SetScaleXYZ(obj,0,0,0);
				Obj_Delete(obj);
			}
			ObjRender_SetAlpha(obj,150);
			ObjRender_SetPosition(obj,ObjMove_GetX(objA),ObjMove_GetY(objA)+36,0);
			ObjRender_SetScaleXYZ(obj,0.6,0.6,0.6);
			ObjRender_SetAngleXYZ(obj,70,0,c*a);
			c+=1;
			n+=12;
			yield;
		}
	Obj_Delete(obj);
	}

task Aura(objA,R,G,B){
let n = 0;
let posx = rand(-5,5);
let posy = rand(0,-10);
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(obj,aura);
Obj_SetRenderPriorityI(obj,25);
ObjRender_SetBlendType(obj,BLEND_ALPHA);
ObjRender_SetColor(obj,R,G,B);
ObjRender_SetAlpha(obj,100);
ObjSprite2D_SetSourceRect(obj,0,0,33,40);
ObjSprite2D_SetDestRect(obj,-17,-20,+17,+20);			
	while(!Obj_IsDeleted(obj)){
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0||Obj_IsDeleted(objEnemy)||n>=20){
				Obj_Delete(obj);
			}
			ObjRender_SetAlpha(obj,255/(n/2));
			ObjRender_SetPosition(obj,ObjMove_GetX(objA)+posx,ObjMove_GetY(objA)-posy-(n*1.5),0);
			ObjRender_SetScaleXYZ(obj,0.6,0.6+n/8,0.6);
			n+=1;
			yield;
		}
	Obj_Delete(obj);
	}
task Aura2(objA,R,G,B,angle){
let n = 0;
let posx = rand(-5,5);
let posy = rand(0,10);
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(obj,aura2);
Obj_SetRenderPriorityI(obj,26);
ObjRender_SetBlendType(obj,BLEND_ALPHA);
ObjRender_SetColor(obj,R,G,B);
ObjRender_SetAlpha(obj,200);
ObjSprite2D_SetSourceRect(obj,0,0,34,33);
ObjSprite2D_SetDestRect(obj,-17,-17,+17,+17);	
ObjRender_SetAngleXYZ(obj,0,0,angle);		
	while(!Obj_IsDeleted(obj)){
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0||Obj_IsDeleted(objEnemy)||n>=20){
				Obj_Delete(obj);
			}
			ObjRender_SetAlpha(obj,255/(n/3));
			ObjRender_SetPosition(obj,ObjMove_GetX(objA)+posx,ObjMove_GetY(objA)+posy+n/2,0);
			ObjRender_SetScaleXYZ(obj,0.5+(n/20),0.5+(n/20),0.6);
			n+=1;
			yield;
		}
	Obj_Delete(obj);
	}
task EnemyMarker(objA){
let n = 0;
let posx = rand(-5,5);
let posy = rand(0,10);
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "enemy.png");
Obj_SetRenderPriorityI(obj,81);
ObjRender_SetBlendType(obj,BLEND_ALPHA);
ObjRender_SetAlpha(obj,200);
ObjSprite2D_SetSourceRect(obj,0,0,50,20);
ObjSprite2D_SetDestRect(obj,-25,-10,+25,+10);	
	while(!Obj_IsDeleted(obj)){
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0||Obj_IsDeleted(objEnemy)||n>=20){
				Obj_Delete(obj);
			}
			ObjRender_SetPosition(obj,ObjRender_GetX(objA)+25,475,0);
			yield;
		}
	Obj_Delete(obj);
	}
task AuraMakeBoss(objA,R,G,B){
	let ang = 1;
	TWaveCircle();
		Magiccircle(objA,ang/5);
		Magiccircle(objA,-ang/5);
	//loop{
		//Aura2(objA,R,G,B,ang);
		//loop(1){yield;}
		//Aura(objA,R,G,B);
		//ang+=10;
	//}
}
task AuraMake(){
	let ang = 0;
	loop{
		Aura(objEnemy,200,50,200);
		Aura2(objEnemy,200,50,200,ang);
		loop(1){yield;}
		ang+=10;
	}
}
task EnglandMove{
	let frame = 0;
	let enx = ObjMove_GetX(objEnemy);
	ObjPrim_SetTexture(objEnemy,England);
	ObjRender_SetBlendType(objEnemy,BLEND_ALPHA);
	ObjRender_SetAlpha(objEnemy,255);
	while(!Obj_IsDeleted(objEnemy)){
			yield;
			ex = ObjMove_GetX(objEnemy);
				if(frame>=0&&frame<8){ 	ObjSprite2D_SetSourceRect(objEnemy,1,6,80,59); ObjSprite2D_SetDestCenter(objEnemy); }
				if(frame>=8&&frame<16){ 	ObjSprite2D_SetSourceRect(objEnemy,87,6,154,59);	ObjSprite2D_SetDestCenter(objEnemy); }
				if(frame>=16&&frame<24){ 	ObjSprite2D_SetSourceRect(objEnemy,161,6,240,59);	ObjSprite2D_SetDestCenter(objEnemy); }
				if(frame>=24&&frame<32){ 	ObjSprite2D_SetSourceRect(objEnemy,247,6,314,59);	ObjSprite2D_SetDestCenter(objEnemy); }
				if(frame>=32){frame = 0;}
			frame++;
		}
	}	
	task SpainMove(objA,text,x,y,s){
	let frame = rand(0,360);
	let enx = ObjMove_GetX(objA);
	ObjPrim_SetTexture(objA,text);
	ObjRender_SetBlendType(objA,BLEND_ALPHA);
	ObjRender_SetAlpha(objA,255);
	ObjRender_SetScaleXYZ(objA,s,s,0);
	while(!Obj_IsDeleted(objA)){
			yield;
			ObjSprite2D_SetSourceRect(objA,0,0,64,64); 
			ObjSprite2D_SetDestRect(objA,-x/2,-y/2+10*sin(frame),x/2,y/2+10*sin(frame));

			frame+=3;
		}
	}	


task maplecharge(){ 

let MCobj = ObjPrim_Create(OBJ_SPRITE_2D);

let mimg = GetCurrentScriptDirectory ~ "img/tree.png";

let setbossx = ObjMove_GetX(objEnemy);
let setbossy = ObjMove_GetY(objEnemy);

let rand_angle = rand(0,360);

let mtimer = 0;
let malpha = 0;

let msize = 0;

let mradius;
let mrandspeed;
let maccel = -1;

let mspinX = rand(0.5,1);
let mspinY = rand(0.5,1);
let mspinZ = rand(0.5,1);

let mangleX = rand(0,360);
let mangleY = rand(0,360);
let mangleZ = rand(0,360);
mradius = rand(100,200);

LoadTexture(mimg);
ObjPrim_SetTexture(MCobj,mimg);
ObjSprite2D_SetDestCenter(MCobj);
ObjSprite2D_SetSourceRect(MCobj,0,0,27,20);
ObjSprite2D_SetDestRect(MCobj,-27/2,-20/2,27/2,20/2);
Obj_SetRenderPriorityI(MCobj,30);


while(!Obj_IsDeleted(MCobj)){
mtimer++;

if(malpha<120){malpha+=4;}
if(malpha>120){malpha=120;}

ObjRender_SetPosition(MCobj,ObjMove_GetX(objEnemy)+cos(rand_angle)*mradius,ObjMove_GetY(objEnemy)+sin(rand_angle)*mradius,0);
ObjRender_SetScaleXYZ(MCobj,GetObjectDistance(MCobj,objEnemy)/50+0.5,GetObjectDistance(MCobj,objEnemy)/50+0.5,GetObjectDistance(MCobj,objEnemy)/50+0.5);

ObjRender_SetAlpha(MCobj,malpha);

ObjRender_SetAngleXYZ(MCobj,mangleX,mangleY,mangleZ);

mangleX+=mspinX;
mangleY+=mspinY;
mangleZ+=mspinZ;

mradius-=maccel;
maccel+=0.1;

msize-=0.05;

if(msize<0){msize=0;}

if(mradius<=0){Obj_Delete(MCobj);}


yield;
}
}

task TWaveCircle()
{
	//ƒŒƒ“ƒ_ƒŠƒ“ƒOƒ^[ƒQƒbƒg‚ÉŽg—p‚·‚éƒeƒNƒXƒ`ƒƒ
	let renderTexture = GetReservedRenderTargetName(0);

	let frame = 0; //ƒtƒŒ[ƒ€
	let baseEffectRadius = 256; //Šî€ƒGƒtƒFƒNƒg”¼Œa
	let outerFluct = 64; //ƒGƒtƒFƒNƒg”¼Œa‚ÌÅ‘å•Ï‰»—Ê
	let effectRadius = 0; //ƒGƒtƒFƒNƒg”¼Œa


	let priEffectMin = 20; //ƒGƒtƒFƒNƒg‚ð‚©‚¯‚éÅ¬—Dæ“x
	let priEffectMax = 28; //ƒGƒtƒFƒNƒg‚ð‚©‚¯‚éÅ‘å—Dæ“x

	//”wŒi‚Ì‚ÝƒGƒtƒFƒNƒg‚Ì‘ÎÛ‚Æ‚·‚é
	//ƒGƒtƒFƒNƒg‚Ì•`‰æ‚Å‚Ü‚©‚È‚¦‚é‚½‚ßA
	//—Dæ“x20`28‚Ì’Êí•`‰æ‚ð–³Œø‚É‚·‚éB
	SetInvalidRenderPriorityA1(priEffectMin, priEffectMax);

	let frameWidth = GetStgFrameWidth();
	let frameHeight = GetStgFrameHeight();
	let frameLeft = GetStgFrameLeft();
	let frameRight = frameLeft + frameWidth;
	let frameTop = GetStgFrameTop();
	let frameBottom = frameTop + frameHeight;


	//--------------------------------
	//‚ä‚ª‚ÝƒIƒuƒWƒFƒNƒg
	let objWave = ObjPrim_Create(OBJ_SPRITE_2D); //2DƒXƒvƒ‰ƒCƒgƒIƒuƒWƒFƒNƒg¶¬
	Obj_SetRenderPriorityI(objWave, 25); //•`‰æ—Dæ“x‚ðÝ’è
	ObjPrim_SetTexture(objWave, renderTexture); //ƒeƒNƒXƒ`ƒƒ‚ðÝ’è
	ObjSprite2D_SetSourceRect(objWave, frameLeft, frameTop, frameRight, frameBottom);
	ObjSprite2D_SetDestRect(objWave, 0, 0, frameWidth, frameHeight);
	Obj_SetRenderPriorityI(objWave, priEffectMax + 1);

	//‚ä‚ª‚ÝƒIƒuƒWƒFƒNƒg‚ÉƒVƒF[ƒ_‚ðÝ’è
	let pathShader = GetCurrentScriptDirectory ~ "SamplePS03_HLSL.txt";
	ObjShader_SetShaderF(objWave, pathShader);
	ObjShader_SetTechnique(objWave, "TecWave");


	//ƒ{ƒX‚Ìƒ‰ƒCƒt‚ª0‚É‚È‚é‚Ü‚ÅƒGƒtƒFƒNƒg‚ð‚©‚¯‚éB
	let objEnemy = GetEnemyBossObjectID[0];
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		//ƒGƒtƒFƒNƒg”¼Œa
		effectRadius = baseEffectRadius + outerFluct * sin(frame*4);

		let enemyX = ObjMove_GetX(objEnemy); //“GÀ•WX
		let enemyY = ObjMove_GetY(objEnemy); //“GÀ•WY

		//--------------------------------
		//—Dæ“x20`28(”wŒi)‚ðƒGƒtƒFƒNƒg—p‚ÌƒeƒNƒXƒ`ƒƒ‚É•`‰æ
		//ƒVƒF[ƒ_‰ðœ
		RenderToTextureA1(renderTexture, priEffectMin, priEffectMax, true);

		//--------------------------------
		//ƒVƒF[ƒ_‚Éƒpƒ‰ƒ[ƒ^‚ðÝ’è
		ObjShader_SetFloat(objWave, "frame_", frame);
		ObjShader_SetFloat(objWave, "enemyX_", enemyX + frameLeft);
		ObjShader_SetFloat(objWave, "enemyY_", enemyY + frameTop);
		ObjShader_SetFloat(objWave, "waveRadius_", effectRadius);

		frame++;
		yield;
	}

	//ƒGƒtƒFƒNƒg—pƒIƒuƒWƒFƒNƒgíœ
	Obj_Delete(objWave);
	ClearInvalidRenderPriority();
}