script_enemy_main {
	#include_function ".\..\ShotSheet\shot_replace.dnh"
	#include_function ".\..\functions.txt"
	#include_function ".\..\s2\em_functions.txt"

	SpellID=28;

	let i=0;
	let frame=0;

    @Initialize {
	StartFunctions(1,0,0);
	Randomize;
        SetLife(2500);
        SetTimer2(35);
	SetDamageRate(100,100);
	SetBG(2);
	MagicCircle(false);
	SetEnemyMarker(true);
	EmpeghInit;
	EmpeghPushbackEffect;

	shotinit;

	CutIn2(1,"Noise [Echo Bomb]");
	SpellCircle2(3,1,4,250);

	MainTask;
    }


function Wait(frames){loop(frames){yield;}}
let colors=[RED56,ORANGE56,YELLOW56,GREEN56,AQUA56,BLUE56,PURPLE56];

task MainTask{
	SetMovePosition03(GetCenterX,200,10,5);
	DelayBombTask;
	Wait(60);
	let a=prand(0,360);
	let color=0;
	let i=0;
	loop{
		SE("shot3");
		SE("laser4");
		loop(30){
			Sound(GetX,GetY,2.8,a,30,40,6,RED56+color%7,4,0);
			color++;
			a+=360/30;
		}
		a+=360/60+prand(-3,3);
		Wait(upper(0,lower(GetLife/2500,GetTimer2/35)-0.3)*100+60);
	}
}
task Sound(x,y,v,a,l,w,s,g,d,d2){
	let widthadd=(w/s);
	loop(s){
		MainShot(x,y,v,a,l,w,g,d2);
		w-=widthadd;
		Wait(d);
	}
}

    let speed=1;

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

	SetPlayerX(GetPlayerX+cos(GetAngleToPlayer)*speed);
	SetPlayerY(GetPlayerY+sin(GetAngleToPlayer)*speed);


	EmpeghDrawLogic;
	yield;
}
    @DrawLoop {
	EmpeghDrawLoop;
}

    @Finalize {
	EmpeghFinalize;
	EndingFunc(2);
	PointItems(GetX,GetY,2,1);
    }


task MainShot(x,y,v,a,l,w,g,d){
    Wait(d);

    let obj = Obj_Create(OBJ_LASER);
    let frame=0;
    let timer=0;
    let bounces=0;

    // initial settings
    Obj_SetPosition   (obj, x,y);
    Obj_SetSpeed      (obj, v);
    ObjShot_SetGraphic(obj, g);
    ObjShot_SetDelay  (obj, 10);
    Obj_SetAngle(obj, a);
    ObjLaser_SetSource(obj,false);
    ObjLaser_SetWidth(obj,w);
    ObjLaser_SetLength(obj,l);
    ObjShot_SetBombResist(obj,false);

    let xv=cos(a)*v;
    let yv=sin(a)*v;
    while(! Obj_BeDeleted(obj)) {
	x+=xv;
	y+=yv;

	if((x<GetClipMinX||x>GetClipMaxX)&&timer==0&&bounces<1){
		a=180-a;
		Obj_SetAngle(obj,a);
		xv=cos(a)*v;
		yv=sin(a)*v;
		bounces++;
	}
//	if((y<GetClipMinY)&&timer==0&&bounces<1){
//		a=360-a;
//		Obj_SetAngle(obj,a);
//		bounces++;
//	}
	if(timer>0){timer--;}
	frame++;

	Obj_SetPosition(obj,x,y);
        yield;
    }

}
}