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

script_enemy_main{
    let name        = "`@uρν―Μ €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_yukari.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\bell01.wav";
   let se2 = csd~ "bgm\eco02_a.wav";
   let se3 = csd~ "bgm\warp03.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   = "`@u½eE@`Border of Real`v";}
   @Initialize{
     /*  CutIn(KOUMA, name, "", 0, 0, 0, 0); */
           if(GetPlayerType == REIMU_A){SetLife(1);}
            else{SetLife(1);}
   /*        SetTimer(65);
     SetScore(12000000);  */
       SetDamageRate(0,0);
           SetInvincibility(120);
           SetGraphicRect(0*128,0,1*128,128);   
    /*       SetMovePosition03(GetCenterX,GetClipMinY+120,20,5);
        SetAlpha(64);  */
    SetDurableSpellCard;
      MagicCircle(false);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
   SetEffectForZeroLife(60,64,1);
           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);  
        loop(250){
              CreateItem(ITEM_SCORE,GetX+2*offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));     
         } 
    if(GetMissCountInThisSpell == 0){
       loop(3){
              CreateItem(ITEM_BOMB,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
              }
         }  
      else if(GetMissCountInThisSpell != 0){
        CreateItem(ITEM_1UP,GetX,GetY);  
         }         
     }
 
     task TMain{
          yield;
   if(dif == 2){BorderBreak;}
      Circle(40);
   if(dif == 1){
    L(GetClipMinX-20,GetClipMinY-20,0,600,1,WHITE01);
    L(GetClipMinX-20,GetClipMaxY+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);
  CutIn(KOUMA, name, "", 0, 0, 0, 0);
     SetScore(12000000);
   PlaySE("seUseSpellCard.wav");
  if(dif == 1){SetMovePosition03(GetCenterX,GetClipMinY+120,20,5); SetTimer(64);}//64
  else if(dif == 2){SetMovePosition03(GetCenterX,GetCenterY,20,5); SetTimer(90);}//90
        SetAlpha(128);
     SetDamageRate(0,0);
  wait(120);
    SetGraphicRect(0*128,2*128,1*128,3*128); 
        if(dif == 1){Shot;}
    else if(dif == 2){Shot2;}
        Count;
    }

      task Shot{
    Bullet;  
   task Bullet{
   let way = 12;
      let delay = 10;
     let v = 1;
     let color = 113;
   let w = 180; let n = 0;
   loop{
   let base =rand(-180,180);
       let x = GetX; let y = GetY;
  ascent(b in 0..5){
       ascent(a in 0..way){
        let angle = base+360/way*a+6*b*(-1)^n;
        P(x,y,0.75+v/3*b,angle,color,delay,0,0);
       }
     }
       PlaySE(se1);
    wait(w);
        Move;
          wait(w);
    v += 0.02;  w -= 9.3; color += 1; n+=1;
   if(w < 10){w = 10;} 
  if(color > 120){color = 113;}
      }
     }
    }

  task Shot2{
   while(GetTimer > 88){wait(1);}
   sc(128,128,128);
    let color = 54; let delay = 0;
   let v = -1.5;   let radius = 200;
   let x = GetX; let y = GetY;
    ascent(a in 0..2){
   let angle = 180*a;
    Border(0,x,y,v,angle,color,delay,radius);
      }
  sc(255,255,255);   

   while(GetTimer > 73){wait(1);}
     sc(128,128,128);
    v = 1.3;  
    ascent(a in 0..4){
   let angle = 90*a+45;
    Border(1,x,y,v,angle,color+1,delay,radius+20);
      }
    sc(255,255,255);  
/* SetShotAutoDeleteClip(200,200,200,200);*/

    while(GetTimer > 60){wait(1);}
 sc(128,128,128);
  ascent(b in 0..4){
    ascent(a in 0..2){
   let angle = 180*a+90*b;  v = 2*(-1)^a;  
    Border(2+a,x,y,v,angle,color+a,delay,radius+20);
      }
    }
  sc(255,255,255);  

 while(GetTimer > 47){wait(1);}
  sc(128,128,128); 
   ascent(a in 0..2){
   let angle = 180*a+30; v = 2;
    Border(9,x,y,v,angle,color-1,delay,radius+40);
      }
  sc(255,255,255); 

 while(GetTimer > 15){wait(1);}
 sc(128,128,128); 
  SetShotAutoDeleteClip(64,64,64,64);
  ascent(b in 0..4){
    ascent(a in 0..2){
   let angle = 90*a+90*b;    
    Border(4+a,x,y,3*(-1)^a,angle,color+a,delay,radius+20);
      }
    }
  sc(255,255,255);  
 
/* while(GetTimer > 50){wait(1);}
   let length = 200; let width = 20;
   let color = 14; let delay = 120;
  ascent(a in 0..4){
   CreateLaserB(0,length,width,color,delay);
   SetLaserDataB(0,0,0,0,0,0,0,90*a,-0.5);
   FireShot(0);
    }
   wait(120);
   ascent(a in 0..4){
   CreateLaserB(0,length,width,color,delay);
   SetLaserDataB(0,0,0,length,0,90*a-90-60,-0.5,90*a+120,-0.5);
   FireShot(0);
    }   */
   }

     task Move{
        let x = rand(64,92);
        let y = rand(0,32);
        let v = 2;
        let xmin = GetClipMinX+64;
        let ymin = GetClipMinY+60;
        let xmax = GetClipMaxX-64;
        let ymax = GetClipMinY+180;
        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,2*128,1*128,3*128); 
         wait(90);
    }
       
      task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }
     
   task P(x,y,v,angle,color,delay,accel,vmin){
        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);
         ObjShot_SetBombResist(p,false);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
    Ref;
    task Ref{
   while(!Obj_BeDeleted(p)){
           let i = 0;
             loop(length(arrayL)){
             if(Collision_Obj_Obj(p,arrayL[i])){
         if(i == 0){Obj_SetY(p,GetClipMaxY+10);}
         else if(i == 1){Obj_SetY(p,GetClipMinY-10);}
         else if(i == 2){Obj_SetX(p,GetClipMinX-10);}
         else if(i == 3){Obj_SetX(p,GetClipMaxX+10);}
    CreateShot01(Obj_GetX(p),Obj_GetY(p),Obj_GetSpeed(p),
        Obj_GetAngle(p),color,0);
     Obj_Delete(p);
        wait(1);  
             break;
             }
         i++;
         if(i == 4){i = 0;}
         }
        wait(1);
        }
       }
  /*       Accel; */
        task Accel{
          while(Obj_GetSpeed(p) >= vmin){
            Obj_SetSpeed(p,Obj_GetSpeed(p)+accel);
             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,false);
              ObjLaser_SetLength(l,length);
              ObjLaser_SetWidth(l,width);
              ObjLaser_SetSource(l,false);
     }

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

     task Border(id,x,y,v,angle,color,delay,radius){
        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);
         ObjShot_SetBombResist(p,true);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,false);
     Spin;
   task Spin{
   let r = 0;
      while(!Obj_BeDeleted(p)){
        Obj_SetX(p,GetCenterX+offsetX(r,Obj_GetAngle(p)+180));
        Obj_SetY(p,GetCenterY+offsetY(r,Obj_GetAngle(p)+180));
      r += radius/90; 
    wait(1);
    if(r > radius){r = radius;}
     Obj_SetAngle(p,Obj_GetAngle(p)+v);
         }
      }
    wait(120); 
   Shot00;
   Shot01;
  task Shot01{
   while(!Obj_BeDeleted(p)){
   ascent(a in 0..3){
   CreateShot01(Obj_GetX(p),Obj_GetY(p),1.5,Obj_GetAngle(p)+180+60*(a-1),color+64,0);
    }
    wait(3);
      }
   }
   task Shot00{
 if(id == 0){
  let shottime = 76;
  let v = 1; 
  let vmax = 3; let color = 118;
   let delay = 0;   let m = 0;
  let w = 20; let w2 = 3;
  loop(10){
  let n = 0;
  let accel = v/60;
  let s = 30/w2;
  loop(s){
   let x = Obj_GetX(p); let y = Obj_GetY(p);
   let angle = Obj_GetAngle(p);
   let angle2 = angle+n;
   BorderShot(x,y,v,angle,angle2,accel,vmax,color,delay,shottime);
   PlaySE(se1);
   n += m;
     wait(w2);
      }  
   m += 0.01;
   v += 0.5;
    wait(w);
   w -= 1.5;
  if(w <= 15){w2 = 2;}
  if(w <= 10){w2 = 1;}
      }
      }
  else if(id == 1){
  let shottime = 62;
  let v = 1; 
  let vmax = 3; let color = 119;
   let delay = 0;   let m = 1;
  let w = 15; let w2 = 3; let n = 0;
  let s = [10,15,20,20,25,30]; let i = 0;
  loop(6){
  let accel = v/60;
  loop(s[i]){
   let x = Obj_GetX(p); let y = Obj_GetY(p);
   let angle = Obj_GetAngle(p);
   let angle2 = angle+n;
   BorderShot(x,y,v,angle,angle2,accel,vmax,color,delay,shottime);
   PlaySE(se1);
   n += m;
     wait(w2);
      }  
   m += 0.5;
   v += 1;
    wait(w);
  i += 1;
/*   w -= 1.5;
  if(w <= 15){w2 = 2;}
  if(w <= 10){w2 = 1;}*/
      }
      }
   else if(id >= 2 && id <= 3){
  let shottime = 50;
  let v = 1; 
  let vmax = 3; let color = 118.5+0.5*(-1)^id;
   let delay = 0;   let m = 3*(-1)^id;
  let w = 6; let w2 = 3;  let n = 0; 
  loop(28){
  let accel = v/60;  
  loop(3){
   let x = Obj_GetX(p); let y = Obj_GetY(p);
   let angle = Obj_GetAngle(p);
   let angle2 = angle+n;
  ascent(a in 0..2){
   BorderShot(x,y,v,angle+180*a,angle2,accel,vmax,color,delay,shottime);
  }
   PlaySE(se1);
   n += m;
     wait(w2);
      }  
  /* m += 3*(-1)^id;*/
   v += 0.2;
    wait(w);
/*   w -= 1.5;
  if(w <= 15){w2 = 2;}
  if(w <= 10){w2 = 1;}*/
      }
      }
  else if(id >= 4 && id <= 8){
  let shottime = 1;
  let v = 1; 
  let vmax = 3; let color = 118.5+0.5*(-1)^id;
   let delay = 0;   let m = 1*(-1)^id;
  let w = 0; let w2 = 1;  let n = 2*(-1)^id; 
  loop(36){
  let accel = v/60;  
  loop(15){
   let x = Obj_GetX(p); let y = Obj_GetY(p);
   let angle = Obj_GetAngle(p);
   let angle2 = angle+n;
  ascent(a in 0..1){
   BorderShot(x,y,v,angle,angle2,accel,vmax,color,delay,shottime);
  }
   PlaySE(se1);
   n += m/3;
     wait(w2);
      }  
  /* m += 3*(-1)^id;*/
   v += 0.18;
    wait(w);
/*   w -= 1.5;
  if(w <= 15){w2 = 2;}
  if(w <= 10){w2 = 1;}*/
      }
      }
  if(id == 9){
  let shottime = [22,21,20,19,18];
/*  let v = 1;  */  let v = 1+480*0.03;
   let color = 117;
   let delay = 0;   let m = 0;
  let w = 1; let w2 = 0; let n = 180;
  loop(480){
  let accel = v/60;
 let i = rand_int(0,4);
  ascent(a in 0..1){
   let x = Obj_GetX(p); let y = Obj_GetY(p);
   let angle = Obj_GetAngle(p);
   let angle2 = angle+n;
  let vmax = 3;
  n += m;
   BorderShot(x,y,v,angle,angle2,accel,vmax,color,delay,shottime[i]);
  wait(w);
      }  
  PlaySE(se1);
 /* shottime += 25/240; */
   m += 0;
   v -= 0.015;
  /*  wait(w);*/
 /*  w -= 1.25;
  if(w <= 15){w2 = 2;}
  if(w <= 10){w2 = 1;}*/
      }
      }
   Obj_Delete(p);
      }
  }

task BorderShot(x,y,v,angle,angle2,accel,vmax,color,delay,shottime){
        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);
         ObjShot_SetBombResist(p,true);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
   Speed;
   task Speed{
     while(!Obj_BeDeleted(p) && Obj_GetSpeed(p) > 0){
      Obj_SetSpeed(p,Obj_GetSpeed(p)-accel);
      wait(1);
      }
   Obj_SetSpeed(p,0);
   Obj_SetAngle(p,angle2);
    while(!Obj_BeDeleted(p) && GetTimer > shottime){
      wait(1);
      }
  if(shottime == 1){wait(45);}
   while(!Obj_BeDeleted(p) && Obj_GetSpeed(p) < vmax){
      Obj_SetSpeed(p,Obj_GetSpeed(p)+0.1);
      wait(1);
      }
    }
 /*  task Stop{
        while(!Obj_BeDeleted(p)){
 while(GetTimeOfPlayerInvincibility==0 && GetTimeOfSuperNaturalBorder==0){
    wait(1);
    }
  let v0 = Obj_GetSpeed(p);
   ObjShot_SetGraphic(p,128);
   while(GetTimeOfPlayerInvincibility!=0 || GetTimeOfSuperNaturalBorder!=0){
   Obj_SetSpeed(p,0);
    wait(1);
    } 
  Obj_SetSpeed(p,v0);
  ObjShot_SetGraphic(p,color);
  wait(1);
       }
     }   */
   }

 task BorderBreak{
   loop{
 while(GetTimeOfPlayerInvincibility==0 && GetTimeOfSuperNaturalBorder==0){
    wait(1);
    }
   let time0 = GetTimer;
   while(GetTimeOfPlayerInvincibility!=0 || GetTimeOfSuperNaturalBorder!=0){
 /*if(time0 >= 89 && time0 >= 88|| time0 == 77  || time0 == 73 || time0 == 63 || time0 == 60 || time0 == 51 || time0 == 23 || time0 == 15 || time0 == 2){SetTimer(time0);}*/
    if(GetTimer >= 88 && GetTimer <= 89){SetTimer(89);}
   else if(GetTimer >= 77 && GetTimer <= 78){SetTimer(79);}
   else if(GetTimer >= 73 && GetTimer <= 74){SetTimer(75);}
   else if(GetTimer >= 63 && GetTimer <= 64){SetTimer(65);}
    else if(GetTimer >= 60 && GetTimer <= 61){SetTimer(62);}
   else if(GetTimer >= 51 && GetTimer <= 52){SetTimer(53);}
    else if(GetTimer >= 47 && GetTimer <= 48){SetTimer(48);}
   else if(GetTimer >= 23 && GetTimer <= 24){SetTimer(25);}
   else if(GetTimer >= 15 && GetTimer <= 16){SetTimer(17);}
   else if(GetTimer >= 2 && GetTimer <= 3){SetTimer(4);}
   wait(1);
    } 
  wait(1);
     }
    }
   

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