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

	SpellID=54;

	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(80,80,80,80);

	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 [Meteor Shower]");
	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(70);
		loop(2){
			MainShot(x1,y1+50);
			loop(20){
				DelayLine(x1,y1+50,2,255,20,2,ADD,[0,255,0],10);
				x1+=xdiff/40;
				y1+=ydiff/40;
				yield;
			}
		}
		MainShot(x1,y1+50);
		loops=1;
//		Wait(50);
	}
}

task MainTask2{
	let angle=0;
	loop{
//		FadeInShot01(GetX,GetY,1.5,angle    ,WHITE56,60);
//		FadeInShot01(GetX,GetY,1.5,angle+180,WHITE56,60);
//		FadeInShot01(GetX,GetY,1.5,-angle    ,WHITE56,60);
//		FadeInShot01(GetX,GetY,1.5,-angle+180,WHITE56,60);
		angle-=9;
		Wait(5);
	}
}

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 angle2=0;

	let k;
	let i=0;
	let color=0;
	loop(4){
		k=0;
		loop(4){
			CreateShot01(x,y,2+k/4,angle+angle2,WHITE26,0);
			CreateShot01Ghost(x,y,2+k/4,angle+angle2,RED58+colorshot[color],0);
			k++;
		}
		CreateExplodingShot(x,y,3,angle+angle2,RED59,70,color);

		if(absolute(angle2)>5){

			k=0;
			loop(4){
				CreateShot01(x,y,2+k/4,angle-angle2,WHITE26,0);
				CreateShot01Ghost(x,y,2+k/4,angle-angle2,RED58+colorshot[color],0);
				k++;
			}
			CreateExplodingShot(x,y,3,angle-angle2,RED59,70,color);


		}
		angle2-=90/4;
		color++;
		Wait(7);
	}
}
//red, yellow, green, blue

let colorshot=[0,2,3,5];
let colorexpr=[255,255,100,100];
let colorexpg=[100,255,255,100];
let colorexpb=[100,100,100,255];

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

	color%=4;

	let radius=60;

	SetShotColor(colorexpr[color],colorexpg[color],colorexpb[color]);
	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+colorshot[color]);
	ObjShot_SetGraphic(obj2,WHITE01);
	Obj_SetAlpha(obj2,140);

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

	loop(d){
		x+=cos(a)*v;
		y+=sin(a)*v;
		Obj_SetPosition(obj2,x-cos(a)*radius*0.5,y-sin(a)*radius*0.5);
		yield;
	}
	x=Obj_GetX(obj);
	y=Obj_GetY(obj);
	Obj_Delete(obj);
	Obj_Delete(obj2);
	SE("shot2");
	CreateExplosionFlatEX(0,x,y,1,[colorexpr[color],colorexpg[color],colorexpb[color]],0.1,40);
	CreateExplosionEX(3,x,y,1,[colorexpr[color],colorexpg[color],colorexpb[color]],0.2,20);

	let angle=prand(0,360);
	loop(4){
		CreateShot02(x,y,0,angle,0.008,1.2,RED58+colorshot[color],0);
		angle+=360/4;
	}
}

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);
    }
}