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

script_enemy_main{
    #include_script".\function_effect.txt"
    #include_script".\function_bullet.txt"
     let name        = "֊@uXgx[Xbvv";
     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\buble03.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   = "֊@uu[x[}WbNv";}
   @Initialize{
           CutIn(KOUMA, name, "", 0, 0, 0, 0);
           if(GetPlayerType == REIMU_A){SetLife(600);}
            else{SetLife(600);}
           SetTimer(60);
      SetScore(4000000);
       SetDamageRate(20,10);
           SetInvincibility(120);
           SetGraphicRect(3*128,0,4*128,128);   
           SetMovePosition03(GetCenterX,GetClipMinY+120,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, 24);

         yield;
       }

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

    @Finalize{
   SetShotColor(255,255,255);
        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;
     PlaySE(se2);
    Spark(120,255,0,0,true);
       wait(90);
      Circle(40);
        if(dif == 1){
        Shot;
        Strawbelly;
        Move;
         }
     else if(dif == 2){
 SetShotColor(64,64,255);
       Shot2;
        Bluebelly;
         }
  Blind;
        Count;
    }

      task Shot{
       wait(60);
      let way = 60;
       let n = 120;
       loop{
       PlaySE(se3); 
         loop(way){
           let radius = 24;
          let angle = GetAngleToPlayer+rand(-60,60)+180;
          let v = rand(0.7,1.5);
          let x = GetX+offsetX(radius,angle);
          let y = GetY+offsetY(radius,angle);
         let color = 41;
          let delay = 0;
         let accel = 0.01; 
         let grvAngle = GetAngleToPlayer;
      GravityShot(x,y,v,angle,accel,grvAngle,color,delay);
       }
      wait(120); 
        if(GetLife>=400){n = 120;}
         else{n -= 10;} 
        if(n <= 60){n = 60;}
     }
    }

     task Strawbelly{
          let radius = 64;
          let color = 25;
           let delay = 20;
           let v = 6;
          let kill = 30;
          loop{
          let angle = rand(-180,180);
           let x = GetPlayerX+offsetX(radius,angle);
          let y = GetPlayerY+offsetY(radius,angle);
           let access = angle+90;
           let anglev = 2*pi;
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,v,access,anglev,0,v,color);
         SetShotKillTime(0,kill);
         FireShot(0);
         PlaySE(se1);
         wait(1);
         if(GetLife>=400){kill = 30;}
         else{kill += 0.1;}
            }
        }

   task Shot2{
   wait(60);
   let v = 1.2;
    let color = 47; let delay = 20;
   loop{
     let x = GetX; let y = GetY;
    let base = GetAngleToPlayer;
  let m = 0; let n = 6; let way = 36;
    loop(18){
    ascent(a in 0..way){
  SetShotColor(64,64,255);
     let angle = base+360/way*a+m;
      CreateShot01(x,y,v,angle,color,delay);
      }
    m += n;
    PlaySE(se1);
     wait(10);
     }
    Move2;
    wait(90);
    m = 0;
   x = GetX;  y = GetY;
  loop(18){
  ascent(a in 0..36){
  SetShotColor(64,64,255);
     let angle = base-360/way*a+m;
      CreateShot01(x,y,v,angle,color,delay);
      }
  PlaySE(se1);
    m -= n;
     wait(10);
     }
    Move2;
    wait(90);
    }
  }

  task Bluebelly{
          let radius = -160;
          let color = 30;
           let delay = 20;
           let v = 3;
    let way = 12;
          loop{
   let ddd = 45;  let n = 5;
  loop(28){
   let base = rand(-180,180);
         ascent(a in 0..way){
        let angle = base + 360/way*a;
           let x = GetPlayerX+offsetX(radius,angle-ddd);
          let y = GetPlayerY+offsetY(radius,angle-ddd);
         CreateShot02(x,y,v,angle,-0.1,1,color,delay);
         }
         PlaySE(se1);
       ddd -= n;
     if(ddd <= 10){n = -n;}
     if(ddd >= 45){n = -n;}
         wait(20);
            }
       ddd = -45;  n = 5;
  loop(28){
   let base = rand(-180,180);
         ascent(a in 0..way){
        let angle = base + 360/way*a;
           let x = GetPlayerX+offsetX(radius,angle-ddd);
          let y = GetPlayerY+offsetY(radius,angle-ddd);
         CreateShot02(x,y,v,angle,-0.1,1,color,delay);
         }
         PlaySE(se1);
       ddd += n;
     if(ddd <= -45){n = -n;}
     if(ddd >= -10){n = -n;}
         wait(20);
            }  
          }
        }

   task Blind{
   let color = 16;
   loop{
   loop(30){ 
   CreateShotA(0,GetPlayerX,GetPlayerY,30); 
         SetShotDataA(0,0,0,0,0,0,0,color);
        SetShotKillTime(0,0);
         FireShot(0);
      } 
       wait(40);
      }
   }

     task Move{
       wait(30);
      loop{
        let x = rand(-64,64);
        let y = rand(-32,32);
        let v = 2;
        let xmin = GetPlayerX-100;
        let ymin = GetClipMinY+60;
        let xmax = GetPlayerX+100;
        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);
         wait(90);
         SetGraphicRect(3*128,0,4*128,128);
         wait(90);
         }
    }
     
 task Move2{
        let x = rand(12,32);
        let y = rand(0,32);
        let v = 2;
        let xmin = GetPlayerX-64;
        let ymin = GetClipMinY+60;
        let xmax = GetPlayerX+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);
         wait(90);
         SetGraphicRect(3*128,0,4*128,128);
    }
  
      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);
     }

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

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