script_enemy_main {
	#include_function ".\..\ShotSheet\shot_replace.dnh"
	#include_function ".\..\functions.txt"
	#include_function ".\e_functions.txt"

	SpellID=11;

	let frame = 0;
	let shottimer=1;
	let x=0;
	let y=0;
	let i=0;
	let i2=0;
	let i3=0;
	let i4=0;
	let angle=0;
	let attacks=1;
	let xshift=0;
	let yshift=0;
	let side=130;
	let vside=90;

	let BGscrollx=0;
	let BGscrolly=0;
	let BGscrollx2=0;
	let BGscrolly2=0;
	let BGscrollx3=0;
	let BGscrolly3=0;
	let bgrot=0;

    @Initialize {
	SetCommonData("BGInit",0);
	StartFunctions(1,0,0);
	Randomize;
        SetLife(3000);
        SetTimer2(35);
	SetBG(1);
	SetDamageRate(110,100);
	MagicCircle(false);
	SetEnemyMarker(true);

	EnderInit;
	shotinit;

	CutIn2(0,"Shuriken Gear [Legend Storm]");
	SpellCircle2(0,1,4,210);
	SetShotAutoDeleteClip(15,15,15,15);

	MainTask;
    }

function Wait(frames){loop(frames){yield;}}

task MainTask{
	SetInvincibility(200);
	Prepare(GetClipMaxX,420,-1,150,0);
	Slash(GetClipMaxX,420,-1);
	Prepare(GetClipMinX,145,1,700,180);
	Slash(GetClipMinX,145,1);
	Center;
	DelayBombTask;
	loop{
		SetX(GetX+(GetPlayerX-GetX)/120);
		yield;
	}
}

    @MainLoop {
	EnderDrawLogic;
        SetCollisionA(GetX, GetY, 35);
	SetCollisionB(GetX, GetY, 25);

	yield;
}


	let EnderFrame=0;

	let aurarot=0;
	let aurascale=0;
	let scaler=prand(1.7,1.9);

    @DrawLoop {
	EnderDrawLoop;
}

    @Finalize {
	EndingFunc(1);
	SetShotAutoDeleteClip(64,64,64,64);
	EnderFinalize;
	PointItems(GetX,GetY,1,1);
    }

function Prepare(x,y,dir,distance,angle2){
	WarningLaser(GetClipMinX,y,0,GetClipMaxX-GetClipMinX,4,RED60,255,150,20);

	let angle=atan2(GetCenterY-y,0)/90;

	if((GetPlayerY>y-40&&angle==-1)||(GetPlayerY<y+40&&angle==1)){
		Arrow(GetCenterX,y,angle*90,    30,14,10);
		Arrow(GetCenterX-120,y,angle*90,30,14,10);
		Arrow(GetCenterX+120,y,angle*90,30,14,10);
	}

	NewChargeShrink(150,8,0.5,255,200,100);
	NewChargeShrink(150,8,0.5,255,100,100);
	NewChargeShrink(150,8,0.5,255,255,255);
	let angle=atan2(y-GetY,x-GetX);
	SetMovePositionHermite(x,y,distance,angle2,700,90-90*dir,150);
	Wait(150);
}
function Slash(x,y,dir){
	let time=15;
	SE("slash");
	CreateLaser01(x,y,((GetClipMaxX-GetClipMinX)/time),90-90*dir,140,120,WHITE56,0);
	CreateLaser01(x,y,((GetClipMaxX-GetClipMinX)/time),90-90*dir,180,90,YELLOW56,0);
	CreateLaser01(x,y,((GetClipMaxX-GetClipMinX)/time),90-90*dir,200,50,RED56,0);
	SetMovePosition02(GetCenterX-(x-GetCenterX),y,time);
//	CreateLaser(160,time,RED54,RED56,RED01,0);
//	CreateLaser(160,time,ORANGE54,ORANGE56,ORANGE01,7);

	CreateLaser(x,y,dir,time,ORANGE54,ORANGE23,WHITE05,0);
	CreateLaser(x,y,dir,time,WHITE54,WHITE23,WHITE05,12);
	let size=0;
	loop(15){
		yield;
		CreateExplosionEX(0,GetX,GetY,0.1,[255,70,70],0.5,20+(size%2)*20);
		size++;
	}
}
function Center{
	SetMovePositionHermite(GetCenterX,90, 1500,0,100,0,120);
	Wait(120);
}
task CreateLaser(x,y,dir,time,g1,g2,g3,offset){
    let obj = Obj_Create(OBJ_LASER);
    let stimer=0;
    let shots=0;

    // initial settings
    Obj_SetPosition   (obj, x,y);
    Obj_SetSpeed      (obj, 0);
    ObjShot_SetGraphic(obj, g1);
    ObjShot_SetDelay  (obj, 0);
    Obj_SetAngle(obj, 90-90*dir);
    ObjShot_SetBombResist(obj,true);
    ObjLaser_SetLength(obj,0);
    ObjLaser_SetSource(obj,false);
    Obj_SetAutoDelete(obj,false);

    let lengthen=((GetClipMaxX-GetClipMinX)+140)/time;
    let length=0;
    let xchange=70/time;

    loop(time){
	RandomWidth(obj);
	length+=lengthen;
	ObjLaser_SetLength(obj,length);
	x-=xchange*dir;
	Obj_SetX(obj,x);
	FadeShot(GetX,GetY,7,prand(0,360),0);
	FadeShot(GetX,GetY,5,prand(0,360),0);
	yield;
    }
    let randangle;
    let randx;
    let randdir;
    while(! Obj_BeDeleted(obj)) {
	RandomWidth(obj);
	if(stimer%24==offset){
		SE("shot2");
		randangle=prand(35,80)*RandDir;
		randx=prand(GetClipMinX,GetClipMaxX);
		randdir=(shots%2-0.5)*2;
		if(GetPlayerY>y){
			MainShot(randx,y,1.5,90+(randangle),g2,g3,10,1,randdir,0,8);
		}else{
			MainShot(randx,y,1.5,-90+(randangle),g2,g3,10,-1,randdir,0,8);
		}
//		randx=prand(GetClipMinX,GetClipMaxX);
//		MainShot(randx,y,2.5,-90+(randangle),g2,g3,10,0,randdir,0,8);
//		MainShot(randx,y,2.5,-90+(randangle),g2,g3,10,0,randdir,-30*randdir,16);

		shots++;
	}

	stimer++;
        yield;
    }
    Obj_Delete(obj);

}
function RandomWidth(obj){
	ObjLaser_SetWidth(obj,prand(5,20));

}


let cshot2=[WHITE50,RED50,ORANGE50,YELLOW50];

task FadeShot(x,y,v,a,d){
    let obj = Obj_Create(OBJ_SHOT);

    v=v*(absolute(cos(a)));

    // initial settings
    Obj_SetPosition   (obj, x,y);
    Obj_SetSpeed      (obj, v);
    ObjShot_SetGraphic(obj, cshot2[prand_int(0,3)]);
    ObjShot_SetDelay  (obj, d);
    Obj_SetAngle(obj, a);
    ObjShot_SetBombResist(obj,true);
    Obj_SetAutoDelete(obj,false);
    ObjShot_FadeDelete(obj);
}
task MainShot(x,y,v,a,g,g2,d,dir,rotdir,rotangle,radius){
    let offsetangle=rotangle;
    let obj=[0,0,0,0,0];
    let i=0;
    let stimer=0;
    let reflect=0;
    let yv=0;

    d*=2;
    loop(4){
	DelayNew(x,y,rand(0,360),30,16,g,d,40);
    }
    Wait(d);
    loop(5){
	obj[i] = Obj_Create(OBJ_SHOT);
	Obj_SetPosition   (obj[i], x,y);
	Obj_SetSpeed      (obj[i], 0);
	ObjShot_SetDelay  (obj[i], 0);
	ObjShot_SetBombResist(obj[i], true);
	Obj_SetAutoDelete(obj[i], false);
	if(i<4){ObjShot_SetGraphic(obj[i], g);}
	i++;
    }
//    ObjShot_SetGraphic(obj[4], g2);
    Obj_SetCollisionToPlayer(obj[4],false);
    Obj_SetAngle(obj[4], a);
    ObjShot_SetBombResist(obj[4], false);
    Obj_SetAutoDelete(obj[4], true);

    while(! Obj_BeDeleted(obj[4])) {
	d--;
	if(d<0){Obj_SetPosition(obj[4],Obj_GetX(obj[4])+cos(a)*v,Obj_GetY(obj[4])+sin(a)*v+yv)}
	if((Obj_GetX(obj[4])<GetClipMinX-7||Obj_GetX(obj[4])>GetClipMaxX+7)&&reflect==0){
		a=180-a;
		Obj_SetAngle(obj[4],a);
		reflect=1;
		rotdir=-rotdir;
	}
	yv+=0.02*dir;
	i=0;
	loop(4){
		let a2=i*90+rotangle;
		Obj_SetPosition(obj[i],Obj_GetX(obj[4])+cos(a2)*radius,Obj_GetY(obj[4])+sin(a2)*radius);
		Obj_SetAngle(obj[i], a2+offsetangle);
		i++;
	}
	rotangle+=rotdir*7;
	stimer++;
	x=Obj_GetX(obj[4]);
	y=Obj_GetY(obj[4]);
        yield;
    }
    i=0;
    loop(4){
	ObjShot_FadeDelete(obj[i]);
	i++;
    }
    loop(60){
	x+=cos(a)*v;
	y+=sin(a)*v+yv;
	i=0;
	loop(4){
		let a2=i*90+rotangle;
		Obj_SetPosition(obj[i],x+cos(a2)*radius,y+sin(a2)*radius);
		Obj_SetAngle(obj[i], a2+offsetangle);
		i++;
	}
	yv+=0.02*dir;

	rotangle+=rotdir*7;
	yield;
    }
}


}