
#Title[「幻の月に棲む悪魔」]
#Text[幻月・スペル1]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
    #include_script".\Local.txt"
   let name        = " 「幻の月に棲む悪魔」";
   let imgBOSS = csd~"img\dot_gengetu.png";
   let shotData = csd ~ "data_usershot.txt";
  let effect = csd ~ "img\circle.png"; 
  let vanish = true;

   @Initialize{
 SetCommonDataEx("","bg",10);
     CutIn(KOUMA, name, "", 0, 0, 0, 0);
     SetScore(5000000);
           SetLife(600);
      SetDamageRate(10,0);
           SetTimer(99);
           SetInvincibility(120);
         sgr(0,0);
      /*   SetMovePosition03(GetCenterX,GetClipMinY+100,20,5);*/
        SetAlpha(255);
           LoadGraphic(imgBOSS);
    LoadGraphic(effect);
          LoadUserShotData(shotData);
   Circle2(60);
           TMain;
    }

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

         yield;
       }

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

    @Finalize{
 SetCommonDataEx("","bg",8);
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
  loop(100){
     CreateItem(ITEM_SCORE,rand(minx,maxx),miny+rand(16,150)); 
       }   
 if(GetMissCountInThisSpell != 0){CreateItem(ITEM_1UP,GetX,GetY); }
 else{
   loop(3){CreateItem(ITEM_BOMB,rand(minx,maxx),miny+rand(16,150));}
     loop(100){
     CreateItem(ITEM_SCORE,rand(minx,maxx),miny+rand(16,150)); 
         }   
       }
     }
 
     task TMain{
    yield;
     SetGraphicScale(0,0);
    SetX(rand(minx+64,maxx-64));
   SetY(miny+100);
     ascent(a in 0..21){
     SetGraphicScale(a*0.05,a*0.05);
   if(a >= 15){vanish = false;}
      wait(1);
        }
          wait(90);
        Shot;
        Count;
    loop{
   if(OnBomb){
   SetAlpha(0);
   SetDamageRate(0,0);}
     else{
    SetAlpha(255);
   SetDamageRate(10,0);}
    wait(1);
      }
    }

   task Shot{
  loop{
  sgr(3,2);
  ShotA;  ShotB;
  wait(120);
  sgr(0,0);
  Move;
   wait(60);
   sgr(3,2);
   ShotC;  ShotD;
  wait(120);
  Move;
   wait(60);
   }
   task ShotA{
      loop(4){
  ascent(a in 0..36){
    ascent(c in 0..2){
 let angle =  GetAngleToPlayer+10*a;
 let x = GetX+offsetX(4,angle-90+180*c);
      let y = GetY+offsetY(4,angle-90+180*c);
    CreateShot01(x,y,6,angle,174,0);
         }
        }
   PlaySE(seshot1);
      wait(20);
        }
      }
  task ShotB{
   let color = 155; let delay = 0;
  let f = 20;
   loop(50){
   ascent(a in 0..2){
 let x = GetX+rand(-f,f); let y = GetY+rand(-f,f);
     let v = rand(2,8);
    let angle = -45-90*a;
    Ref2(x,y,v,angle,color,delay);
       }
 PlaySE(seshot4);
    wait(2);
         }
      }
   task ShotC{
     loop(90){
  let nn = rand_int(1,3);
  loop(nn){
   let r = rand(0,32);
   let ra = rand(-180,180);
      let angle = 180*rand_int(0,1);
      let x = GetX+offsetX(r,ra);
      let y = GetY+offsetY(r,ra);
    let v = rand(1,8);
      Ref(x,y,v,90,YELLOW01,10);
   }
   PlaySE(seshot4);
   wait(1);
   }
        }
   task ShotD{
    loop(18){
    loop(2){
  CreateEnemyFromFile(csd~".\Enemy103.txt",rand(minx+32,maxx-32),miny+32,0,0,4);
        }
     wait(5);
        }
      }
    }

   task Ref(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+180*rand_int(0,1)-90);
         ObjShot_SetGraphic(p,color); 
         ObjShot_SetDelay(p,delay);
         Obj_SetAutoDelete(p,true);
         ObjShot_SetBombResist(p,false);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
      wait(delay);
    while(!Obj_BeDeleted(p) && Obj_GetSpeed(p) > 0){
      Obj_SetSpeed(p,Obj_GetSpeed(p)-0.1);
      wait(1);
      }
    Obj_SetSpeed(p,0);
    wait(20);
    Obj_SetSpeed(p,v);
   Obj_SetAngle(p,angle);
  /* while(!Obj_BeDeleted(p) && Obj_GetY(p) < maxy){wait(1);}
   Obj_SetAngle(p,-90);*/
   }

   task Ref2(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,true);
         ObjShot_SetBombResist(p,false);
         Obj_SetCollisionToObject(p,true);
         Obj_SetCollisionToPlayer(p,true);
    let re = 0; 
     while(!Obj_BeDeleted(p)){
    let basex = Obj_GetX(p);
   wait(1);
 if(Obj_GetX(p) < minx){Obj_SetAngle(p,180-Obj_GetAngle(p));}
 else if(Obj_GetX(p) > maxx){Obj_SetAngle(p,180-Obj_GetAngle(p));}
 if(Obj_GetY(p) < miny && re == 0){Obj_SetAngle(p,-Obj_GetAngle(p)); re+=1;}
  if(Obj_GetY(p) > maxy){Obj_SetAngle(p,-Obj_GetAngle(p));}
      }
  }

   function Move{
    ascent(a in 0..21){
     SetGraphicScale(1-a*0.05,1-a*0.05);
   if(a >= 15){vanish = true;}
      wait(1);
        }
   SetX(rand(minx+64,maxx-64));
   SetY(GetY);
     ascent(a in 0..21){
     SetGraphicScale(a*0.05,a*0.05);
   if(a >= 15){vanish = false;}
      wait(1);
        }
    }

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