
#Title[pXBXy1]
#Text[pXBXy1]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
     let name        = "i@uO[Xg[J[v";
     let csd = GetCurrentScriptDirectory;
        let gx = GetX;
        let gy = GetY;
        let gspeed = GetSpeed;
        let gangle = GetAngle;     
        let arg = GetArgument;
   let imgBOSS = csd~"img\character\dot-kaze-ti2.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\warp01.wav";
   let se2 = csd~ "bgm\tm2_coin000.wav";
   let se3 = csd~ "bgm\pyoro03.wav";
   let se4 = csd~ "bgm\countdown.wav";
   let arrayL = [];
let dif = GetCommonData("Dif");
 if(dif == 2){name = "i@uO[`FCT[v";}
   @Initialize{
         CutIn(KOUMA, name, "", 0, 0, 0, 0);
           if(GetPlayerType == REIMU_A){SetLife(1250);}
            else{SetLife(1250);}
           SetTimer(60);
           SetDamageRate(50,40);
    /* if(dif == 2){SetDamageRate(40,0);}*/
           SetScore(1500000);
           SetInvincibility(180);
           SetGraphicRect(64,1,128,64);
           SetMovePosition03(GetCenterX,GetClipMinY+100,10,5);	

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

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

         yield;
       }

   @DrawLoop{  
        DrawGraphic(GetX,GetY);
     }

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

      task Shot{
  let v = 2;
     let n = 1;
     if(dif == 2){v = 6;}
       loop(n){
           let radius = 0;
          let angle = 90;
          let x = rand(GetClipMinX,GetClipMaxX);
          let y = GetClipMinY;
         let color = 52;
          let delay = 0;
         Monster(x,y,v,angle,color,delay);
        }
    }

     task Move{
       wait(600);
      loop{ 
        let x = rand(GetClipMinX+60,GetClipMaxX-60);
        let y = rand(GetClipMinY+32,GetCenterY);
        SetMovePosition03(x,y,20,3);
        wait(600);
         }
    }
       
      task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }

     task Monster(x,y,v,angle,color,delay){
            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);
            ObjShot_SetBombResist(b,true); 
           Obj_SetAutoDelete(b,false);           
         if(dif == 1){Chase;}
      if(dif == 2){Angle; ObjShot_SetGraphic(b,color);}
         Trace;
     /*    wait(600);
         Obj_Delete(b);*/

         task Chase{
             loop{
                Obj_SetAngle(b,atan2(GetPlayerY-Obj_GetY(b),GetPlayerX-Obj_GetX(b)));
               wait(10);
               }
             } 
    task Angle{
  if(Obj_GetAngle(b) > 180){
       Obj_SetAngle(b,Obj_GetAngle(b)-360);
         }
       else if(Obj_GetAngle(b) <= -180){
       Obj_SetAngle(b,Obj_GetAngle(b)+360);
         }
    while(!Obj_BeDeleted(b)){
     let g = atan2(GetPlayerY-Obj_GetY(b),GetPlayerX-Obj_GetX(b));
       if(Obj_GetAngle(b) > 180){
       Obj_SetAngle(b,Obj_GetAngle(b)-360);
         }
        if(Obj_GetAngle(b) <= g-180){
       Obj_SetAngle(b,Obj_GetAngle(b)+360);
         } 
        Obj_SetAngle(b,Obj_GetAngle(b)-(Obj_GetAngle(b)-g)/30);
       wait(1);
        } 
       }
        task Trace{
           while(!Obj_BeDeleted(b)){
           let x = Obj_GetX(b)+offsetX(rand(0,32),rand(-180,180));
           let y = Obj_GetY(b)+offsetY(rand(0,32),rand(-180,180));
           let v = 0;
           let angle = atan2(GetPlayerY-Obj_GetY(b),GetPlayerX-Obj_GetX(b));
           let arraycolor = [4,12,28,36];
           let color = arraycolor[rand_int(0,3)];
           let delay = rand(10,60);
          let accel = 0.01;
      let ff = 600;
     if(dif == 2){accel = 0.02; ff = 360;}
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,v,angle,0,0,v,color);
         SetShotDataA(0,ff,v,NULL,0,accel,3,color);
       /*  SetShotKillTime(0,600);*/
         FireShot(0);
         PlaySE(se3);
     let w = 6;
    if(dif == 2){w = 2;}
         wait(w);
           }
         }
       }

    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;}
       }
}