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

    SpellID=516;

    @Initialize {
        SetLife(2500);
        SetTimer2(40);
	SetDamageRate(80,40);
	MagicCircle(false);
	SetEnemyMarker(true);
	PatchyInit;
	MainTask;
	shotinit;
	SetShotAutoDeleteClip(70,20,70,5);
    }


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

	let winddir=90;
	let coswinddir=cos(90);
	let sinwinddir=sin(90);

task MainTask{
	DelayBombTask;
	yield;
	PatchyElement(2,GetCenterX,80,20,9, 170,180);
	SetMovePositionNew(GetCenterX,150,60);
	Wait(50);
	let frame=0;
	let angle=rand(0,360);
	loop{
		loop(1+(frame%3==0)){
			LeafShot(GetX,GetY,rand(1.5,2.5),rand(0,360));
			CreateShot01(GetX,GetY,rand(1.5,2.5),rand(0,-180),ORANGE11+rand_int(0,2),0);
			angle+=48;
		}
		frame++;
		winddir=cos(frame/3)*50+90;
		coswinddir=cos(winddir);
		sinwinddir=sin(winddir);
		if(frame%4==0){
			CreateExplosionEX(0,GetX,GetY,1,[rand(50,128),128,50],rand(0.05,0.2),rand(20,60));
		}
		Wait(1);
	}
}

task LeafShot(x,y,v,angle){
	let xv=cos(angle)*v;
	let yv=sin(angle)*v;

	let obj=Obj_Create(OBJ_SHOT);
	ObjShot_SetGraphic(obj,ORANGE11+rand_int(0,2));

	while(!Obj_BeDeleted(obj)){
		Obj_SetPosition(obj,x,y);
		x+=xv;
		y+=yv;
		xv+=v*coswinddir/90;
		yv+=v*sinwinddir/90;
		angle=atan2(yv,xv);
		Obj_SetAngle(obj,angle);
		if(xv*xv+yv*yv>v*v){
			xv=cos(angle)*v;
			yv=sin(angle)*v;
		}

		yield;
	}

}

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

	PatchyDrawLogic;

	yield;
}

@DrawLoop {
	PatchyDrawLoop;
}

@Finalize {
	SetShotAutoDeleteClip(64,64,64,64);
	EndingFunc(6);
	PatchyFinalize;
	PointItems(GetX,GetY,7,0);
}

}