
#Title[ルーミアスペル1]
#Text[ルーミアスペル1]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
     let name        = "夜符　「ナイトバタフライ」";
     let csd = GetCurrentScriptDirectory;
        let gx = GetX;
        let gy = GetY;
        let gspeed = GetSpeed;
        let gangle = GetAngle;     
        let arg = GetArgument;
   let imgBOSS = csd~"img\character\ExRumia.png";
   let wIni  = 120;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\tm2_gun007_miniguned.wav";
   let se2 = csd~ "bgm\tm2_sonic003.wav";
   let se3 = csd~ "bgm\concentration.wav";
   let se4 = csd~ "bgm\countdown.wav";
   let arrayL = [];
let dif = GetCommonData("Dif");
 if(dif == 2){
   name        = "夜符　「ナイトバタフライ -Lunatic-」";
  }
   @Initialize{
        CutIn(KOUMA, name, "", 0, 0, 0, 0);
           if(GetPlayerType == REIMU_A){SetLife(1200);}
            else{SetLife(1200);}
           SetTimer(60);
           SetScore(1000000);
           SetDamageRate(45,50);
   /*    if(dif == 2){SetDamageRate(45,0);}*/
           SetInvincibility(wIni);
           SetGraphicRect(1,1,64,64);
           SetMovePosition02(GetCenterX,GetClipMinY+120,120);	

           LoadGraphic(imgBOSS);
          /* SetTexture(imgBOSS); */
           setGraphicStop;
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
           TMain;
    }

   @MainLoop{
           SetCollisionA(GetX, GetY, 24);
           SetCollisionB(GetX, GetY, 24);

         yield;
       }

   @DrawLoop{
         SetTexture(imgBOSS);  
        DrawGraphic(GetX,GetY);
     }

    @Finalize{
        DeleteGraphic(imgBOSS);
        DeleteSE(se1);DeleteSE(se2);DeleteSE(se3);DeleteSE(se4);
        if(!IsTimeOut){
                loop(48){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,16),rand(-180,180)));
              }
           }
     }
 
     task TMain{
          yield;
          wait(120);

  if(dif == 1){Shot;}
  else if(dif == 2){Shot2;}
     Count;
    }

      task Shot{
         loop{
           ascent(i in 0..6){
           loop(i*8+5){
          let angle = rand(-180,0);
          let x = GetX+offsetX(32,angle);
          let y = GetY+offsetY(32,angle);
          let v = rand(10,20);
          let color = 225;
          let delay = 20;
          let d = rand(-5,5);
          let vmax = rand(0.5,4);
          Butterfly(x,y,v,angle,color,delay,d,vmax);
         PlaySE(se1);
             }
          wait(15);
          }
       Move;
        wait(180);
      }     
    }

 task Shot2{
         loop{
           ascent(i in 0..9){
           loop(i*8+5){
          let angle = rand(-180,0);
          let x = GetX+offsetX(32,angle);
          let y = GetY+offsetY(32,angle);
          let v = rand(10,25);
          let color = 225;
          let delay = 20;
          let d = rand(-5,5);
          let vmax = rand(1,5);
          Butterfly(x,y,v,angle,color,delay,d,vmax);
         PlaySE(se1);
             }
          wait(10);
          }
       Move;
        wait(180);
      }     
    }

  task Move{
        let x = rand(48,64);
        let y = rand(0,32);
        let v = 2;
        let xmin = GetClipMinX+96;
        let ymin = GetClipMinY+60;
        let xmax = GetClipMaxX-96;
        let ymax = GetCenterY-60;
    /*    if(x>=GetX){SetGraphicRect(1*128,1*128,2*128,2*128);}
       else if(x<=GetX){SetGraphicRect(1*128,1*128,2*128,2*128);}*/
         SetMovePositionRandom01(x,y,v,xmin,ymin,xmax,ymax);
      /*   wait(90);
         SetGraphicRect(0*128,0,1*128,128); 
         wait(90); */
    }

     task Butterfly(x,y,v,angle,color,delay,d,vmax){
            let b = Obj_Create(OBJ_SHOT);
            Obj_SetX(b,x);
            Obj_SetY(b,y);
            Obj_SetSpeed(b,v);
            Obj_SetAngle(b,angle);
            ObjShot_SetGraphic(b,color);
            ObjShot_SetDelay(b,delay);

        while(Obj_GetSpeed(b)>=0.02){
               Obj_SetAngle(b,Obj_GetAngle(b)+d);
               Obj_SetSpeed(b,Obj_GetSpeed(b)-0.5);
             wait(1);
              }
          wait(30);
         PlaySE(se2);
                loop(40){
                Obj_SetAngle(b,Obj_GetAngle(b)+d);
                if(Obj_GetSpeed(b) <= vmax){Obj_SetSpeed(b,Obj_GetSpeed(b)+0.1);}
                 wait(2);
                 }
        }

     task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }
            

    function offsetX(radius,angle){
           return radius * cos(angle);
     }

        function offsetY(radius,angle){
            return radius * sin(angle);
     }           

       function move(f){
          SetMovePosition02(rand(GetClipMinX+60,GetClipMaxX-60),GetY+rand(-20,20),f);
     }
            
        function sc(r,g,b){
             SetShotColor(r,g,b);
     }

       sub setGraphicStop  { SetGraphicRect( 1, 1, 64, 64); }

       function wait(w){
                loop(w){yield;}
       }
}