
#Title[cʏe1]
#Text[cʏe1]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
     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\eco02_a.wav";
   let se3 = csd~ "bgm\bell01.wav";
   let se4 = csd~ "bgm\countdown.wav";
    let effect = csd ~ "img\magic_circle\powercircle.png";
   let arrayL = [];
  let dif = GetCommonData("Dif");
   @Initialize{
           if(GetPlayerType == REIMU_A){SetLife(2500);}
            else{SetLife(2500);}
           SetTimer(60);
       SetDamageRate(80,50);
           SetInvincibility(120);
           SetGraphicRect(0*128,0*128,1*128,1*128);   
           SetMovePosition03(GetCenterX,GetClipMinY+120,20,5);
           LoadGraphic(imgBOSS);
          LoadGraphic(effect);
          LoadUserShotData(shotData);
         LoadSE(se1);LoadSE(se2);LoadSE(se3);LoadSE(se4);
           TMain;
    }

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

         yield;
       }

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

    @Finalize{
  SetCommonData("TimeOut",IsTimeOut);
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
        DeleteSE(se1);DeleteSE(se2);DeleteSE(se3);DeleteSE(se4);           
     }
 
     task TMain{
          yield;
      Circle(40);
       wait(120);
        Shot;  
       Shot2;  
        Count;
    }

      task Shot{
      let base =-162;
      let radius = 64;
       let angle = base+162;
       let v = 6;
    if(dif == 2){
         radius = 96;
     v = 9;
     }
       let x = GetX+offsetX(radius,base);
        let y = GetY+offsetY(radius,base);
       let color = 46;
        let delay = 20;
       let id = 0;
        P(x,y,v,angle,color,0,id);
         PlaySE(se1);
     SetGraphicRect(4*128,2*128,5*128,3*128);
       wait(120);
      Move;
      wait(60);
       loop{
         let base =rand(-180,180);
        SetGraphicRect(4*128,2*128,5*128,3*128);
        loop(2){
         let angle = base+162;
        let x = GetX+offsetX(radius,base);
        let y = GetY+offsetY(radius,base);
        P(x,y,v,angle,color,0,id);
          PlaySE(se1);
         base += 108;
        }
       wait(120);
        Move;
      wait(60);
     }
    }

    task Shot2{
     let wayarray = [6,7,8];
     let i = 0;
      loop{
     let way = wayarray[i];
    /*  let base =rand(-180,180);*/
      let base =GetAngleToPlayer;
    loop(way){
      let radius = 144;
    if(dif == 2){radius = 180;}
       let angle = base+162;
       let v = 6;
     let xbase = rand(GetClipMinX+32,GetClipMaxX-32);
     let ybase = rand(GetClipMinY+64,GetCenterY);
    /*   let x = xbase+offsetX(radius,base);
        let y = ybase+offsetY(radius,base);  */
     let x = GetX+offsetX(radius,base);
     let y = GetY+offsetY(radius,base);
       let color = 25;
        let delay = 20;
       let id = 1;
        P(x,y,v,angle,color,0,id); 
        base += 360/way;
       }
        i += 1;
      if(i > 2){i = 0;}
        wait(180);
          }
       }

     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;
        SetGraphicRect(2*128,1*128,3*128,2*128);
         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(20);
          Obj_SetAngle(p,Obj_GetAngle(p)+144);
            }
           wait(20);
            Obj_Delete(p);
         }
        Shot;
        task Shot{
           let v = 2;
           let accel = 0;
           while(!Obj_BeDeleted(p)){ 
         ascent(a in 0..52*2){
            let angle = Obj_GetAngle(p)+90;
           let x = Obj_GetX(p);
            let y = Obj_GetY(p); 
          let stop = 224-a*1;
       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.3,1.5,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;
       loop(2){
         CreateShotA(0,x,y,delay); 
         SetShotDataA(0,0,0,angle,0,0,0,color);
         SetShotDataA(0,stop-60,v*2,angle,0,accel,v*2,color);
         FireShot(0); 
          angle += 180;
          }
          }
            wait(1);
         DeleteEnemyShotImmediatelyInCircle(ALL,0,0,8);
             }
            }
       if(id == 0){
          wait(60);
         PlaySE(se3);
           }
          } 
  }

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