
#Title[早苗スペル1]
#Text[早苗スペル1]
#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_sanae.png";
   let wIni  = 60;
   let csd = GetCurrentScriptDirectory;
   let shotData = csd ~ "data_usershot.txt";
   let se1 = csd~ "bgm\arrow01.wav";
   let se2 = csd~ "bgm\tm2_swing002.wav";
   let se3 = csd~ "bgm\bell01.wav";
   let se4 = csd~ "bgm\countdown.wav";
    let effect = csd ~ "img\magic_circle\powercircle.png";
   let arrayL = [];
 let out = GetCommonData("TimeOut");
 let dif = GetCommonData("Dif");
  if(dif == 2){ name    = "奇跡　「蛙と蛇 -Lunatic-」";}
   @Initialize{
     if(!out){
         CutIn(KOUMA, name, "", 0, 0, 0, 0);
           if(GetPlayerType == REIMU_A){SetLife(800);}
            else{SetLife(800);}
           SetTimer(60);
        SetScore(6000000);
       SetDamageRate(20,15);
       MagicCircle(false);
     }
           SetInvincibility(120);
          SetGraphicRect(0*128,0*128,1*128,1*128);
           SetMovePosition03(GetCenterX,GetClipMinY+80,20,5);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
   SetEffectForZeroLife(1,0,0);
           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 && !out){
                loop(100){
              CreateItem(ITEM_SCORE,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
              }
          CreateItem(ITEM_1UP,GetX+offsetX(rand(0,64),rand(-180,180)),GetY+offsetY(rand(0,64),rand(-180,180)));
           }  
       DeleteCommonData("TimeOut");         
     }
 
     task TMain{
          yield;
      Circle(40);
       wait(120);
     SetGraphicRect(4*128,2*128,5*128,3*128);
        if(dif == 1){Shot; Shot2; }
     else if(dif == 2){Shot3; Shot4; }
        Count;
    }

      task Shot{
      let v = 5;
      let color = 68;
      let delay = 0;
      let accel = -0.05;
      let vmin = 0.5;
      loop{
       let angle = GetAngleToPlayer;
       let x = GetX;
       let y = GetY;
        ascent(b in 0..90){
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,v,angle+4*b,0,0,v,color);
         SetShotDataA(0,10,NULL,NULL,1*(-1)^b,accel,vmin,color);
         SetShotDataA(0,90,NULL,NULL,0.01*(-1)^b,accel,vmin,color);
         FireShot(0);
        } 
       PlaySE(se2);
         wait(60);
          }
    }

   task Shot3{
      let v = 5;
      let color = 68;
      let delay = 0;
      let accel = -0.05;
      let vmin = 1;
    let way = 120;
      loop{
       let angle = GetAngleToPlayer;
       let x = GetX;
       let y = GetY;
        ascent(b in 0..way){
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,v,angle+360/way*b,0,0,v,color);
         SetShotDataA(0,10,NULL,NULL,1*(-1)^b,accel,vmin,color);
         SetShotDataA(0,90,NULL,NULL,0.01*(-1)^b,accel,vmin,color);
         FireShot(0);
        } 
       PlaySE(se2);
         wait(60);
          }
    }

    task Shot2{
    wait(180);
      let way = 36;
      let v = 3;
      let color = 68;
      let delay = 0;
      let length = 32;
      let width = 8;
      let id = 0;
      let spin = 12;
      loop{
       let angle = GetAngleToPlayer;
       let x = GetX;
       let y = GetY;
        ascent(b in 0..way){
         Snake(x,y,v,angle+360/way*b,length,width,color,delay,0,spin);
         Snake(x,y,v,angle+360/way*b+5,length,width,color,delay,1,spin);
        } 
         wait(300);
          }
       }

   task Shot4{
    wait(180);
      let way = 36;
      let v = 3;
      let color = 68;
      let delay = 0;
      let length = 32;
      let width = 8;
      let id = 0;
      let spin = 12;
      loop{
   ascent(a in 0..2){
       let angle = GetAngleToPlayer+2.5*a;
       let x = GetX;
       let y = GetY;
        ascent(b in 0..way){
         Snake(x,y,v,angle+360/way*b,length,width,color,delay,0,spin);
         Snake(x,y,v,angle+360/way*b+5,length,width,color,delay,1,spin);
        } 
      wait(30);
      }
         wait(300);
          }
       }

     task Move{
        let x = rand(48,96);
        let y = rand(0,24);
        let v = 2;
        let xmin = GetClipMinX+100;
        let ymin = GetClipMinY+30;
        let xmax = GetClipMaxX-100;
        let ymax = GetClipMinY+120;
        if(x>=GetX){SetGraphicRect(4.5*64,2*64,5.5*64,4*64);}
       else if(x<=GetX){SetGraphicRect(4.5*64,2*64,5.5*64,4*64);}
         SetMovePositionRandom01(x,y,v,xmin,ymin,xmax,ymax);
    }

      task Count{
        loop{
          if(GetTimer <= 10){PlaySE(se4);}
         wait(60);
        }
    }
     
  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);
         Obj_SetAutoDelete(p,true);
         ObjShot_SetBombResist(p,true);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
        Angle;
       task Angle{
          loop(4){
            wait(30);
          Obj_SetAngle(p,Obj_GetAngle(p)+144);
            }
           wait(32);
            Obj_Delete(p);
         }
        Shot;
        task Shot{
           let v = 2;
           let accel = 0;
           while(!Obj_BeDeleted(p)){ 
         ascent(a in 0..78){
            let angle = Obj_GetAngle(p)+90;
           let x = Obj_GetX(p);
            let y = Obj_GetY(p); 
          let stop = 240-a*2;
       if(id == 0){
         ascent(b in 0..5){
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,0,angle+72*b,0,0,0,color);
         SetShotDataA(0,stop-60,10,NULL,0,-0.5,0,color);
         SetShotDataA(0,stop,0,NULL,rand(-180,180),0,0,color);
         SetShotDataA(0,stop+60,v,GetAngleToPlayer+72*b,0,accel,v,color);
         FireShot(0); 
        }
         PlaySE(se1);
           }
         else if(id == 1){
        angle -= 90;
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,0,angle,0,0,0,color);
         SetShotDataA(0,stop,v*2,angle,0,accel,v*2,color);
         FireShot(0); 
          }
            wait(2);
         DeleteEnemyShotImmediatelyInCircle(ALL,0,0,8);
             }
            }
          wait(30);
         PlaySE(se3);
          } 
  }

   task Snake(x,y,v,angle,length,width,color,delay,id,spin){
         let snake = Obj_Create(OBJ_SINUATE_LASER);
         Obj_SetX(snake,x);
         Obj_SetY(snake,y);
         Obj_SetSpeed(snake,v);
         Obj_SetAngle(snake,angle);
         Obj_SetAlpha(snake,255);
         ObjShot_SetBombResist(snake,true);
         ObjShot_SetDelay(snake,delay);
         ObjShot_SetGraphic(snake,color);
         ObjSinuateLaser_SetLength(snake,length);
         ObjSinuateLaser_SetWidth(snake,width);
       Angle;
        task Angle{
          if(id == 0){
           while(!Obj_BeDeleted(snake)){
            loop(10){
               Obj_SetAngle(snake,Obj_GetAngle(snake)+spin);
              wait(2);
               }
           loop(10){
               Obj_SetAngle(snake,Obj_GetAngle(snake)-spin);
              wait(2);
               }
           }
         }
           else if(id == 1){
         loop(2){
            loop(10){
               Obj_SetAngle(snake,Obj_GetAngle(snake)+spin);
              wait(2);
               }
           loop(10){
               Obj_SetAngle(snake,Obj_GetAngle(snake)-spin);
              wait(2);
           }
         }
           loop(60){
               Obj_SetAngle(snake,Obj_GetAngle(snake)-6);
              wait(1);
               }
            while(!Obj_BeDeleted(snake)){
            loop(10){
               Obj_SetAngle(snake,Obj_GetAngle(snake)+spin);
              wait(2);
               }
           loop(10){
               Obj_SetAngle(snake,Obj_GetAngle(snake)-spin);
              wait(2);
               }
            }
         }
        }  
          Color;
          task Color{
            let color1 = 68;
            let color2 = 67;
              while(!Obj_BeDeleted(snake)){
                ObjShot_SetGraphic(snake,color1);
                wait(1); 
                ObjShot_SetGraphic(snake,color2);
                wait(1);
                }
          }
        task Ref{
     while(!Obj_BeDeleted(snake)){
       if(Obj_GetX(snake) <= GetClipMinX ||
         Obj_GetX(snake) >= GetClipMaxX){
           Obj_SetAngle(snake,-Obj_GetAngle(snake));
           }
       if(Obj_GetY(snake) <= GetClipMinY){
           Obj_SetAngle(snake,180-Obj_GetAngle(snake));
           }
         wait(1);
           }
         }
     }

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

       task timestop(time){
          TimeStop(time,1,0,1);
          MotionBlurEx(ALL,time,200,ADD);
       loop(time/10){PlaySE(se3); wait(15);} 
     }
            
        function sc(r,g,b){
             SetShotColor(r,g,b);
     }

       sub setGraphicStop  { SetGraphicRect( 1, 1, 64, 64); }

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