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

script_enemy_main{
     let name        = "╄@up[tFNgChv";
     let csd = GetCurrentScriptDirectory;
        let gx = GetX;
        let gy = GetY;
        let gspeed = GetSpeed;
        let gangle = GetAngle;     
        let arg = GetArgument;
   let imgBOSS = csd~"img\character\dot_sakuya.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\arrow01.wav";
   let se2 = csd~ "bgm\cursor02.wav";
   let se3 = csd~ "bgm\clock02.wav";
   let se4 = csd~ "bgm\countdown.wav";
    let effect = csd ~ "img\magic_circle\powercircle.png";
   let arrayL = [];
 let out = GetCommonData("TimeOut");
 let dif = GetCommonData("Dif");
  if(dif == 2){ name    = "p@uChE@Gv";}
   @Initialize{
     if(!out){
        CutIn(KOUMA, name, "", 0, 0, 0, 0);
           if(GetPlayerType == REIMU_A){SetLife(800);}
            else{SetLife(800);}
           SetTimer(60);
        SetScore(6000000);
       SetDamageRate(20,10);
       MagicCircle(false);
     }
           SetInvincibility(120);
           SetGraphicRect(0*128,0,1*128,128);   
           SetMovePosition03(GetCenterX,GetClipMinY+140,20,5);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
      SetEffectForZeroLife(1,0,0);
           TMain;
    }

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

         yield;
       }

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

    @Finalize{
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
        DeleteSE(se1);DeleteSE(se2);DeleteSE(se3);DeleteSE(se4);    
        if(!IsTimeOut && !out){
                loop(100){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
              }
          CreateItem(ITEM_1UP,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
           }  
   DeleteCommonData("TimeOut");      
     }
 
     task TMain{
          yield;
      Circle(40);
      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);
       wait(120);
        if(dif == 1){Shot;}
        else if(dif == 2){Shot2;}
    /*    Move; */
        Count;
    }

      task Shot{
    let way = 12;
       loop{
      let base = GetAngleToPlayer;
       ascent(b in 0..30){
         ascent(a in 0..way){
           let radius = 0;
          let angle = base+360/way*(a-(way-1)/2)+rand(4.5,5.5)*b;
          let angle2 = base+360/way*(a-(way-1)/2)-rand(4.5,5.5)*b;
          let v = 2;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
         let color = 174;
          let delay = 0;
         let accel = 0;
       let id = rand_int(0,3);
        CreateShot01(x,y,v,angle,color,delay);
        CreateShot01(x,y,v,angle2,color,delay);   
         PlaySE(se1);
          }
       wait(6);
       }
       Move; 
       wait(60);
       Concentration01(90);
        wait(90);
  SetGraphicRect(4*128,2*128,5*128,3*128);   
     let base = rand(-180,180);
     ascent(r in 0..6){
        let radius = 0;
          let v = 3;
          let color = 69;
          let delay = 0;
        ascent(a in 0..36){
          let angle = base + 10*a;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
        let id = rand_int(0,0);
        let color2 = 173;
        P(x,y,v,angle,color,delay,id,color2);
        }
        wait(6);
       }
         wait(30);
      timestop(90);  
    SetDamageRate(20,20);  
     }
    }

   task Shot2{
  let n = rand_int(0,1);
 Concentration01(120);
   wait(120);
  SetGraphicRect(4*128,2*128,5*128,3*128);   
   Blue(3,45);
   wait(180);
  loop{
 Yellow(26,6);
  SetGraphicRect(2*128,3*128,3*128,4*128);   
  SetMovePosition02(GetCenterX+60*(-1)^n,GetY-60,20);
 wait(20);
  SetMovePosition03(GetCenterX+120*(-1)^n,GetY,30,10);
   wait(60);
  SetGraphicRect(3*128,2*128,4*128,3*128);   
   Fire;  
   Blue(3,15);  
   wait(80);
 SetGraphicRect(5*128,0*128,6*128,1*128); 
   timestop(90);
 SetX(GetCenterX+120*(-1)^(n+1)); SetY(GetClipMinY+80);
   wait(90);
 SetGraphicRect(4*128,2*128,5*128,3*128);   
  Blue(3,45);
 /*Yellow(27,6);*/
  wait(120);
 Fire;
 wait(120);
 timestop(90);
 wait(90);
 SetInvincibility(60);
  SetX(GetCenterX+400); SetY(GetClipMinY+800);
  wait(60);
  SetAlpha(255);
  SetX(GetCenterX); SetY(GetClipMinY+140);
   wait(45);
  n += 1;
  SetDamageRate(20,20);
   }

     task Blue(v,n){
        let way = 12;
      let base = GetAngleToPlayer;
       ascent(b in 0..n){
         ascent(a in 0..way){
           let radius = 0;
          let angle = base+360/way*(a-(way-1)/2)+rand(4.5,5.5)*b;
          let angle2 = base+360/way*(a-(way-1)/2)-rand(4.5,5.5)*b;
         /* let v = 3;*/
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
         let color = 174;
          let delay = 20;
         let accel = 0;
       let id = rand_int(0,3);
        P2(x,y,v,angle,color,delay,id,172);
        P2(x,y,v,angle2,color,delay,id,172);   
          }
        PlaySE(se1);
       wait(4);
        }
      }

     task Fire{
   let color = 241;
    let v = 1.5;
    let x = GetX; let y = GetY;
     let delay = 20;
    ascent(a in 0..4){
     ascent(b in 0..5){
       ascent(c in 0..4){
       let id = rand_int(10,16);
      let angle = GetAngleToPlayer+5*(b-2)+90*a;
      P2(x,y,v-0.3*c,angle,color,delay,id,169);
        }
       }
     }
    ascent(a in 0..4){
     ascent(b in 0..3){
       ascent(c in 0..4){
       let id = rand_int(10,16);
      let angle = GetAngleToPlayer+5*(b-1)+90*a+45;
      P2(x,y,v-0.3*c,angle,color,delay,id,169);
        }
        }
       }
    }
    
   task Yellow(n,delay){
     let id = 100;
      let v = 2.5;
      let color = 171;
      /* let delay = 91;*/
      loop(n){
      let x = GetX; let y = GetY;
      let angle = GetAngleToPlayer+rand(-0,0);
     P2(x,y,v,angle,color,delay,id,176);
    /* delay -= 10;*/
  PlaySE(se1);
       wait(6);
       }
     }
  }

     task Move{
        let x = rand(100,100);
        let y = rand(0,32);
        let v = 1.2;
        let xmin = GetClipMinX+64;
        let ymin = GetClipMinY+30;
        let xmax = GetClipMaxX-64;
        let ymax = GetClipMinY+120;
        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);
         wait(90);
         SetGraphicRect(0*128,0,1*128,128); 
         wait(90);
    }

      task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }
     
  task P(x,y,v,angle,color,delay,id,color2){
        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);
    wait(delay);
      if(id >= 1 && id <= 10){
        Time;
       task Time{
        let w = rand(5,45);
             while(!IsBGStopping){
          wait(1);
          }
         Obj_SetSpeed(p,0);
             wait(w);
              Obj_SetAngle(p,rand(-180,180));
              ObjShot_SetGraphic(p,color2);  
          while(IsBGStopping){
             wait(1);
             }
     let v2 = rand(1,3);
       while(!Obj_BeDeleted(p)){
            Obj_SetSpeed(p,v2);
          while(!IsBGStopping){
          wait(1);
          }
            Obj_SetSpeed(p,0);
            while(!IsBGStopping){
          wait(1);
          }
        wait(1);
          }
         }
      }
         else{
            Time2;
         task Time2{
           while(!Obj_BeDeleted(p)){
          loop{
             if(IsBGStopping){
              Obj_SetSpeed(p,0);
             break;
             }
            else{Obj_SetSpeed(p,v);}
           wait(1);
         }
             while(IsBGStopping){
                wait(1);
              }    
            v = 1;
          }
         }        
      }
        Refrect;
        task Refrect{
       let n = 1;
           while(!Obj_BeDeleted(p)){
           let i = 0;
             loop(length(arrayL)){
             if(Collision_Obj_Obj(p,arrayL[i]) && n >= 1){
         Obj_SetAngle(p,2*Obj_GetAngle(arrayL[i])-Obj_GetAngle(p));
        wait(1);  
       n -= 1;
             break;
             }
         i++;
         if(i == 3){i = 0;}
         }
        wait(1);
        }
        }
  }

task P2(x,y,v,angle,color,delay,id,color2){
        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);
    wait(delay);
      if(id >= 1 && id <= 9){
        Time;
       task Time{
        let w = rand(5,45);
             while(!IsBGStopping){
          wait(1);
          }
         Obj_SetSpeed(p,0);
             wait(w);
              Obj_SetAngle(p,rand(-180,180));
              ObjShot_SetGraphic(p,color2);  
          while(IsBGStopping){
             wait(1);
             }
     let v2 = rand(1,3);
       while(!Obj_BeDeleted(p)){
            Obj_SetSpeed(p,v2);
          while(!IsBGStopping){
          wait(1);
          }
            Obj_SetSpeed(p,0);
            while(!IsBGStopping){
          wait(1);
          }
        wait(1);
          }
         }
      }
         if(id == 0 || id == 10){
            Time2;
         task Time2{
           while(!Obj_BeDeleted(p)){
            while(!IsBGStopping){
          wait(1);
          }
           Obj_SetSpeed(p,0);
          while(IsBGStopping){
          wait(1);
          }
            Obj_SetSpeed(p,v);
         wait(1);
           }
         }       
   }
   if(id >= 11 && id <= 19){
        Time;
       task Time{
        let w = rand(5,45);
             while(!IsBGStopping){
          wait(1);
          }
         Obj_SetSpeed(p,0);
             wait(w);
              Obj_SetAngle(p,rand(-180,180));
              ObjShot_SetGraphic(p,color2);  
          while(IsBGStopping){
             wait(1);
             }
  ObjShot_SetGraphic(p,color);  
     let v2 = rand(1,3);
       while(!Obj_BeDeleted(p)){
            Obj_SetSpeed(p,v2);
          while(!IsBGStopping){
          wait(1);
          }
            Obj_SetSpeed(p,0);
            while(!IsBGStopping){
          wait(1);
          }
        wait(1);
          }
         }
      }
  if(id == 100){
            Time3;
         task Time3{
           while(!Obj_BeDeleted(p)){
            while(!IsBGStopping){
          wait(1);
          }
           Obj_SetSpeed(p,0);
   let radius = 5; let s = 90;
     PlaySE(se2);
          while(IsBGStopping){
  if(!Obj_BeDeleted(p)){
  ascent(a in 0..2){
     let x = Obj_GetX(p) + offsetX(radius,Obj_GetAngle(p)+180*a);
     let y = Obj_GetY(p) + offsetY(radius,Obj_GetAngle(p)+180*a);
      CreateShotA(0,x,y,0); 
      SetShotDataA(0,0,0,angle,0,0,0,color2);
      SetShotDataA(0,s,6,angle,0,0,v,color2);
      FireShot(0);
      }
    }
       s -= 1;
     if(s < 1){s = 1;}
       radius += 10;
          wait(1);
          }
            Obj_SetSpeed(p,v);
         wait(1);
           }
         }       
   }
  }

  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 Circle(size){
           let c= Obj_Create(OBJ_EFFECT);
             Obj_SetX(c,GetX);
             Obj_SetY(c,GetY);
             Obj_SetAngle(c,0);
             ObjEffect_SetAngle(c,0,0,0);
             ObjEffect_SetTexture(c,effect);
             ObjEffect_SetRenderState(c,ADD);
             ObjEffect_SetPrimitiveType(c,PRIMITIVE_TRIANGLEFAN);
             ObjEffect_SetLayer(c,0);
             ObjEffect_CreateVertex(c,62);
             ObjEffect_SetVertexXY(c,0,0,0);
             ObjEffect_SetVertexUV(c,0,128,128);
             ObjEffect_SetVertexColor(c,0,255,255,255,255);
             ObjEffect_SetScale(c,size/10,size/10);

            ascent(let i in 0..61){
                ObjEffect_SetVertexXY(c,i+1,cos(i*6)*20,sin(i*6)*20);
                ObjEffect_SetVertexUV(c,i+1,cos(i*6)*130+130,sin(i*6)*130+130);
                ObjEffect_SetVertexColor(c,i+1,255,255,255,255);
            }
       Spin;
       Up;
     task Spin{
      let h = 0;
         loop{
            Obj_SetX(c,GetX); Obj_SetY(c,GetY);
            ObjEffect_SetAngle(c,0,0,h);
            h += 8;
            wait(1);
           }
         }
       task Up{
            loop{
             ascent(a in 0..30){
            Obj_SetX(c,GetX); Obj_SetY(c,GetY);
            ObjEffect_SetScale(c,(size+a/5)/10,(size+a/5)/10);
            wait(1);
             }
            wait(10);
             descent(a in 0..30){
            Obj_SetX(c,GetX); Obj_SetY(c,GetY);
            ObjEffect_SetScale(c,(size+a/5)/10,(size+a/5)/10);
            wait(1);
             }
            wait(10);
           }
        }
    }

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

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

       task timestop(time){
          TimeStop(time,1,0,1);
          MotionBlurEx(ALL,time,200,ADD);
       loop(time/10){PlaySE(se3); wait(15);} 
     }
            
        function sc(r,g,b){
             SetShotColor(r,g,b);
     }

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

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