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

	SpellID=10;

	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(30);
        SetBG(1);
	SetDamageRate(110,100);
	MagicCircle(false);
	SetEnemyMarker(true);

	EnderInit;
	shotinit;

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

	MainTask;
    }

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

task MainTask{
	SetInvincibility(100);
	Prepare;
	Slash;
	Center;
	DelayBombTask;
//	loop{
//		i3=0;
//		while(i3<30){
//			i=prand(GetClipMinX+100,GetClipMaxX-100);
//			i2=prand(40,120);
//			i3=(distance(GetX,GetY,i,i2)^0.5)/2;
//		}
//		SetMovePositionHermite(i,i2,0,0,0,0,i3);
//		Wait(i3);
//	}

	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);
	EnderFinalize;
	PointItems(GetX,GetY,1,1);
    }

function Prepare{
	WarningLaser(GetClipMinX,163,0,GetClipMaxX-GetClipMinX,4,RED60,255,150,20);

	if(GetPlayerY<y+40){
		Arrow(GetCenterX,y,90,    30,14,10);
		Arrow(GetCenterX-120,y,90,30,14,10);
		Arrow(GetCenterX+120,y,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(160-GetY,GetClipMinX-GetX);
	SetMovePositionHermite(GetClipMinX-20,160,150,angle+180,500,0,150);
	Wait(150);
}
function Slash{
	let time=15;
	SE("slash");
	CreateLaser01(GetClipMinX,163,((GetClipMaxX-GetClipMinX)/time),0,140,120,WHITE56,0);
	CreateLaser01(GetClipMinX,163,((GetClipMaxX-GetClipMinX)/time),0,180,90,YELLOW56,0);
	CreateLaser01(GetClipMinX,163,((GetClipMaxX-GetClipMinX)/time),0,200,50,RED56,0);
	SetMovePosition02(GetClipMaxX-20,160,time);
//	CreateLaser(160,time,RED54,RED56,RED01,0);
//	CreateLaser(160,time,ORANGE54,ORANGE56,ORANGE01,14);

	CreateLaser(160,time,RED54,RED23,RED05,0);
	CreateLaser(160,time,ORANGE54,ORANGE23,ORANGE05,6);
	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(y,time,g1,g2,g3,offset){
    let obj = Obj_Create(OBJ_LASER);
    let stimer=0;
    let shots=0;
    let x=GetClipMinX;

    // initial settings
    Obj_SetPosition   (obj, GetClipMinX,y);
    Obj_SetSpeed      (obj, 0);
    ObjShot_SetGraphic(obj, g1);
    ObjShot_SetDelay  (obj, 0);
    Obj_SetAngle(obj, 0);
    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;
	Obj_SetX(obj,x);
	FadeShot(GetX,GetY,7,prand(0,360),0);
	FadeShot(GetX,GetY,5,prand(0,360),0);
	yield;
    }
    while(! Obj_BeDeleted(obj)) {
	RandomWidth(obj);
	if(stimer%12==offset){
		SE("shot2");
		if(GetPlayerY>y){
			MainShot(prand(GetClipMinX,GetClipMaxX),y,2.5,90+(prand(35,80)*RandDir),g2,g3,10,1);
		}else{
			MainShot(prand(GetClipMinX,GetClipMaxX),y,2.5,-90+(prand(35,80)*RandDir),g2,g3,10,-1);
		}
		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){
    let obj=[0,0,0,0,0];
    let i=0;
    let stimer=0;
    let reflect=0;
    let rotangle=0;
    let rotdir=RandDir;
    let radius=8;
    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);
	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);

    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);
		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);
		i++;
	}
	yv+=0.02*dir;

	rotangle+=rotdir*7;
	yield;
    }
}

}