
#Title[紫スペル2]
#Text[紫スペル2]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
    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_yukari.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot_add.txt";
   let se1 = csd~ "bgm\bell01.wav";
   let se2 = csd~ "bgm\eco02_a.wav";
   let se3 = csd~ "bgm\arrow01.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(1000);}
            else{SetLife(1000);}
           SetTimer(75);
     SetScore(6000000);
       SetDamageRate(20,12);
           SetInvincibility(120);
           SetGraphicRect(0*128,0,1*128,128);   
           SetMovePosition03(GetCenterX,GetClipMinY+120,20,5);
        SetAlpha(255);
      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{
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
        DeleteSE(se1);DeleteSE(se2);DeleteSE(se3);DeleteSE(se4);  
       if(!IsTimeOut){
                loop(125){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
              }
           }         
     }
 
     task TMain{
          yield;
      Circle(40);
  if(dif == 1){
    L(GetClipMinX,GetClipMinY,0,600,1,WHITE01);
    L(GetClipMaxX,GetClipMinY,90, 300,1,WHITE01);
      L(GetClipMinX,GetClipMinY,90,300,1,WHITE01);
   }
  else if(dif == 2){
     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);
    SetGraphicRect(0*128,2*128,1*128,3*128); 
        if(dif == 1){Shot; }
     else if(dif == 2){ Shot2;}
        Count;
    }

      task Shot{
    Bullet;  
   task Bullet{
   loop{
      let way = [24,36,48];
      let delay = 0;
      let accel = 0.01; let vmax = 5;
    ascent(i in 0..3){
       let x = GetX; let y = GetY;
     let color = rand_int(225,228);
       ascent(a in 0..way[i]){
        let v = rand(0.3,2);
        let angle = GetAngleToPlayer+30+360/way[i]*a;
        P(x,y,v,angle,color,delay,accel,vmax);
       }
       PlaySE(se1);
       wait(120);
        Move;
       wait(120);
       } 
      }
     }
       Bullet2;
       task Bullet2{
         let xlength = GetClipMaxX-GetClipMinX;
         loop{
          let angle = 90;
          let v = 2;
       ascent(b in 0..13){
          let x = GetX+(b-6)*xlength/12;
          let y = GetY+rand(-64,64);
       let color = rand_int(113,120);
          ascent(a in 0..10){
            let delay = a*4+4;
          CreateShot01(x,y,v,angle,color,delay);
            }
          wait(10);
          }
          }
        }
    }

 task Shot2{
  loop(4){
    ascent(d in 0..2){
   ascent(b in 0..3){
    let v2 = 1.5+2*b;
   ascent(a in 0..32){
   let angle = 360/32*a+180/32*d;
    CreateShotA(0,GetX,GetY,10); 
    SetShotDataA(0,0,1,angle,0,0,0,143);
    SetShotDataA(0,90,v2,angle,0,0,0,143);
    FireShot(0);
   }
   }
   PlaySE(se1);
   wait(10);
    }
  }
wait(180);
   Bullet;
  Bullet2;
   task Bullet{
  wait(180);
   let angle = 0; let color = 142;
   let delay = 20;
  loop{
  ascent(b in 0..3){
   let v = b+1;
  ascent(a in 0..2){
   let x = GetCenterX+80*(-1)^a; 
     let y = GetClipMinY+180;
    P2(x,y,v,angle,color,delay,0,0);
        }
     }
   angle += 20;
    PlaySE(se3);
   wait(10);
     }
    }
    task Bullet2{
   let color = 137;
   let delay = 10; let way = 3;
  loop{
  if(GetLife <= 400 || GetTimer <= 3){
   way = 5;
   }
  ascent(b in 0..3){
   let v = (b+1)/2;
  ascent(a in 0..2){
   let x = GetCenterX+40*(-1)^a; 
     let y = GetClipMinY+60;
  ascent(g in 0..way){
  let angle = atan2(GetPlayerY-y,GetPlayerX-x)+360/way*g;
    P2(x,y,v,angle,color,delay,0,0);
   }
        }
     }
    PlaySE(se3);
   wait(60);
     }
    }
     
   }

     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);
         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,vmax){
        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);
    Ref;
    task Ref{
   while(!Obj_BeDeleted(p)){
           let i = 0;
             loop(length(arrayL)){
             if(Collision_Obj_Obj(p,arrayL[i])){
          if(color == 225){color = 144;}
          else if(color == 226){color = 137;}
          else if(color == 227){color = 143;}
          else if(color == 228){color = 142;}
        CreateShot02(Obj_GetX(p),Obj_GetY(p),Obj_GetSpeed(p),atan2(GetPlayerY-Obj_GetY(p),GetPlayerX-Obj_GetX(p)),accel,vmax,color,10);    
             Obj_Delete(p); 
        wait(1);  
             break;
             }
         i++;
         if(i == 3){i = 0;}
         }
        wait(1);
        }
       }
         Del;
        task Del{
          while(!Obj_BeDeleted(p)){
            if(OnBomb){Obj_SetSpeed(p,0);}
             else{Obj_SetSpeed(p,v);}
             wait(1);
           }
          }
     }

 task P2(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);
           }
        }
    }

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