script_enemy_main {
    let GCSD=GetCurrentScriptDirectory;
    #include_function ".\..\ShotSheet\shot_replace.dnh"
    #include_function ".\..\functions.txt"
    #include_function ".\alice_functions.txt"

    SpellID=523;

    @Initialize {
	SetCommonData("AliceBGColor1",[0,0,0]);
	SetCommonData("AliceBGColor2",[1,0.5,0]);

	Randomize;
        SetLife(1200);
        SetTimer2(50);
	SetDamageRate(80,40);
	MagicCircle(false);
	SetEnemyMarker(true);
	AliceInit;
	MainTask;
	shotinit;

	StartFunctions(1,0,0);
	SetBG(22);

	SpellCircle2(5,1,4,200);
    }

let dollx=[-50,-50];
let dolly=[0,0];
let mirroractive=false;

let scalex=1;
let scaley=1;

let hitboxframe=0;

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

task MainTask{
	DelayBombTask;
	CutIn2(8,"Ancient Artifact [Mirror of Amaterasu]");

	SetMovePositionNew(GetCenterX,120,60);
	Wait(60);

	let frame=0;
	let angle=180;
	let angle2=-90;

	let cosa;
	let sina;
	let cosa2;
	let sina2;
	let len;
	let wid;
	let cx=GetCenterX;
	let cy=120;
	let cx2=GetCenterX;
	let cy2=120;

	let obj=InitEffect(0,0,imgAlice,4,ALPHA,2);
	ObjEffect_SetXYRect(obj,-20,-40,20,40);
	ObjEffect_SetUVRect(obj,414,150,414+40,150+80);
	HandleMirror(obj);
	loop(60){
		dollx[0]=GetCenterX+cos(90+angle)*100;
		dolly[0]=30+sin(90+angle)*180;
		dollx[1]=GetCenterX+cos(90-angle)*100;
		dolly[1]=30+sin(90-angle)*180;
		frame++;
		angle=cos(frame*(180/60))*90+95;
		yield;
	}
	loop{
		frame=0;
		angle=90+rand(-20,20);
//		while(absolute(angle2-angle)<40){
			angle=rand(-40,40);
//		}
//		angle2=angle;
		angle+=(GetAngleToPlayer-90);
		angle=atan2(cy-GetPlayerY,cx-GetPlayerX)+rand(-30,30)+180;

		cosa=cos(angle);
		sina=sin(angle);
		cosa2=cos(angle+90);
		sina2=sin(angle+90);
		loop(90){
			ObjEffect_SetScale(obj,scalex,upper( (-frame/20)+1,0 )*scaley);
			dollx[0]=dollx[0]+(GetX+cosa*90+cosa2*5-dollx[0])/20;
			dolly[0]=dolly[0]+(GetY+sina*90+sina2*5-dolly[0])/20;
			dollx[1]=dollx[1]+(GetX+cosa*90-cosa2*5-dollx[1])/20;
			dolly[1]=dolly[1]+(GetY+sina*90-sina2*5-dolly[1])/20;
			frame++;
			yield;
		}
		mirroractive=true;
		frame=0;
		loop(240){
			dollx[0]=dollx[0]+(GetX+cosa*90+cosa2*50-dollx[0])/20;
			dolly[0]=dolly[0]+(GetY+sina*90+sina2*50-dolly[0])/20;
			dollx[1]=dollx[1]+(GetX+cosa*90-cosa2*50-dollx[1])/20;
			dolly[1]=dolly[1]+(GetY+sina*90-sina2*50-dolly[1])/20;

			cx=(dollx[0]+dollx[1])/2;
			cy=(dolly[0]+dolly[1])/2;

			Obj_SetPosition(obj,cx,cy);
			len=distance(dollx[0],dolly[0],dollx[1],dolly[1])^0.5;
			ObjEffect_SetScale(obj,(len/100)*scalex,scaley);
			ObjEffect_SetAngle(obj,0,0,atan2(dolly[1]-dolly[0],dollx[1]-dollx[0])+90);

			SetShotColor(255,255,sin(frame*5)*100+155);
			if(frame%2==0){
				//MainShot(GetX,GetY,rand(2,3.3),angle+rand(-20,20),angle,RED21);
				MainShot(GetX,GetY,rand(2,2.5),angle+sin(frame*10)*20,angle,RED21);
				SE("shot1");
			}
			len=rand(0,len*0.4)*RandDir;
			wid=rand(-10,10);
			CreateShot01(GetX,GetY,rand(2,4),angle+rand(-40,-(360-40)),ORANGE21,0);
			SetShotColor(255,255,255);
			CreateShot01(cx+cos(angle)*wid+cos(angle+90)*len,cy+sin(angle)*wid+sin(angle+90)*len, 3,frame*8,WHITE01,0);
			CreateShot01(cx+cos(angle)*wid+cos(angle+90)*len,cy+sin(angle)*wid+sin(angle+90)*len, 3,-frame*8,WHITE01,0);


			if(absolute(AngularDistance(angle,GetAngleToPlayer))>10){
				angle+=GetSign(AngularDistance(angle,GetAngleToPlayer))/2;
				cosa=cos(angle);
				sina=sin(angle);
				cosa2=cos(angle+90);
				sina2=sin(angle+90);
			}

			frame++;
			yield;
		}
		MainShots2;
		frame=0;
		while(distance(cx,cy,cx2,cy2)<150*150&&frame<150){
			cx=rand(GetClipMinX+100,GetClipMaxX-100);
			cy=rand(GetClipMinY+80,GetClipMaxY-160);
			frame++;
		}
		MoveRandom(50,50,GetClipMinX+150,GetClipMinY+70,GetClipMaxX-150,GetClipMinY+150,200,90);
		mirroractive=false;
	}
}

task MainShots2{
	let k=0;
	let angle=63;
	let j=0;
	let color=0;
	let frame=0;
	loop(30){
		SE("shot3");
		k=j;
		loop(5){
			SetShotColor(255,255,cos(color)*80+175);
			CreateShot01(GetX,GetY,1.7-k/10+frame*2,GetAngleToPlayer+angle,RED22,k*6);
			CreateShot01(GetX,GetY,1.7-k/10+frame*2,GetAngleToPlayer-angle,RED22,k*6);
			color+=50;
			k++;
		}
		color-=40*4;
		angle-=2;
		j=1-j;
		SetShotColor(255,255,255);
		frame+=1/30;
		yield;
		yield;
	}

}

task MainShot(x,y,v,a,a2,g){
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj,x,y);
	Obj_SetAngle(obj,a);
	Obj_SetSpeed(obj,v);
	ObjShot_SetGraphic(obj,g);
	let dir=RandDir;
	//GetSign(a-a2)
	let state=0;
	while(!Obj_BeDeleted(obj)){
		if(mirroractive&&state<2){
			if(GetPointInRectangle(Obj_GetX(obj),Obj_GetY(obj), (dollx[0]+dollx[1])/2,(dolly[0]+dolly[1])/2, 80,50, atan2(dolly[1]-dolly[0],dollx[1]-dollx[0]) )){
				dir=GetSign(AngularDistance(a,GetAngleToPlayer));
				a+=4*dir;
				Obj_SetAngle(obj,a);
				state=1;
			}else if(state==1){
				state=2;
			}
		}
		yield;
	}
}

task HandleMirror(obj){
	let frame=0;
	loop{
		if(frame%3==0){
			Obj_SetColor(obj,4,rand(150,255),255,255,255);
			scalex=RandDir;
			scaley=RandDir;
		}
		frame++;
		yield;
	}
}

@MainLoop {
	if(hitboxframe%2==0||!mirroractive){
		SetDamageRate(80,40);
	        SetCollisionA(GetX, GetY, 40);
	}else{
		SetDamageRate(20,20);
	        SetCollisionA((dollx[0]+dollx[1])/2, (dolly[0]+dolly[1])/2, 20);
	}
	SetCollisionB(GetX, GetY, 20);

	hitboxframe++;

	AliceDrawLogic;

	yield;
}

@DrawLoop {
	AliceDrawLoop;
	DrawDoll(dollx[0],dolly[0],hitboxframe,1);
	DrawDoll(dollx[1],dolly[1],hitboxframe,1);
}

@Finalize {
	EndingFunc(6);
	AliceFinalize;
	PointItems(GetX,GetY,7,1);
}

}