
#Title[霊夢スペル5]
#Text[霊夢スペル5]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
  #include_script".\function_effect.txt"
    let name        = "大結界　「博霊拡散結界」";
     let csd = GetCurrentScriptDirectory;
        let gx = GetX;
        let gy = GetY;
        let gspeed = GetSpeed;
        let gangle = GetAngle;     
        let arg = GetArgument;
   let imgBOSS = csd~"img\character\dot_reimu.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\bell01.wav";
   let se2 = csd~ "bgm\arrow01.wav";
   let se3 = csd~ "bgm\tm2_laser000.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   = "大結界　「博霊森羅結界」";}
   @Initialize{
     /*  CutIn(KOUMA, name, "", 0, 0, 0, 0);*/
           if(GetPlayerType == REIMU_A){SetLife(2000);}
            else{SetLife(2000);}
      /*     SetTimer(90);
          SetScore(2000000);*/
       SetDamageRate(100,0);
         /*  SetInvincibility(120);*/
           SetGraphicRect(0*128,0,1*128,128);   
        /*   SetMovePosition03(GetCenterX,GetCenterY,20,5);*/
        SetAlpha(255);
      MagicCircle(false);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
        SetEffectForZeroLife(60,244,3);
           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){
                loop(500){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,200),rand(-180,180)),GetY-120+offsetY(rand(0,64),rand(-180,180)));
              }
           }         
     }
 
     task TMain{
          yield;
  /* let k = 80;
  SetShotAutoDeleteClip(k,k,k,k); */
      Circle(40);
      B;
       wait(90);
    CutIn(KOUMA, name, "", 0, 0, 0, 0);
    SetTimer(180);
     SetScore(100000000);
   PlaySE("seUseSpellCard.wav");
 SetMovePosition03(GetCenterX,GetCenterY,20,5);
     SetDamageRate(10,0);
   wait(120);
    SetGraphicRect(0*128,2*128,1*128,3*128); 
        if(dif == 1){Shot;}
    else if(dif == 2){Shot2;}
        Count;
      Pray(8);
   Bullet;
    }

      task Shot{
     let base = GetAngleToPlayer;
     let way = 3;
     let span = 5;
     let v = 3;
     loop{
     ascent(b in 0..2){
        ascent(a in 0..way){
       let x = GetX; let y = GetY;
        let angle = base+360/way*a;
       let color = 255; let delay = 60;
       let id = b;
         P(x,y,v,angle,color,delay,id,way,span);
         }
       wait(120);
       }
     if(GetLife <= 1600 || GetTimer <= 150){span = 4; v = 3;}
      if(GetLife <= 1250|| GetTimer <= 120){span = 3; v = 3;}
      if(GetLife <= 800|| GetTimer <= 90){way = 4; v = 3;}
      if(GetLife <= 550|| GetTimer <= 60){span = 2; v = 3; way = 4;}
     if(GetLife <= 300 || GetTimer <= 35){span = 2; way = 5; v = 3;}
     if(GetTimer <= 10){span = 1; v = 3; way = 5;}
       base += rand(60,300);
         }
    }

 task Shot2{
     let base = GetAngleToPlayer;
     let way = 3;
     let span = 5;
     let v = 3;
     loop{
     ascent(b in 0..2){
        ascent(a in 0..way){
       let x = GetX; let y = GetY;
        let angle = base+360/way*a;
       let color = 255; let delay = 60;
       let id = b;
         P(x,y,v,angle,color,delay,id,way,span);
         }
   wait(60);
       }
     if(GetLife <= 1600 || GetTimer <= 150){span = 4; v = 3;}
      if(GetLife <= 1250|| GetTimer <= 120){span = 3; v = 3;}
      if(GetLife <= 800|| GetTimer <= 90){way = 4; v = 3;}
      if(GetLife <= 550|| GetTimer <= 60){span = 2; v = 3;}
     if(GetLife <= 300 || GetTimer <= 35){span = 2; way = 5; v = 3;}
     if(GetTimer <= 10){span = 1; v = 3;}
       base += rand(60,300);
         }
    }

  task P(x,y,v,angle,color,delay,id,way,span){
        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);
         Obj_SetAutoDelete(p,true);
       Set;
       task Set{
         function set_xy(radius,v){
             Obj_SetAngle(p,Obj_GetAngle(p)+v);   
             Obj_SetX(p,GetX+offsetX(radius,Obj_GetAngle(p)));
             Obj_SetY(p,GetY+offsetY(radius,Obj_GetAngle(p)));
            }
          while(!Obj_BeDeleted(p)){
          ascent(a in 0..301){
              set_xy(a*v,3*(-1)^id);
            wait(1);
             }
           Obj_Delete(p);
            }
          }
        Laser;
        task Laser{
     while(!Obj_BeDeleted(p)){
       ascent(a in 0..2){
     let radius = ((GetX-Obj_GetX(p))^2+(GetY-Obj_GetY(p))^2)^0.5;
     let x = Obj_GetX(p); let y = Obj_GetY(p);
      let width = 5; let color = 192;
     /* let delay = 240;*/
        let angle = Obj_GetAngle(p)+(360-180*(way-2)/way)/2+180*(way-2)/way*a;
       let length = (2*radius^2-2*radius^2*cos(360/way*a))^0.5;
      L(x,y,angle,length,width,color,delay);
           }
         wait(span);
          }
        }
     }

   task L(x,y,angle,length,width,color,delay){
              let l = Obj_Create(OBJ_LASER);
              arrayL = arrayL ~ [l];
              Obj_SetX(l,x);
              Obj_SetY(l,y);
              Obj_SetAngle(l,angle);
              ObjShot_SetGraphic(l,color);
              ObjShot_SetDelay(l,delay);
              Obj_SetCollisionToObject(l,true);
              Obj_SetCollisionToPlayer(l,true);
              ObjLaser_SetLength(l,length); 
              ObjLaser_SetWidth(l,width);
              ObjLaser_SetSource(l,false);
           Obj_SetAutoDelete(l,true);
       wait(delay);
      PlaySE(se3);
          wait(delay*0.5);
         Obj_Delete(l);
     }

    task Bullet{
         Bullet_A;
         task Bullet_A{
  let n = 3; if(dif == 2){n = 6;}
          loop{
            if(GetLife <= 1500 || GetTimer<=150){
              let r = rand(-180,180);
          let x = GetX; let y = GetY;
          let v = 3; let color = 129; let delay = 10;
        loop(n){
          ascent(a in 0..24){
             let angle = r+15*a;
               CreateShot01(x,y,v,angle,color,delay);
                 }
           wait(5);
                }
               }
            if(GetLife > 1000 && GetTimer > 60){wait(60);}
               wait(60);
                 }
             }
  
          Bullet_B;
         task Bullet_B{
  let n = 3; if(dif == 2){n = 5;}
              loop{
  let r = rand(-180,180);
            if(GetLife <= 500 || GetTimer<=40){
          loop(n){
            /*  let r = rand(-180,180);*/
          let x = GetX; let y = GetY;
          let v = 1.5; let color = 136; let delay = 10;
    if(dif == 2){v = 2;}
         ascent(a in 0..20){
            ascent(b in 0..3){
               let angle = r + 18*a + (b-2)*3;
            CreateShot01(x,y,v,angle,color,delay);
                  }
              }
          wait(30);
              }
           }
       wait(150);
           }
        }

        Bullet_C;
        task Bullet_C{
           while(GetLife >= 1200 && GetTimer >= 60){
             wait(1);
              }
          loop{
             let x =  GetX + rand(64,256)*(-1)^rand_int(0,3);
             let y = GetClipMinY+rand(-32,192);
            let v = rand(5,10);
            let angle = 90; let accel = -0.1;
             let color = rand_int(243,246); let delay = 15;
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,0,angle,0,accel,0,color);
         SetShotDataA(0,20,v,angle,0,accel,rand(1,4),color);
         FireShot(0);
         PlaySE(se1);
           wait(1);
           }
        }
      }

     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 = 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);
         loop(90){
           CreateShot01(GetX,GetY,rand(0.5,3),rand(-180,180),9,10);
           wait(1);
        }
         SetGraphicRect(0*128,2*128,1*128,3*128); 
         wait(90);
    }
       
     task B{
     let frame = 10;
     let pattern = 6;
     let radius = 64;
     let accAngle = 6;
     let blur = 6;
     let graphic = 14;
      loop{
     BombBarrier(frame,pattern,radius,accAngle,blur,graphic );
        wait(1);
        }
       }      

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

     task Pray(n){
    loop{
       SetGraphicRect(6*128,0*128,7*128,1*128); 
       wait(n);
       SetGraphicRect(7*128,0*128,8*128,1*128); 
       wait(n);
       SetGraphicRect(6*128,1*128,7*128,2*128); 
       wait(n);
       SetGraphicRect(7*128,1*128,8*128,2*128); 
       wait(n);
       SetGraphicRect(6*128,2*128,7*128,3*128); 
       wait(n);
       SetGraphicRect(7*128,2*128,8*128,3*128); 
        wait(n);
       SetGraphicRect(6*128,3*128,7*128,4*128); 
       wait(n);
       SetGraphicRect(7*128,3*128,8*128,4*128); 
       wait(n);
       }
     }

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

     function enemy(text,x,y,speed,angle,arg){
               CreateEnemyFromFile(text,x,y,speed,angle,arg);
            }
}