

script_enemy_main
{    

    let count=-100;
    let diff = 4;
    let bg = "script\DanMUku\Image\bonusroom.png";
    let bonus = "script\DanMUku\Image\bonus.png";
    let tir ="script\DanMUku\Stage1\Son\tir.wav";
    let munch ="script\DanMUku\Stage1\Son\muncher.wav";
    let timer = 0;
    let v = 1;
    let x = GetCenterX;
    let change=1;
    let graphic=100;
    let phase = false;


   @Initialize
   {
      LoadUserShotData("script\DanMUku\MUtir\MUtir.txt");
      LoadGraphic(bg);
      LoadGraphic(bonus);
      LoadMusic("script\DanMUku\Musique\bonus.mp3");
      LoadSE(tir);
      LoadSE(munch);
	LastSpell;
	//PlayMusic("script\DanMUku\Musique\bonus.mp3");
            SetLife(8500);
            SetTimer(120);
            SetInvincibility(100);
            SetEnemyMarker(false);
            SetScore(0);
   }
   
 function Wait(let frames){
   loop(frames){yield;}
 }

   @MainLoop
   {

	if(count%diff==0){
		let larg = GetClipMaxX - GetClipMinX;

		Bullet((rand(30, larg-30)+x)%larg+GetClipMinX, 10, v, 90, graphic);
		if (phase) {
			CreateShot01((rand(30, larg-30)+x)%larg+GetClipMinX, 10, 1, 90, graphic-18, 0);
		}	
		graphic+=1;
		if (graphic==109){ graphic=100; }
		x+=change;
		if (x==GetClipMaxX-20) { change=-1; }
		if (x==GetClipMinX+20) { change=1; }
	}

	if (count==2400||count==4800){
		v+=0.5;
	}

	if (count==3600||count==6000){
		diff-=1;
	}

	if (count==1200){
		phase = true;
	}


timer=GetTimer;

count++;
yield;
   }
   @Finalize
   {
	DeleteMusic("script\DanMUku\Musique\bonus.mp3");
	if (GetArgument==5) {
		if(timer==0){
			CreateItem(ITEM_BOMB, rand(GetX-20, GetX+20), GetClipMinY); 
		}
		if(timer<20){
			CreateItem(ITEM_1UP, rand(GetX-20, GetX+20), GetClipMinY);  
		}
		if(timer<40){
			CreateItem(ITEM_BOMB, rand(GetX-20, GetX+20), GetClipMinY);  
		}
		if(timer<60){
			CreateItem(ITEM_1UP, rand(GetX-20, GetX+20), GetClipMinY);  
		}
		if(timer<90){
			CreateItem(ITEM_BOMB, rand(GetX-20, GetX+20), GetClipMinY);  
		}
	}
	if (GetArgument==4) {
		if(timer==0){
			CreateItem(ITEM_1UP, rand(GetX-20, GetX+20), GetClipMinY); 
		}
		if(timer<20){
			CreateItem(ITEM_BOMB, rand(GetX-20, GetX+20), GetClipMinY);  
		}
		if(timer<40){
			CreateItem(ITEM_1UP, rand(GetX-20, GetX+20), GetClipMinY);  
		}
		if(timer<60){
			CreateItem(ITEM_BOMB, rand(GetX-20, GetX+20), GetClipMinY);  
		}
	}
	if (GetArgument==3) {
		if(timer==0){
			CreateItem(ITEM_BOMB, rand(GetX-20, GetX+20), GetClipMinY); 
		}
		if(timer<20){
			CreateItem(ITEM_1UP, rand(GetX-20, GetX+20), GetClipMinY);  
		}
		if(timer<40){
			CreateItem(ITEM_BOMB, rand(GetX-20, GetX+20), GetClipMinY);  
		}
	}


	loop(GetArgument*((120-timer)/5) ){
		CreateItem(ITEM_SCORE, rand(GetX-20, GetX+20), rand(GetClipMinY+20, GetClipMinY+50));  
	}

   }

   @DrawLoop
   {

            SetTexture(bg);
            SetRenderState(ALPHA);
            SetAlpha(255);
            SetGraphicRect(0, 0, 50000, 50000);

            SetGraphicScale(1,1);
            SetGraphicAngle(0, 0, 0);
            DrawGraphic(GetCenterX, GetCenterY-50);


			SetTexture(bonus);
		    SetRenderState(ALPHA);
		    SetAlpha(255);
		    SetGraphicRect(0, 0, 150, 80);
			SetGraphicScale(1,1);
            		SetGraphicAngle(0, 0, 0);
			DrawGraphic(GetCenterX, GetCenterY-50);


	DrawText("Bonus", GetClipMinX+10, GetClipMinY+15, 12, 255);
   }


   task Bullet(x, y, v, angle, graphic) {
     let obj_id=Obj_Create(OBJ_SHOT);

     Obj_SetPosition(obj_id, x, y);
     Obj_SetAngle(obj_id, angle);
     Obj_SetSpeed(obj_id, v);
     ObjShot_SetGraphic(obj_id, graphic);
     ObjShot_SetDelay  (obj_id, 0);
	let approche=false;
     while(Obj_BeDeleted(obj_id)==false) {
	if(absolute( GetPlayerX-Obj_GetX(obj_id) )<30 && absolute( GetPlayerY-Obj_GetY(obj_id) )<30 && approche==false)
{
     ObjShot_SetGraphic(obj_id, graphic+10);
     Obj_SetSpeed(obj_id, 0);
	Wait(60);
	approche=true;
	Obj_SetAngle(obj_id, atan2(GetPlayerY-Obj_GetY(obj_id), GetPlayerX-Obj_GetX(obj_id)) );
}
	if(approche){
		if(Obj_GetSpeed(obj_id)<v) {
			Obj_SetSpeed(obj_id, Obj_GetSpeed(obj_id)+0.02 );
		}

	}
             yield;
      }
}

}
