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

script_enemy_main{
    #include_script".\function_effect.txt"
    #include_script".\function_bullet.txt"
     let name        = "֒e@uth[yfv";
     let csd = GetCurrentScriptDirectory;
        let gx = GetX;
        let gy = GetY;
        let gspeed = GetSpeed;
        let gangle = GetAngle;     
        let arg = GetArgument;
   let imgBOSS = csd~"img\character\dot_flandre.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\tm2_swing001.wav";
   let se2 = csd~ "bgm\eco02_a.wav";
   let se3 = csd~ "bgm\bell01.wav";
   let se4 = csd~ "bgm\countdown.wav";
    let effect = csd ~ "img\magic_circle\powercircle.png";
   let arrayL = [];
 let dif = GetCommonData("Dif");
  if(dif == 2){name   = "֒e@uth[yf -Lunatic-v";}
   @Initialize{
       /*    CutIn(KOUMA, name, "", 0, 0, 0, 0); */
           if(GetPlayerType == REIMU_A){SetLife(1400);}
            else{SetLife(1400);}
     /*      SetTimer(90);
      SetScore(6000000);  */
       SetDamageRate(20,5);
           SetInvincibility(120);
        /*   SetGraphicRect(3*128,0,4*128,128);   
           SetMovePosition03(GetCenterX,GetCenterY-60,20,5);  */
       MagicCircle(false);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
           TMain;
    }

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

         yield;
       }

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

    @Finalize{
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
        DeleteSE(se1);DeleteSE(se2);DeleteSE(se3);DeleteSE(se4);   
       if(!IsTimeOut){
                loop(80){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
              }
           }           
     }
 
     task TMain{
          yield;
  Circle(40);
    wait(120);
  CutIn(KOUMA, name, "", 0, 0, 0, 0);
   SetTimer(90);
     SetScore(6000000);
   PlaySE("seUseSpellCard.wav");
     SetGraphicRect(3*128,0,4*128,128);   
    SetMovePosition03(GetCenterX,GetCenterY-60,20,5); 
 wait(120);
     PlaySE(se2);
    Spark(120,255,0,0,true);
        wait(90);
     SetGraphicRect(4*128,2*128,5*128,3*128);
        if(dif == 1){Shot;}
     else if(dif == 2){Shot2;}
        Count;
    }

      task Shot{
      let radius = 24;
      let c = 49;
       let delay = 0;
       ascent(b in 0..4){ 
          let v = b*3+1;
          ascent(a in 0..8){
          let v2 = v+amari(a,3)/* v+(-1)^a/2*/;
          let angle = -22.5+360/8*(a-4);
          let color = c+a;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
          let spin = (-1)^a/2;
          P(x,y,v2,angle,color,delay,spin);
         PlaySE(se3);
          } 
     }
     wait(120);
       loop{
           let x = GetClipMinX; let x2 = GetClipMaxX;
           let y = rand(GetClipMinY,GetClipMaxY);
           let v = 0.5;
           let color = rand_int(153,160);
           let delay = 0;
           CreateShot01(x,y,v,0+rand(-30,30),color,delay);
           CreateShot01(x2,y,v,180+rand(-30,30),color,delay);
           wait(30);
          }
    }

   task Shot2{
      let radius = 24;
      let c = 49;
       let delay = 0;
       ascent(b in 0..5){ 
          let v = b*2+1.7;
          ascent(a in 0..8){
          let v2 = v+(-1)^a/2;
          let angle = -22.5+360/8*(a-4);
          let color = c+a;
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
          let spin = (-1)^a/2;
          P(x,y,v2,angle,color,delay,spin);
         PlaySE(se3);
          } 
     }
     wait(120);
       loop{
           let x = GetClipMinX; let x2 = GetClipMaxX;
           let y = rand(GetClipMinY,GetClipMaxY);
           let x3 = rand(GetClipMinX,GetClipMaxX);
          let y2 = GetClipMinY; let y3 = GetClipMaxY;
           let v = rand(0.3,1.2);
           let color = rand_int(153,160);
           let delay = 10;
           CreateShot01(x,y,v,0+rand(-30,30),color,delay);
           CreateShot01(x2,y,v,180+rand(-30,30),color,delay);
           CreateShot01(x3,y2,v,90+rand(-30,30),color,delay);
           CreateShot01(x3,y3,v,-90+rand(-30,30),color,delay);
           wait(20);
          }
    }

    task P(x,y,v,angle,color,delay,spin){
        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,false);
         ObjShot_SetBombResist(p,true);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
        wait(30);
        Obj_SetSpeed(p,0);
        wait(90);
        let n = 1.5;
       let radius = ((Obj_GetX(p)-GetX)^2+(Obj_GetY(p)-GetY)^2)^0.5;
       Set;  let im = 0;
        task Set{
            while(!Obj_BeDeleted(p)){
             Obj_SetAngle(p,Obj_GetAngle(p)+spin*n);   
             Obj_SetX(p,GetX+offsetX(radius,Obj_GetAngle(p)));
             Obj_SetY(p,GetY+offsetY(radius,Obj_GetAngle(p)));
             wait(1);
         if(GetLife<=500||GetTimer<=20 && im == 0){n = rand(0.5,1.5);}
         im += 1;
          if(im > 60){im = 0;} 
             }
         }   
    }

     task Move{
       wait(30);
      loop{
        let x = rand(-64,64);
        let y = rand(-32,32);
        let v = 2;
        let xmin = GetClipMinX+100;
        let ymin = GetClipMinY+60;
        let xmax = GetClipMaxX-100;
        let ymax = GetCenterY-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(3*128,0,4*128,128);
         wait(90);
         }
    }
       
      task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }

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

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

     function amari(a,b){
        return trunc(a-trunc(a/b)*b);
     }  

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

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