
#Title[dʏe2]
#Text[dʏe2]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
     let csd = GetCurrentScriptDirectory;
        let gx = GetX;
        let gy = GetY;
        let gspeed = GetSpeed;
        let gangle = GetAngle;     
        let arg = GetArgument;
   let imgBOSS = csd~"img\character\dot_daiyousei.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\arrow01.wav";
   let se2 = csd~ "bgm\eco00.wav";
   let se3 = csd~ "bgm\concentration.wav";
   let se4 = csd~ "bgm\countdown.wav";
    let effect = csd ~ "img\magic_circle\powercircle.png";
   let arrayL = [];
   let dif = GetCommonData("Dif");
 let out = GetCommonData("TimeOut");
   @Initialize{
       if(!out){
           if(GetPlayerType == REIMU_A){SetLife(2000);}
            else{SetLife(2000);}
           SetTimer(45);
       SetDamageRate(100,50);
       }
           SetInvincibility(120);
           SetGraphicRect(0*128,0*128,1*128,1*128);   
           SetMovePosition03(GetCenterX,GetClipMinY+120,20,5);
        SetAlpha(255);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          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{
      DeleteCommonData("TimeOut"); 
     if(!out){SetCommonData("TimeOut",IsTimeOut);}
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
        DeleteSE(se1);DeleteSE(se2);DeleteSE(se3);DeleteSE(se4); 
         if(!IsTimeOut && !out){
                loop(30){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
              }
      /*    CreateItem(ITEM_BOMB,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));  */
           }           
     }
 
     task TMain{
          yield;
     Concentration01(90);
       PlaySE(se2);
       wait(90);
      L(GetClipMinX-20,GetClipMinY-20,0,600,1,WHITE01);
      L(GetClipMaxX+20,GetClipMinY-20,90,600,1,WHITE01);
      L(GetClipMinX-20,GetClipMinY-20,90,600,1,WHITE01);
        Shot;
        Count;
    }

      task Shot{
        if(dif == 1){Shot1;}
  else if(dif == 2){Shot2;}
     task Shot1{
        let accel = 0.001;
        let v = 1;
        let radius = 32;
        let delay = 0;
        let c = [169,174];
       loop{
         let base = rand(-180,180);
        ascent(d in 0..10){
         ascent(a in 0..4){
           ascent(b in 0..2){
          let v2 = v-0.3*b;
          let angle = base+90*a+6*d;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
         let color = c[1];
      Knife(x,y,v2,angle,color,delay,accel);
         PlaySE(se1);
          }
      }
       wait(3);
        }
      wait(90); 
    move; 
  base = rand(-180,180);
     ascent(d in 0..10){
         descent(a in 0..4){
           ascent(b in 0..2){
         let v2 = v-0.3*b;
          let angle = base-90*a-6*d;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
         let color = c[0];
      Knife(x,y,v2,angle,color,delay,accel);
         PlaySE(se1);
          }
      }
       wait(3);
        }
     SetGraphicRect(0*128,0*128,1*128,1*128);   
      wait(120);
      accel += 0.001;
      v += 0.1;
        }
       }

      task Shot2{
        let accel = 0.001;
        let v = 1;
        let radius = 32;
        let delay = 0;
        let c = [169,174];
       loop{
         let base = rand(-180,180);
        ascent(d in 0..12){
         ascent(a in 0..5){
           ascent(b in 0..2){
          let v2 = v-0.3*b;
          let angle = base+72*a+5*d;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
         let color = c[1];
      Knife(x,y,v2,angle,color,delay,accel);
         PlaySE(se1);
          }
      }
       wait(4);
        }
      wait(90); 
    move; 
  base = rand(-180,180);
     ascent(d in 0..12){
         descent(a in 0..5){
           ascent(b in 0..2){
         let v2 = v-0.3*b;
          let angle = base-72*a-5*d;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
         let color = c[0];
      Knife(x,y,v2,angle,color,delay,accel);
         PlaySE(se1);
          }
      }
       wait(4);
        }
     SetGraphicRect(0*128,0*128,1*128,1*128);   
      wait(120);
      accel += 0.001;
      v += 0.1;
        }
       }  
    }

     function move{
        let x = rand(64,128);
        let y = rand(16,64);
        let v = 1;
        let xmin = GetClipMinX+64;
        let ymin = GetClipMinY+60;
        let xmax = GetClipMaxX-64;
        let ymax = GetCenterY-60;
        if(x>=GetX){SetGraphicRect(2*128,3*128,3*128,4*128);}
       else if(x<=GetX){SetGraphicRect(2*128,1*128,3*128,2*128);}
         SetMovePositionRandom01(x,y,v,xmin,ymin,xmax,ymax);
    }
       
      task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }
     
   task L(x,y,angle,length,width,color){
              let l = Obj_Create(OBJ_LASER);
              arrayL = arrayL ~ [l];
              Obj_SetX(l,x);
              Obj_SetY(l,y);
              Obj_SetAngle(l,angle);
              Obj_SetAutoDelete(l,false);
              ObjShot_SetGraphic(l,color);
              Obj_SetCollisionToObject(l,true);
              Obj_SetCollisionToPlayer(l,true);
              ObjLaser_SetLength(l,length);
              ObjLaser_SetWidth(l,width);
              ObjLaser_SetSource(l,true);
              Obj_SetCollisionToPlayer(l,true);
     }

     task Knife(x,y,v,angle,color,delay,accel){
        let p = Obj_Create(OBJ_SHOT);
         Obj_SetX(p,x);
         Obj_SetY(p,y);
         Obj_SetSpeed(p,v);
         Obj_SetAngle(p,angle);
         ObjShot_SetGraphic(p,color);
         ObjShot_SetDelay(p,delay);
         Obj_SetAutoDelete(p,true);
         ObjShot_SetBombResist(p,false);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);

       Accel;
      task Accel{
           loop{ 
            Obj_SetSpeed(p,Obj_GetSpeed(p)+accel);
            wait(1);
            }
          }
         while(!Obj_BeDeleted(p)){
           let i = 0;
             loop(length(arrayL)){
             if(Collision_Obj_Obj(p,arrayL[i])){
        CreateShot02(Obj_GetX(p),Obj_GetY(p),Obj_GetSpeed(p),2*Obj_GetAngle(arrayL[i])-Obj_GetAngle(p),accel,Obj_GetSpeed(p)+2,color,delay);    
             Obj_Delete(p); 
        wait(1);  
             break;
             }
         i++;
         if(i == 4){i = 0;}
         }
        wait(1);
        }
    }


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

        function offsetY(radius,angle){
            return radius * sin(angle);
     }           
            
        function sc(r,g,b){
             SetShotColor(r,g,b);
     }

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

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