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

	SpellID=55;

	let i=0;
	let frame=0;

    @Initialize {
	StartFunctions(1,0,0);
	Randomize;
        SetLife(2500);
        SetTimer2(45);
	SetDamageRate(100,100);
	MagicCircle(false);
	SetEnemyMarker(false);
	YumemiInit;

	shotinit;

	SetShotAutoDeleteClip(20,10,20,20);

	MainTask;
    }


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

task MainTask{
	MoveIntoPosition;
	WaitEvent(300);
	let eventstepped=0;
	while(EventActive){
		if(EventStep==2){
			yuanim=1;
			eventstepped=1;
			SetBG(4);
			SetMovePositionNew(GetCenterX,GetCenterY,60);
		}
		yield;
	}
	if(eventstepped==0){
		SetBG(4);
	}
	yuanim=0;
	DelayBombTask;
	SetMovePosition03(GetCenterX,130,10,5);
	SetEnemyMarker(true);
	SpellCircle2(4,1,4,250);
	CutIn2(4,"Astral Science [Comet of Ages]");
	Wait(120);
	let x1=0;
	let y1=0;
	let x2=0;
	let y2=0;
	let xdiff=0;
	let ydiff=0;
	let frame=0;
	let loops=0;
	MainTask2;
	loop{
		x1=GetCenterX+prand(-200,200);
		y1=prand(20,220);
		x2=x1;
		y2=y1;
		while(distance(x1,y1,x2,y2)^0.5<200){
			x2=GetCenterX+prand(-200,200);
			y2=prand(20,220);
		}
		if(loops==1){
			YumemiMove(50);
			MoveRandom(70,20, GetClipMinX+50,130,GetClipMaxX-50,180, 150, 50);
		}
		xdiff=x2-x1;
		ydiff=y2-y1;
		if(loops==1){
			Wait(100);
		}
		YumemiAttack(60);
		loop(1){
			MainShot(x1,y1);
			loop(30){
				DelayLine(x1,y1,2,255,20,2,ADD,[0,255,0],10);
				x1+=xdiff/30;
				y1+=ydiff/30;
				yield;
			}
		}
		MainShot(x1,y1);
		loops=1;
//		Wait(50);
	}
}

task MainTask2{
	let angle=0;
	let k=0;
	let frame=0;
	let cosframe;
	loop{
		cosframe=cos(frame*20)*0.5;
		k=0;
		loop(8){
			FadeInShot01(GetX,GetY,2+cosframe,angle+k,RED21,60);
			FadeInShot01(GetX,GetY,2-cosframe,-(angle+k),RED21,60);
			k+=45;
		}
		angle-=8;
		frame++;
		Wait(12);
	}
}

task DelayLine(x,y,width,alpha,time,layer,renderstate,color,delay){
	Wait(delay);
	Line(GetYuHandX,GetYuHandY,x,y,width,alpha,time,layer,renderstate,color);
}
task MainShot(x,y){
	Cross(x,y,60,0);
	Wait(20);

	let angle=atan2(GetPlayerY-y,GetPlayerX-x);

	let k=0;
	loop(4){
		CreateExplodingShot(x,y,2+k/2,angle,AQUA58-k,AQUA58-k,30+k*8,4-k);
//		CreateShot01(x,y,2+k/4,angle,WHITE26,0);
//		CreateShot01Ghost(x,y,2+k/4,angle,GREEN58,0);
		k++;
	}
	CreateExplodingShot(x,y,2+k/2,angle,RED59,RED58,75,0);
}

task CreateExplodingShot(x,y,v,a,g,g2,d,expcolor){

	let radius=40;
	if(expcolor==0){
		radius=80;
	}
	let color=RainbowArray(expcolor*(360/7),120,50);

	SetShotColor(color[0],color[1],color[2]);
	let obj2=Obj_Create(OBJ_LASER);
	SetShotColor(255,255,255);
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj,x,y);
	Obj_SetSpeed(obj,v);
	Obj_SetAngle(obj,a);
	Obj_SetAngle(obj2,a);
	ObjShot_SetGraphic(obj,g);
	ObjShot_SetGraphic(obj2,WHITE01);
	Obj_SetAlpha(obj2,140);

	ObjLaser_SetWidth(obj2,radius);
	ObjLaser_SetLength(obj2,radius);
	ObjLaser_SetSource(obj2,false);
	Obj_SetCollisionToPlayer(obj2,false);

	let frame=0;
	let cosa=cos(a);
	let sina=sin(a);
	let colordec=(255-180)/d;
	loop(d){
		x+=cosa*v;
		y+=sina*v;
		if(expcolor==0&&frame%Detail(3,4,8)==0){
			CreateExplosionEX(3,x,y,1,[180+colordec*frame,180-colordec*frame,180-colordec*frame],0.05,40);
		}
		Obj_SetPosition(obj2,x-cosa*radius*0.5,y-sina*radius*0.5);
		frame++;
		yield;
	}
	x=Obj_GetX(obj);
	y=Obj_GetY(obj);
	Obj_Delete(obj);
	Obj_Delete(obj2);
	SE("shot2");
	CreateExplosionEX(0,x,y,1,color,0.2,40);
//	WarningLaser(x,y,a,260,5,GREEN01,150,80,80);
//	CreateExplosionEX(3,x,y,1,[255,150,100],0.4,20);

	let num=4;
	if(expcolor==0){
		num=10;
	}
	let angle=prand(0,360);
	let i=0;
	loop(num){
		CreateShot02(x,y,0,angle,0.008,0.5+i/2,g2,0);
//		WarningLaser(x,y,angle,150,3,RED01,150,30,60);
		angle+=360/num;
		i=1-i;
	}
}


function MoveIntoPosition{
	let echonum=0;
	let frame=0;
	YumemiMove(60);
	SetX(GetCenterX+300);
	SetY(GetCenterY);
	SetMovePositionNew(GetCenterX,GetCenterY,60);
	Wait(60);
}
    @MainLoop {
        SetCollisionA(GetX, GetY, 50);
	SetCollisionB(GetX, GetY, 20);

	YumemiDrawLogic;
	yield;
}
    @DrawLoop {
	YumemiDrawLoop;
}


    @Finalize {
	YumemiFinalize;
	PointItems(GetX,GetY,3,1);
	EndingFunc(3);
    }
}