
#Title[霊夢スペル3]
#Text[霊夢スペル3]
#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_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 arrayL2 = [];
 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(90);
     SetScore(15000000);
       SetDamageRate(15,3);
           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);
           TMain;
    }

   @MainLoop{
           SetCollisionA(GetX, GetY, 24);
           SetCollisionB(GetX, GetY, 24);

         yield;
       }

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

    @Finalize{
  SetShotAutoDeleteClip(64,64,64,64);
        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;
   SetShotAutoDeleteClip(150,100,150,100);
      Circle(40);
       wait(120);
    SetGraphicRect(0*128,2*128,1*128,3*128); 
        if(dif == 1){Shot; }
    else if(dif == 2){ShotLuna;}
        Count;
    }

      task Shot{
    ascent(b in 0..2){
      ascent(a in 0..4){
      let length = 150+60+(150+60)*b;
      let width = 10;
      let color = 185;
      let delay = 120;
      let angle = 90*a;
       let x = GetX+offsetX(length*2^0.5/2-2.5,angle-135);
       let y = GetY+offsetY(length*2^0.5/2-2.5,angle-135);
        L(x,y,angle,length,width,color,delay,0);
        }
       }
       pray(15,1);
     SetDamageRate(15,3);
       wait(120);

   Shot2;
   task Shot2{
     let way = 4;
       let angle = GetAngleToPlayer+180/way+90/way+30;
      loop(60){
        ascent(a in 0..way){
       let x = GetX; let y = GetY;
        let v = 3; 
        let color = 113; let delay =30;
      let angle2 = angle + 360/way*a;
         P(x,y,v,angle2,color,delay,0);
   /*    PlaySE(se2); */
        }
      wait(3);
      }
     let v = 3;
       let color = 113;
       let delay = 30;     
       let n = 2;
       let spin = 0;
     loop{
         ascent(a in 0..way){
          let x = GetX;
          let y = GetY;
          let angle2 = angle + 360/way*a;
       ascent(c in 0..1){
          P(x,y,v-c,angle2-c,color,delay,0);
      }
          }
          angle += n;
      /*  n += spin; */
       /*  PlaySE(se2); */
          wait(3);
         }
      }

  Shot3;
   task Shot3{
     let way = 4;
       let angle = GetAngleToPlayer+180/way-90/way+30;
      loop(60){
        ascent(a in 0..way){
       let x = GetX; let y = GetY;
        let v = 3; 
        let color = 113+7; let delay =30;
      let angle2 = angle + 360/way*a;
         P(x,y,v,angle2,color,delay,0);
   /*    PlaySE(se2); */
        }
      wait(3);
      }
     let v = 3;
       let color = 113+7;
       let delay = 30;     
       let n = -2;
       let spin = 0;
     loop{
         ascent(a in 0..way){
          let x = GetX;
          let y = GetY;
          let angle2 = angle + 360/way*a;
         ascent(c in 0..1){
          P(x,y,v-c,angle2-c,color,delay,0);
      }
          }
          angle += n;
      /*  n += spin; */
       /*  PlaySE(se2); */
          wait(3);
         }
      }

  /* Shot4;*/
  task Shot4{
   wait(120);
   let v = 1.5; let color = 33;
   let way = 1; let delay = 60;
   let id = -2; let radius = 0;
  loop{
    ascent(a in 0..way){
    let angle = GetAngleToPlayer+360/way*a;
   let x = GetX+offsetX(radius,angle);
   let y = GetY+offsetY(radius,angle);
     CreateShot01(x,y,v,angle,color,delay);
          }
       wait(60);
         }
      } 
    }

 task ShotLuna{
    ascent(b in 0..2){
      ascent(a in 0..4){
      let length = 150+60+(150+60)*b;
   let length2 = 150+150*b;
      let width = 10;
      let color = 185;
      let delay = 120;
      let angle = 90*a;
       let x = GetX+offsetX(length*2^0.5/2-2.5,angle-135);
       let y = GetY+offsetY(length*2^0.5/2-2.5,angle-135);
    let x2 = GetX+offsetX(length2*2^0.5/2-2.5,angle-135);
       let y2 = GetY+offsetY(length2*2^0.5/2-2.5,angle-135);
        L(x,y,angle,length,width,color,delay,0);
   L2(x2,y2,angle,length2,width,color+7,delay,0);
        }
       }
       pray(15,1);
     SetDamageRate(15,3);
       wait(120);

   Shot2;
   task Shot2{
     let way = 4;
       let angle = GetAngleToPlayer+180/way+90/way+30;
      loop(60){
        ascent(a in 0..way){
       let x = GetX; let y = GetY;
        let v = 3; 
        let color = 113; let delay =30;
      let angle2 = angle + 360/way*a;
         P(x,y,v,angle2,color,delay,0);
   /*    PlaySE(se2); */
        }
      wait(3);
      }
     let v = 3;
       let color = 113;
       let delay = 30;     
       let n = 2;
       let spin = 0;
     loop{
         ascent(a in 0..way){
          let x = GetX;
          let y = GetY;
          let angle2 = angle + 360/way*a;
          P(x,y,v,angle2,color,delay,0);
          }
          angle += n;
      /*  n += spin; */
       /*  PlaySE(se2); */
          wait(3);
         }
      }

  Shot3;
   task Shot3{
     let way = 4;
       let angle = GetAngleToPlayer+180/way-90/way+30;
      loop(60){
        ascent(a in 0..way){
       let x = GetX; let y = GetY;
        let v = 3; 
        let color = 113+7; let delay =30;
      let angle2 = angle + 360/way*a;
         P2(x,y,v,angle2,color,delay,0);
   /*    PlaySE(se2); */
        }
      wait(3);
      }
     let v = 3;
       let color = 113+7;
       let delay = 30;     
       let n = -2;
       let spin = 0;
     loop{
         ascent(a in 0..way){
          let x = GetX;
          let y = GetY;
          let angle2 = angle + 360/way*a;
          P2(x,y,v,angle2,color,delay,0);
          }
          angle += n;
      /*  n += spin; */
       /*  PlaySE(se2); */
          wait(3);
         }
      }

 /*  Shot4;*/
  task Shot4{
   wait(120);
   let v = 1.5; let color = 134;
   let way = 36; let delay = 60;
   let id = -2; let radius = -310;
  loop{
    ascent(a in 0..way){
    let angle = GetAngleToPlayer+360/way*a+180/way;
   let x = GetX+offsetX(radius,angle);
   let y = GetY+offsetY(radius,angle);
     P(x,y,v,angle,color,delay,id);
          }
       wait(360);
         }
      } 
    }

  task P(x,y,v,angle,color,delay,id){
        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);
         Obj_SetAutoDelete(p,true);
       Warp;
       task Warp{
          while(!Obj_BeDeleted(p)){
           let i = 0;
             loop(length(arrayL)){
             if(Collision_Obj_Obj(p,arrayL[i])){
       let radius = ((Obj_GetX(p)-GetX)^2+(Obj_GetY(p)-GetY)^2)^0.5+10*(-1)^id;
     
   if(id == 1 || id == -2){
      Obj_SetAngle(p,Obj_GetAngle(p)+180);
       } 
    if(id >=0){
           Obj_SetX(p,GetX+offsetX(2*radius,Obj_GetAngle(p)));
           Obj_SetY(p,GetY+offsetY(2*radius,Obj_GetAngle(p)));
      }
    if(id < 0){
       Obj_SetX(p,GetX+offsetX(1/2*radius,Obj_GetAngle(p)));
       Obj_SetY(p,GetY+offsetY(1/2*radius,Obj_GetAngle(p)));
      }
 if(id == 0 || id == -1){
      Obj_SetAngle(p,Obj_GetAngle(p)+180);
       }
   id += 1;
       PlaySE(se1);
         /*    Obj_Delete(p);*/ 
        wait(30/v);  
             break;
             }
         i++;
         if(i == 8){i = 0;}
         }
        wait(1);
        }
        }
     }

   task L(x,y,angle,length,width,color,delay,id){
              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,false);
              ObjLaser_SetLength(l,length); 
              ObjLaser_SetWidth(l,width);
              ObjLaser_SetSource(l,true);
          wait(delay);
     PlaySE(se3);
         ObjLaser_SetSource(l,false);
     }

 task P2(x,y,v,angle,color,delay,id){
        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);
         Obj_SetAutoDelete(p,true);
       Warp;
       task Warp{
          while(!Obj_BeDeleted(p)){
           let i = 0;
             loop(length(arrayL2)){
             if(Collision_Obj_Obj(p,arrayL2[i])){
       let radius = ((Obj_GetX(p)-GetX)^2+(Obj_GetY(p)-GetY)^2)^0.5+10*(-1)^id;
     
   if(id == 1 || id == -2){
      Obj_SetAngle(p,Obj_GetAngle(p)+180);
       } 
    if(id >=0){
           Obj_SetX(p,GetX+offsetX(2*radius,Obj_GetAngle(p)));
           Obj_SetY(p,GetY+offsetY(2*radius,Obj_GetAngle(p)));
      }
    if(id < 0){
       Obj_SetX(p,GetX+offsetX(1/2*radius,Obj_GetAngle(p)));
       Obj_SetY(p,GetY+offsetY(1/2*radius,Obj_GetAngle(p)));
      }
 if(id == 0 || id == -1){
      Obj_SetAngle(p,Obj_GetAngle(p)+180);
       }
   id += 1;
       PlaySE(se1);
         /*    Obj_Delete(p);*/ 
        wait(30/v);  
             break;
             }
         i++;
         if(i == 8){i = 0;}
         }
        wait(1);
        }
        }
     }

   task L2(x,y,angle,length,width,color,delay,id){
              let l = Obj_Create(OBJ_LASER);
              arrayL2 = arrayL2 ~ [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,false);
              ObjLaser_SetLength(l,length); 
              ObjLaser_SetWidth(l,width);
              ObjLaser_SetSource(l,true);
          wait(delay);
     PlaySE(se3);
         ObjLaser_SetSource(l,false);
     }

     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 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 pray(m ,n){
    loop(m){
       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);
            }
}