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

    SpellID=517;

    @Initialize {
        SetLife(2700);
        SetTimer2(40);
	SetDamageRate(80,40);
	MagicCircle(false);
	SetEnemyMarker(true);
	PatchyInit;
	MainTask;
	shotinit;

	CutIn2(7,"Five Elements [Chaos Arcanum]");
	SpellCircle2(6,1,4,200);
	SetBG(21);
    }

let familiarx=GetX;
let familiary=GetY;
let frame=0;

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

task MainTask{
	ElementTask;
	DelayBombTask;
	SetMovePositionNew(GetCenterX,100,90);
	Wait(50);

	let i=0;
	let color=0;
	let dir=1;
	let angle=45;
	MainTask2;
	MainTask3;
	MoveTask;
	loop{
		i=rand(0,360);
		angle=90+rand(-90,90)+(GetCenterX-familiarx)/30;
		CreateExplosionEX(3,familiarx,familiary,1,[255,50,50],rand(0.2,0.3),rand(60,120));
		CreateExplosionEX(3,familiarx,familiary,1,[50,255,50],rand(0.2,0.3),rand(60,120));
		CreateExplosionEX(3,familiarx,familiary,1,[50,50,255],rand(0.2,0.3),rand(60,120));
		SE("laser2");
		SE("laser3");
		SE("shot3");
		PatchySwirl(0,familiarx,familiary,angle, 0+i);
		PatchySwirl(1,familiarx,familiary,angle, 120+i);
		PatchySwirl(2,familiarx,familiary,angle, 240+i);

//		PatchySwirl(3,GetX,GetY,angle, 180);

		Wait(120);
	}
}
task MainTask2{	//moon shots
	let angle=2.5;
	let k=0;
	let x;
	let y;
	Wait(140);
	NewChargeShrink(120,6,1,255,255,255);
	NewCharge(120,6,0.5,255,255,255);
	Wait(120);
	let obj=[0,0];
	loop(2){
		obj[k]=PatchyElement(k+5,-100,-100,20,15, 255,1);
		k++;
	}
	loop{
		x=GetX;
		y=GetY;
		loop(5){
			SE("shot1");
			k=0;
			loop(6){
				FadeInShot01(x,y,3,90+angle+60*k,WHITE54,60);
				FadeInShot01(x,y,3,90-angle+60*k,WHITE54,60);
				k++;
			}
			loop(6){
				Obj_SetPosition(obj[0],GetX-50,GetY);
				Obj_SetPosition(obj[1],GetX+50,GetY);
				yield;
			}
		}
		loop(3){
			Obj_SetPosition(obj[0],GetX-50,GetY);
			Obj_SetPosition(obj[1],GetX+50,GetY);
			yield;
		}
		angle+=10;
	}
}

task MainTask3{	//sun shots
	let angle;
	let k=0;
	Wait(140+300);
	NewChargeShrink(120,8,1,255,200,100);
	NewCharge(120,8,0.5,255,200,100);
	Wait(120);
	loop{
		SE("shot2");
		angle=rand(0,360);
		k=0;
		loop(30){
			SunShot(GetX,GetY,2+(k%2)/2,angle,YELLOW02,RED01,45,0);
			angle+=360/30;
			k++;
		}
		Wait(70);
	}
}
task MoveTask{
	Wait(40);
	loop{
		MoveRandom(50,30,GetClipMinX+50,50,GetClipMaxX-50,120,100,120);
		Wait(150);
	}
}

@MainLoop {
        SetCollisionA(GetX, GetY, 40);
	SetCollisionB(GetX, GetY, 20);

	familiarx=GetCenterX+sin(frame*1.3)*100;
	familiary=100+sin(frame*1.3*2)*50;
	frame++;

	PatchyDrawLogic;
	yield;
}
task ElementTask{
	yield;
	let obj=[0,0,0];
	let k=0;
	loop(3){
		obj[k]=PatchyElement(k,-100,-100,20,15, 210,1);
		Obj_SetColor(obj[k],4,210,255,255,255);
		k++;
	}
	let frame=0;
	let angle=0;
	loop{
		k=0;
		loop(3){
			Obj_SetPosition(obj[k],familiarx+cos(angle+k*120)*20,familiary+sin(angle+k*120)*20);
			k++;
		}
		angle+=3;
		yield;
	}
}

task PatchySwirl(type, x,y,angle, phase){

	let shotfreq=90;

	let frame=0;
	let offset=0;
	let lastx=0;
	let lasty=0;
	let newx=x;
	let newy=y;
	let graphicangle;
	let speed=0.2;

	let xs=[];
	let ys=[];
	loop(15){
		xs=xs~[-100];
		ys=ys~[-100];
	}
	let k=0;
	let grazedtime=0;

	let vels=[2,2.2,2.4,2.6,2.8,2.6,2.4,2.2];
	let colors=[[1,0,0],[0,1,1],[1,1,0]];
	let color=[0,0,0];

	while(OnScreen(x,y,80)){

		speed=lower(speed+0.01,1);
		offset=sin(frame*5+phase)*40*speed;

		if(frame<100){
			angle+=center(-1,AngularDistance(angle,atan2(GetPlayerY-y,GetPlayerX-x))*speed,1);
		}

		lastx=newx;
		lasty=newy;
		x+=cos(angle)*5*speed;
		y+=sin(angle)*5*speed;
		newx=x+cos(angle+90)*offset;
		newy=y+sin(angle+90)*offset;

		graphicangle=atan2(newy-lasty,newx-lastx)+180;

		if(frame>0){
			if(type==0){
				PatchyLightningColor(newx,newy,rand(0,2),rand(-1,1)*0.3,graphicangle,rand(50,70),rand(30,70), 2,0,60, 3, [120+(frame%2)*200,255,120+(frame%2)*200]);
			}else if(type==1){
				PatchyFire(newx,newy,rand(0,2),rand(-1,1)*0.1,graphicangle,rand(20,90),rand(20,30), 2,0,60, SP01);
			}else if(type==2){
				PatchyFire(newx,newy,rand(0,2),rand(-1,1)*0.5,graphicangle,rand(20,50),rand(20,40), 2,0,60, SP04);
			}else if(type==3){
				PatchyFire(newx,newy,rand(0,2),0,graphicangle,rand(20,50),rand(20,40), 2,0,60, GREEN50);
			}
		}
		xs[(frame)%15]=newx;
		ys[(frame)%15]=newy;

		k=0;
		loop(10){
			if(grazedtime<=0){
				if(CircleGraze(xs[k],ys[k],20)){
					grazedtime=30;
				}
			}
			if(CircleCollision(xs[k],ys[k],13)){ShootDownPlayer;}
			k++;
		}
		if(grazedtime>0){
			grazedtime--;
		}


		frame++;
		Wait(1);
	}

}

@DrawLoop {
	PatchyDrawLoop;
	SetTexture(imgWhite);
	SetGraphicRect(0,0,10,10);
	SetGraphicAngle(0,0,0);
	DrawGraphic(familiarx,familiary);
}

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

}