
#Title[剣符　「夢幻の剣」]
#Text[夢月・スペル2]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
    #include_script".\Local.txt"
   let name        = " 剣符　「夢幻の剣」";
   let imgBOSS = csd~"img\dot_mugetu.png";
   let shotData = csd ~ "data_usershot.txt";
  let effect = csd ~ "img\circle.png"; 

   @Initialize{
  SetCommonDataEx("","bg",4);
     CutIn(KOUMA, name, "", 0, 0, 0, 0);
     SetScore(2000000);
           SetLife(500);
      SetDamageRate(20,0);
           SetTimer(54);
           SetInvincibility(120);
         sgr(0,0);
         SetMovePosition03(GetCenterX,GetClipMinY+120,20,2);
        SetAlpha(255);
           LoadGraphic(imgBOSS);
    LoadGraphic(effect);
          LoadUserShotData(shotData);
    Circle2(60);
           TMain;
    }

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

         yield;
       }

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

    @Finalize{
   SetCommonDataEx("","bg",3);
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
  loop(48){
     CreateItem(ITEM_SCORE,rand(minx,maxx),miny+rand(16,150)); 
       }   
     }
 
     task TMain{
          wait(90);
   SetShotAutoDeleteClip(64,64,64,64);
     /*   Shot;  */
  Shot2;
        Count;
    loop{
   if(OnBomb){
   SetAlpha(0);
   SetDamageRate(0,0);}
     else{
    SetAlpha(255);
   SetDamageRate(20,0);}
    wait(1);
      }
    }
/* task Shot{
  loop{
   Shot1(36);
  Move(0);
  wait(180);
   Move(1);
   Shot2(36);
    Move(2);
   wait(90);
   Move(3);
   Shot2(36);
   Move(4);
   wait(120);
  Shot3(36);
  wait(180);
   }
   function Shot1(way){
  ascent(a in 0..way){
  let x = GetX; let y = GetY;
   let v = 6; 
  let color = 174; let delay = 10;
   let angle = -120+240/way*a;
  Sward(x,y,v,angle,color,delay);
   PlaySE(seshot1);
   wait(2);
        }
     }
   function Shot2(way){
  ascent(a in 0..way){
  let x = GetX; let y = GetY;
   let v = 6; 
  let color = 174; let delay = 10;
   let angle = 90;
  Sward(x,y,v,angle,color,delay);
   PlaySE(seshot1);
   wait(2);
        }
     }
    function Shot3(way){
  ascent(a in 0..way){
  let x = GetX; let y = GetY;
   let v = 6; 
  let color = 174; let delay = 10;
   let angle = -60-240/way*a;
  Sward(x,y,v,angle,color,delay);
   PlaySE(seshot1);
   wait(2);
        }
     }
    }*/

  task Shot2{
  loop{
   let x = GetX; let y = GetY;
  let v = 4; let color = 174;
  let delay = 20;
   ascent(a in 0..18){
  let angle = GetAngleToPlayer+20*a;
   Sward(x,y,v,angle,color,delay);
         }
   PlaySE(seshot1);
     wait(180);
    Move;
    wait(120);
  loop(3){
    ascent(b in 0..5){
       ascent(c in 0..2){
       ascent(a in 0..6){
  let angle = GetAngleToPlayer+60*a;
    let x = GetX+offsetX(8*(b-2),angle+90*(-1)^c); 
    let y = GetY+offsetY(8*(b-2),angle+90*(-1)^c);
   Sward(x,y,v,angle,color,delay);
         }
        }
        }
  PlaySE(seshot1);
      wait(180);
    Move;
    wait(120);
       } 
      }
    }     

   task Sward(x,y,v,angle,color,delay){
        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,false);
         ObjShot_SetBombResist(p,false);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
      Shot;
  Ref;
    task Shot{ 
   wait(delay);
    let dangle = 0;
       while(!Obj_BeDeleted(p)){
     let x = Obj_GetX(p);
     let y = Obj_GetY(p);
     let v = rand(1,3);
     let angle = 180+Obj_GetAngle(p)+rand(-dangle,dangle);
     let color = 176;
     let delay = 10;
      CreateShot01(x,y,v,angle,color,delay);
     wait(10);
      dangle += 2;
          }
       }
   task Ref{ 
  let re = 0;
    while(!Obj_BeDeleted(p)){
      if(Obj_GetX(p) < minx-20 || Obj_GetX(p) > maxx+20){Obj_SetAngle(p,180-Obj_GetAngle(p));re+=1;}
    if(Obj_GetY(p) < miny-20){Obj_SetAngle(p,-Obj_GetAngle(p)); re+=1;}
    wait(1);
    if(re >= 2){Obj_Delete(p);}
         }
     }
    }

  /*  task Move(id){
   let f = 1.2;
  alternative(id)
  case(0){
    SetMovePosition03(minx+16,miny+16,15,2);
    }
   case(1){
    SetMovePosition03(maxx-32,miny+96,15,5);
    }
   case(2){
    SetMovePosition03(maxx-16,miny+16,15,f);
    }
   case(3){
    SetMovePosition03(minx+32,miny+96,15,5);
    }
    case(4){
     SetMovePosition03(cx,miny+120,15,3);
    }
  }  */

   task Move{
   let f = 1.2;
  let r = rand_int(0,1);
 if(GetX < minx+100){r = 0;}
    else if(GetX > maxx-100){r = 1;}
    let x = GetX + 48*(-1)^r;
    let y = GetY + rand(-48,48);
    if(y < miny+48){y = miny+48;}
    else if(y > miny+160){y = miny+160;}
  /* if(GetX > x){sgr(2,1);}
   else if(GetX < x){sgr(2,3);}  */
    SetMovePosition03(x,y,15,f);
    }


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

  function sgr(a,b){SetGraphicRect(a*128,b*128,(a+1)*128,(b+1)*128);}

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