#include"./system/Load_AllTexture.dnh"

task Aura(control,r,g,b){
	/*本家東方のオーラ*/
	let count	= 0;
	let EC		= GetAreaCommonData("Option","Effect_Cut",3);
	
	while(!Obj_IsDeleted(enm) && EC<3){
		count++;
		if(count==1){
			Aura_Source1(r,g,b);
			count=(control-1)*-1;
		}
		Aura_Source2(r,g,b);
		Distortion();
		yield;
	}
}

task Aura_Source1(r,g,b){
	let aeffu		= ObjPrim_Create(OBJ_SPRITE_2D);
	let aeff		= ObjPrim_Create(OBJ_SPRITE_2D);
	let delete		= false;
	let a			= 0;
	let r1			= 0;
	let g1			= 0;
	let b1			= 0;
	let i			= 0;
	let alpha1		= 0;
	let alphaA		= 0;
	let scax1		= 0;
	let scaxA		= 0;
	let scay1		= rand(1,2);
	let scayA		= 0;
	let ang			= rand(-16,16);
	let EnmX		= ObjMove_GetX(enm);
	let EnmY		= ObjMove_GetY(enm);
	
	ObjRender_SetPosition(aeffu,EnmX,EnmY,0);
	ObjPrim_SetTexture(aeffu,effect_tex[0]);
	Obj_SetRenderPriorityI(aeffu,24);
	ObjRender_SetBlendType(aeffu,BLEND_SUBTRACT);
	ObjRender_SetAngleXYZ(aeffu,0,0,180+ang*-2);
	ObjRender_SetScaleXYZ(aeffu,scax1,scay1,0);
	ObjSprite2D_SetSourceRect(aeffu,7,131,43,173);
	ObjSprite2D_SetDestCenter(aeffu);
	ObjRender_SetColor(aeffu,r1,g1,b1);
	ObjRender_SetAlpha(aeffu,1001);
	
	ObjRender_SetPosition(aeff,EnmX,EnmY,0);
	ObjPrim_SetTexture(aeff,effect_tex[0]);
	Obj_SetRenderPriorityI(aeff,24);
	ObjRender_SetBlendType(aeff,BLEND_ADD_RGB);
	ObjRender_SetAngleXYZ(aeff,0,0,ang);
	ObjRender_SetScaleXYZ(aeff,scax1,scay1,0);
	ObjSprite2D_SetSourceRect(aeff,7,131,43,173);
	ObjSprite2D_SetDestCenter(aeff);
	ObjRender_SetColor(aeff,r1,g1,b1);
	ObjRender_SetAlpha(aeff,100);
	
	while(!Obj_IsDeleted(enm)){
		let EnmX		= ObjMove_GetX(enm);
		let EnmY		= ObjMove_GetY(enm);
	
		if(scax1<1){scaxA+=10;}
		scax1=1*sin(scaxA);
		if(scay1<1.5){scayA+=10;}
		scay1=1.5*sin(scayA);
		ObjRender_SetScaleXYZ(aeffu,2.25-scax1,scay1,0);
		ObjRender_SetScaleXYZ(aeff,2.5-scax1,scay1,0);
		
		if(r1<r&&delete==false){r1+=truncate(r/15);}
		if(g1<g&&delete==false){g1+=truncate(g/15);}
		if(b1<b&&delete==false){b1+=truncate(b/15);}
		if(r1>0&&delete==true){r1-=truncate(r/15);}
		if(g1>0&&delete==true){g1-=truncate(g/15);}
		if(b1>0&&delete==true){b1-=truncate(b/15);}
		if(r1>=r&&g1>=g&&b1>=b){delete=true;}
		ObjRender_SetColor(aeffu,r1,g1,b1-64);
		ObjRender_SetColor(aeff,r1,g1,b1);
		
		a+=3;
		ObjRender_SetPosition(aeffu,EnmX,EnmY+16-a,0);
		ObjRender_SetPosition(aeff,EnmX,EnmY-a,0);
		
		if(delete==true&&r1<=0&&g1<=0&&b1<=0){
			Obj_Delete(aeffu);
			Obj_Delete(aeff);
			break;
		}
		yield;
	}
	Obj_Delete(aeffu);
	Obj_Delete(aeff);
}

task Aura_Source2(r,g,b){
	let aeffu		= ObjPrim_Create(OBJ_SPRITE_2D);
	let aeff		= ObjPrim_Create(OBJ_SPRITE_2D);
	let alpha		= 0;
	let delete		= false;
	let r1			= 0;
	let g1			= 0;
	let b1			= 0;
	let scaxmx		= truncate(rand(1,5));
	let scaymx		= scaxmx;
	let scax1		= 8;
	let scaxA		= 0;
	let scay1		= 8;
	let scayA		= 0;
	let EnmX		= ObjMove_GetX(enm);
	let EnmY		= ObjMove_GetY(enm);
	
	ObjRender_SetPosition(aeffu,EnmX,EnmY,0);
	ObjPrim_SetTexture(aeffu,effect_tex[0]);
	Obj_SetRenderPriorityI(aeffu,23);
	ObjRender_SetBlendType(aeffu,BLEND_SUBTRACT);
	ObjRender_SetAngleXYZ(aeffu,0,0,rand(0,360));
	ObjRender_SetScaleXYZ(aeffu,scax1,scay1,0);
	ObjSprite2D_SetSourceRect(aeffu,53,136,89,170);
	ObjSprite2D_SetDestCenter(aeffu);
	ObjRender_SetColor(aeffu,r1,g1,b1);
	ObjRender_SetAlpha(aeffu,alpha);
	
	ObjRender_SetPosition(aeff,EnmX,EnmY,0);
	ObjPrim_SetTexture(aeff,effect_tex[0]);
	Obj_SetRenderPriorityI(aeff,23);
	ObjRender_SetBlendType(aeff,BLEND_ADD_RGB);
	ObjRender_SetAngleXYZ(aeff,0,0,rand(0,360));
	ObjRender_SetScaleXYZ(aeff,scax1,scay1,0);
	ObjSprite2D_SetSourceRect(aeff,53,136,89,170);
	ObjSprite2D_SetDestCenter(aeff);
	ObjRender_SetColor(aeff,r1,g1,b1);
	ObjRender_SetAlpha(aeff,alpha);
	
	while(!Obj_IsDeleted(enm)){
		let EnmX		= ObjMove_GetX(enm);
		let EnmY		= ObjMove_GetY(enm);
		
		ObjRender_SetPosition(aeffu,EnmX,EnmY,0);
		ObjRender_SetPosition(aeff,EnmX,EnmY,0);
		
		if(scaxA<5){scaxA+=0.4;}
		if(scayA<5){scayA+=0.4;}
		
		ObjRender_SetScaleXYZ(aeffu,scax1-scaxA,scay1-scayA,0);
		ObjRender_SetScaleXYZ(aeff,scax1-scaxA,scay1-scayA,0);
		
		if(r1<r&&delete==false){r1+=truncate(r/15);}
		if(g1<g&&delete==false){g1+=truncate(g/15);}
		if(b1<b&&delete==false){b1+=truncate(b/15);}
		if(r1>0&&delete==true){r1-=truncate(r/15);}
		if(g1>0&&delete==true){g1-=truncate(g/15);}
		if(b1>0&&delete==true){b1-=truncate(b/15);}
		if(r1>=r&&g1>=g&&b1>=b){delete=true;}
		ObjRender_SetColor(aeffu,r1*1,g1*1,b1*1);
		ObjRender_SetColor(aeff,r1*1,g1*1,b1*1);
		
		if(delete==true&&r1<=0&&g1<=0&&b1<=0){
			Obj_Delete(aeffu);
			Obj_Delete(aeff);
			break;
		}
		yield;
	}
	Obj_Delete(aeffu);
	Obj_Delete(aeff);
}

task Distortion(){
	let aeff		= ObjPrim_Create(OBJ_SPRITE_2D);
	let EC			= GetAreaCommonData("Option","Effect_Cut",3);
	let alpha		= 0;
	let delete		= false;
	let scax		= rand(10,16);
	let scay		= rand(10,16);
	let r			= 16;
	let g			= 8;
	let b			= 16;
	let r1			= 0;
	let g1			= 0;
	let b1			= 0;
	let EnmX		= ObjMove_GetX(enm);
	let EnmY		= ObjMove_GetY(enm);
	
	ObjRender_SetPosition(aeff,EnmX,EnmY,0);
	ObjPrim_SetTexture(aeff,effect_tex[0]);
	Obj_SetRenderPriorityI(aeff,21);
	ObjRender_SetBlendType(aeff,BLEND_SUBTRACT);
	ObjRender_SetAngleXYZ(aeff,0,0,rand(0,360));
	ObjRender_SetScaleXYZ(aeff,scax,scay,0);
	ObjSprite2D_SetSourceRect(aeff,53,136,89,170);
	ObjSprite2D_SetDestCenter(aeff);
	ObjRender_SetColor(aeff,r,g,b);
	ObjRender_SetAlpha(aeff,alpha);
	
	while(!Obj_IsDeleted(enm) && EC<1){
		let EnmX		= ObjMove_GetX(enm);
		let EnmY		= ObjMove_GetY(enm);
		ObjRender_SetPosition(aeff,EnmX,EnmY,0);
		
		if(scax>0){scax-=0.8;}
		if(scay>0){scay-=0.8;}
		ObjRender_SetScaleXYZ(aeff,scax,scay,0);
		
		if(r1<r&&delete==false){r1+=truncate(r/15);}
		if(g1<g&&delete==false){g1+=truncate(g/15);}
		if(b1<b&&delete==false){b1+=truncate(b/15);}
		if(r1>0&&delete==true){r1-=truncate(r/15);}
		if(g1>0&&delete==true){g1-=truncate(g/15);}
		if(b1>0&&delete==true){b1-=truncate(b/15);}
		if(r1>=r&&g1>=g&&b1>=b){delete=true;}
		ObjRender_SetColor(aeff,r1,g1,b1);
		
		if(delete==true&&r1<=0&&g1<=0&&b1<=0||scax<=0&&scay<=0){
			Obj_Delete(aeff);
			break;
		}
		yield;
	}
	Obj_Delete(aeff);
}