#東方弾幕風
#Title[正体不明「未確認幻想飛行物体X」]
#Text[飛鉢+遊星+星剣]
#Player[FREE]
#Image[.\img\正体不明「未確認幻想飛行物体X」.png]
#BackGround[User]
#ScriptVersion[2]

script_enemy_main{
    #include_script".\Local.txt"
   let name        = " 正体不明「未確認幻想飛行物体Ｘ」";
   let imgBOSS = csd~"img\dot_nue.png";
   let shotData = csd ~ "data_usershot.txt";
  let effect = csd ~ "img\circle.png";
  let distance = [];  
  let radian = [];  

   @Initialize{
     CutIn(KOUMA, name, "", 0, 0, 0, 0);
 SetScore(123456789/3);
 BombBarrierOnSpell(100,0);
           SetLife(20000);
           SetTimer(180);
           SetInvincibility(120);
         sgr(0,0);
   SetX(cx); SetY(miny+120);
        SetAlpha(255);
           LoadGraphic(imgBOSS);
    LoadGraphic(effect);
          LoadUserShotData(shotData);
           TMain;
    }

   @MainLoop{
    if(OnEvent){break;}
      SetCollisionA(GetX, GetY, 24);
      SetCollisionB(GetX, GetY, 18);

         yield;
       }

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

    @Finalize{
 PlaySE(sevanish);
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
     }
 
     task TMain{
 Circle2(48,0);
 BombBarrierEffect;
 SpellBackGround(150);
          wait(90);
 SetEnemyMarker(true);
    sgr(4,2);
        Shot;
        Count;
    }

   task Shot{
 let x = GetX; let y = GetY;
 let w = 300; let spin = 0.3;
 let v = 0.7; let way = 24;
 let length = 7; let dp = 0.07*5/3;
 let dx = [-120,-45,45,120];
 let dy = [50,-60,-60,50];
 let i = 0;  let color = [134,132,135,129];
 loop{
  let vector = 90;
 if(GetLife <= 15000 || GetTimer <= 120){break;}
   loop(2){
  if(GetLife <= 15000 || GetTimer <= 120){break;}
  ascent(a in 0..4){
  Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[3],way);
   }
   PlaySE(seshot4);
  spin = -spin;
    wait(w);
    if(GetLife <= 15000 || GetTimer <= 120){break;}
         }
   if(GetLife <= 15000 || GetTimer <= 120){break;}
    ascent(a in 0..4){
  let vector = atan2(GetPlayerY-y-dy[a],GetPlayerX-x-dx[a]);
  Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[3],way);
   }
   PlaySE(seshot4);
  spin = -spin;
 wait(w);
   if(GetLife <= 15000 || GetTimer <= 120){break;}
      }
  
 let i = rand_int(0,3);
  loop{
  let vector = 90;
  if(GetLife <= 10000 || GetTimer <= 90){break;}
   loop(2){
  if(GetLife <= 10000 || GetTimer <= 90){break;}
  ascent(a in 0..4){
 if(i == 3){Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way);}
 if(i == 0){No(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],length,dp);}
 if(i == 1){Tri(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
 if(i == 2){Squ(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
   }
   PlaySE(seshot4);
  spin = -spin;
  i++;
 if(i > 3){i = 0;}
    wait(w);
    if(GetLife <= 10000 || GetTimer <= 90){break;}
         }
   if(GetLife <= 10000 || GetTimer <= 90){break;}
    ascent(a in 0..4){
  let vector = atan2(GetPlayerY-y-dy[a],GetPlayerX-x-dx[a]);
 if(i == 3){Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way);}
 if(i == 0){No(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],length,dp);}
 if(i == 1){Tri(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
 if(i == 2){Squ(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
   }
   PlaySE(seshot4);
  spin = -spin;
   i++;
  if(i > 3){i = 0;}
 wait(w);
   if(GetLife <= 10000 || GetTimer <= 90){break;}
      }

 w = 240;
  loop{
  let vector = 90;
  if(GetLife <= 5000 || GetTimer <= 34){break;}
   loop(2){
  let i = rand_int(0,3);
  if(GetLife <= 5000 || GetTimer <= 34){break;}
  ascent(a in 0..4){
 if(i == 3){Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way);}
 if(i == 0){No(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],length,dp);}
 if(i == 1){Tri(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
 if(i == 2){Squ(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
  i++;
  if(i > 3){i = 0;}
   }
   PlaySE(seshot4);
  spin = -spin;
    wait(w);
    if(GetLife <= 5000 || GetTimer <= 34){break;}
         }
   if(GetLife <= 5000 || GetTimer <= 34){break;}
 let i = rand_int(0,3);
    ascent(a in 0..4){
  let vector = atan2(GetPlayerY-y-dy[a],GetPlayerX-x-dx[a]);
 if(i == 3){Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way);}
 if(i == 0){No(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],length,dp);}
 if(i == 1){Tri(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
 if(i == 2){Squ(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
  i++;
  if(i > 3){i = 0;}
   }
   PlaySE(seshot4);
  spin = -spin;
 wait(w);
   if(GetLife <= 5000 || GetTimer <= 34){break;}
      }

  w = 180;
  way = 24;
  color = [174,172,175,169];
  loop{
  let vector = 90;
   loop(2){
  let i = rand_int(0,3);
  ascent(a in 0..4){
 if(i == 3){Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way);}
 if(i == 0){No(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],length,dp);}
 if(i == 1){Tri(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
 if(i == 2){Squ(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
  i++;
  if(i > 3){i = 0;}
   }
   PlaySE(seshot4);
  spin = -spin;
    wait(w);
         }
 let i = rand_int(0,3);
    ascent(a in 0..4){
  let vector = atan2(GetPlayerY-y-dy[a],GetPlayerX-x-dx[a]);
 if(i == 3){Yes(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way);}
 if(i == 0){No(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],length,dp);}
 if(i == 1){Tri(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
 if(i == 2){Squ(x+dx[a],y+dy[a],v,vector,spin*(-1)^a,color[i],way); }
  i++;
  if(i > 3){i = 0;}
   }
   PlaySE(seshot4);
  spin = -spin;
 wait(w);
      }
    }

  /* 
  Yes(x,y,v,vector,spin,color,way);
  No(x,y,v,vector,spin,color,length,dp);
  Tri(x,y,v,vector,spin,color,way); 
  Squ(x,y,v,vector,spin,color,way); 
  Polygon(x,y,v,vector,spin,color,6,way); 
  */


//広がる円
  function Yes(x,y,v,vector,spin,color,way){
  let base = rand(-180,180);
  let delay = 0;
  let expand = 1;
   let maxpand = 300;
    ascent(a in 0..way){
   let angle = a*360/way+base;
  P(x,y,v,vector,angle,spin,expand,maxpand,color,delay);
      }
    }

//広がるバツ
 function No(x,y,v,vector,spin,color,length,dp){
  let base = rand(-180,180);
  let delay = 0;
   ascent(b in 0..length){
   let expand = dp*b;
  let maxpand = 300/0.7*expand;
    ascent(a in 0..4){
   let angle = a*90+base;
  P(x,y,v,vector,angle,spin,expand,maxpand,color,delay);
      }
      }
    }

//広がる三角
 function Tri(x,y,v,vector,spin,color,way){
 long(3,way/3,300);
  let base = rand(-180,180);
  let delay = 0;
 ascent(b in 0..length(distance)){
    ascent(a in 0..3){
   let angle = a*120+base+radian[b];
   let expand = 0.7*distance[b]/300;
   let maxpand = 300/0.7*expand;
  P(x,y,v,vector,angle,spin,expand,maxpand,color,delay);
      }
      }
   }

 //広がる四角
 function Squ(x,y,v,vector,spin,color,way){
 long(4,way/4,300);
  let base = rand(-180,180);
  let delay = 0;
 ascent(b in 0..length(distance)){
    ascent(a in 0..4){
   let angle = a*90+base+radian[b];
   let expand = 0.7*distance[b]/300;
   let maxpand = 300/0.7*expand;
  P(x,y,v,vector,angle,spin,expand,maxpand,color,delay);
      }
      }
   }

 //広がる多角形
 function Polygon(x,y,v,vector,spin,color,n,way){
 long(truncate(n),way/truncate(n),300);
  let base = rand(-180,180);
  let delay = 0;
 ascent(b in 0..length(distance)){
    ascent(a in 0..truncate(n)){
   let angle = a*360/truncate(n)+base+radian[b];
   let expand = 0.7*distance[b]/300;
   let maxpand = 300/0.7*expand;
  P(x,y,v,vector,angle,spin,expand,maxpand,color,delay);
      }
      }
   }

  task P(x,y,v,vector,angle,spin,expand,maxpand,color,delay){
        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(delay);
 Del;
  let radius = 0;
     Set;
   task Set{
    while(!Obj_BeDeleted(p)){ 
    Obj_SetX(p,x+offsetX(radius,angle));
    Obj_SetY(p,y+offsetY(radius,angle));
    Obj_SetAngle(p,angle+90*spin);
    angle += spin;
    if(radius <= maxpand){radius += expand;}
    else{ObjShot_SetBombResist(p,false);}
    wait(1); 
    x += v*cos(vector);
    y += v*sin(vector);
    if(x < minx-320 || x > maxx+320 || y < miny-320 || y > maxy+20+maxpand){
      Obj_Delete(p);  break; 
        }
        }
      }
    task Del{
    while(!Obj_BeDeleted(p)){
    if(OnPlayerMissed && (Obj_GetX(p)-GetPlayerX)^2+(Obj_GetY(p)-GetPlayerY)^2 <= 100^2){wait(30); ObjShot_SetDelay(p,20); break;}
     wait(1);
       }
    wait(15);
      Obj_Delete(p); 
     }
   }

  function long(i,n,max){
 distance = [];
 radian = [];
 let width = 10;
 let color = 247;
  let delay = 0;
 let angle = 360/i;
  let length = (2*max^2*(1-cos(360/i)))^0.5;
 let x = offsetX(max,angle); 
 let y = offsetY(max,angle); 
 L(x,y,180-90*(i-2)/i,length,width,color,delay);
  return distance;
  return radian;
  task L(x,y,angle,length,width,color,delay){
              let l = Obj_Create(OBJ_LASER);
              Obj_SetX(l,x);
              Obj_SetY(l,y);
              Obj_SetAngle(l,angle);
              Obj_SetAutoDelete(l,false);
              ObjShot_SetGraphic(l,color);
              ObjShot_SetDelay(l,delay);
              Obj_SetCollisionToObject(l,true);
              Obj_SetCollisionToPlayer(l,false);
              ObjLaser_SetLength(l,length);
              ObjLaser_SetWidth(l,width);
              ObjLaser_SetSource(l,false);
      ascent(a in -n..0){
      let d = ((Obj_GetX(l)+(ObjLaser_GetEndX(l)-Obj_GetX(l))/n*a)^2+(Obj_GetY(l)+(ObjLaser_GetEndY(l)-Obj_GetY(l))/n*a)^2)^0.5;
      let r = atan2(Obj_GetY(l)+(ObjLaser_GetEndY(l)-Obj_GetY(l))/n*a,Obj_GetX(l)+(ObjLaser_GetEndX(l)-Obj_GetX(l))/n*a);
      distance = distance ~ [d];
      radian = radian ~ [r];
        }
    Obj_Delete(l);
     }
   }

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

  function sgr(a,b){SetGraphicRect(a*128,b*128,(a+1)*128,(b+1)*128);}

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