
script_enemy_main
{    
   let imgEnemy="script\DanMUku\Stage5\Image\magikoopa.png";
   let count=50;
   let a = 0;
   let x = GetX;
   let attaque = false;
   let angle = 0;
    let tir ="script\DanMUku\Stage1\Son\tir.wav";
    let munch ="script\DanMUku\Stage1\Son\muncher.wav";
   @Initialize
   {            
		LoadUserShotData("script\DanMUku\MUtir\MUtir.txt");
            LoadGraphic(imgEnemy);	
	          LoadSE(tir);
	  LoadSE(munch);
	SetTimer(100);
            SetLife(8000);    
   }
   
function ShootShape(spawnX, spawnY, v, angle, angle2, num, bside, graphic, delay, size) {
	ascent(i in 0..num) {
		let sx = spawnX+cos(angle+i*360/num)*size; let sy = spawnY+sin(angle+i*360/num)*size; let sxN = spawnX+cos(angle+(i+1)*360/num)*size; let syN = spawnY+sin(angle+(i+1)*360/num)*size;
		Tir(spawnX, spawnY, v*(((sx-spawnX)^2+(sy-spawnY)^2)^0.5), v, atan2(sy-spawnY,sx-spawnX), angle2, graphic, delay);
		ascent(j in 0..bside) {
			let toAngle = atan2(syN-sy,sxN-sx); let toDist = (((sxN-sx)^2+(syN-sy)^2)^0.5); let sx2 = sx+toDist/bside*j*cos(toAngle); let sy2 = sy+toDist/bside*j*sin(toAngle);
			Tir(spawnX, spawnY, v*(((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5), v, atan2(sy2-spawnY,sx2-spawnX), angle2, graphic, delay);
		}
	}
}

function Circle(X, Y, v, angle, num, size, graphic, delay) {
	let a=0;
	loop(num) {
		CreateShot01(X+cos(a)*size, Y+sin(a)*size, v, angle, graphic, delay);
		a+=360/num;
	}
}

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


   @MainLoop
   {
          SetCollisionA(GetX, GetY, 32);
        SetCollisionB(GetX, GetY, 16);
	if (count==100) {
		SetSpeed(0);
	}
	if (count>150 && count%480==0) {
		ShootShape(GetX, GetY, 2, 45, 90, 4, 16, 147, 100, 1);
		PlaySE(munch);
	}
	if (count>150 && count%480==240) {
		ShootShape(GetX, GetY, 2, 270, 90, 3, 16, 149, 100, 1);
		PlaySE(munch);
	}
	if (count>240 && count%150==0){
		angle = GetAngleToPlayer-150;
		attaque=true;
	}
	if (attaque && count%5==0) {
		Circle(GetX, GetY, 4, angle, 12, 25, 148, 0);
		angle+=50;
		PlaySE(tir);
	}
	if (count%150==100) {
		attaque=false;
	}

	if (count%5==0) {
		a+=33;
		if (a==99) {
			a=0;
		}
	}
      count++;
	yield;
   }
   
   @Finalize
   {
	if (GetMissCountInThisSpell==0) {
		SetCommonData("B", true);
	}
	loop(10)
	{
		CreateItem(ITEM_SCORE, rand(GetX-20, GetX+20), rand(GetY-20, GetY+20));  
	}
      DeleteGraphic(imgEnemy);
   }
   
   @DrawLoop
   {
	if(x>GetX){
		SetGraphicAngle(0,0,0);
	}
	if(x<GetX){
		SetGraphicAngle(180,0,0);
	}
      x=GetX;
      SetColor(255,255,255);
            SetRenderState(ALPHA);
            SetTexture(imgEnemy);
		if (attaque) {
            SetGraphicRect(98,0,148,56);
		}
		else {
            SetGraphicRect(a,7,a+28,49);
		}
            DrawGraphic(GetX,GetY);
		
   }

task Tir(x, y, v, vv, angle, angle2, graphic, delay){

            let obj=Obj_Create(OBJ_SHOT);
            Obj_SetPosition(obj, x, y);
            Obj_SetAngle(obj, angle);
            Obj_SetSpeed(obj, v);
            ObjShot_SetGraphic(obj, graphic);
	    ObjShot_SetBombResist(obj, false);
	    Wait(delay);
	    Obj_SetAngle(obj, angle2);
	    Obj_SetSpeed(obj, vv);
	    Wait(rand(delay/8, delay/2));
	    Obj_SetSpeed(obj, rand(1, vv));
	    Obj_SetAngle(obj, rand (70, 110));
	}



}


