
#Title[磨符　「マジックミラーバーン」]
#Text[夢月・スペル1]
#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(3000000);
           SetLife(700);
      SetDamageRate(20,0);
           SetTimer(70);
           SetInvincibility(240);
         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){
      SetCollisionA(GetX, GetY, 36);
      SetCollisionB(GetX, GetY, 18);
     }

         yield;
       }

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

    @Finalize{
  SetCommonDataEx("","bg",3);
        DeleteGraphic(imgBOSS);
        DeleteGraphic(effect);
  loop(64){
     CreateItem(ITEM_SCORE,rand(minx,maxx),miny+rand(16,150)); 
       }   
  sc(255,255,255);
     }
 
     task TMain{
     wait(30);
   con(120);
  loop(3){Explosion01(GetX,GetY,0,10,10);}
   PlaySE(sefreeze);
   Slip;
  wait(30);
        Shot;
        Count;
    loop{
   if(OnBomb){
   SetAlpha(0);
   SetDamageRate(0,0);}
     else{
    SetAlpha(255);
   SetDamageRate(20,0);}
    wait(1);
      }
    }

   task Shot{
  loop{
  con(90);
 ShotA;
  wait(120);
  SetMovePosition03(GetX+120,GetY-30,20,2);
  con(240);
  ShotB;
  wait(120);
   SetMovePosition03(GetX-210,GetY,20,2);
  con(240);
  ShotC;
  wait(120);
  SetMovePosition03(GetX-60,GetY-20,20,0.5);
  con(180);
  SetMovePosition02(maxx-60,GetY,120);
  ShotD;
  wait(120);
  SetMovePosition03(GetCenterX,GetClipMinY+100,20,5);
  wait(120);
  }

  function ShotA{
    ascent(a in 0..5){
  let v = 5;
  let angle = -90+30*(a-2);
  let color = BLUE03;
   let delay = 0;
  sc(128,255,255);
   Ref(GetX,GetY,v,angle,color,delay);
    }
  loop(48){
 let base = rand(-180,180);
  let x = GetX+offsetX(16,base);
  let y = GetY+offsetY(16,base);
    let angle = -90+30*(rand_int(0,4)-2);
  let color = BLUE02;
   let delay = 0;
 let v = rand(3,5);
  sc(255,255,255);
   Ref(x,y,v,angle,color,delay);
   }
 loop(100){
 let base = rand(-180,180);
  let x = GetX+offsetX(32,base);
  let y = GetY+offsetY(32,base);
    let angle = -90+30*(rand_int(0,4)-2);
  let color = BLUE04;
   let delay = 0;
 let v = rand(0.5,4);
  sc(128,128,255);
   Ref(x,y,v,angle,color,delay);
   }
  PlaySE(seshot1);
  SetMovePosition02(GetX,GetY+30,5);
   }

  function ShotB{
     ascent(a in 0..5){
  let v = 5;
  let angle = 30*(a-2);
  let color = BLUE03;
   let delay = 0;
  sc(128,255,255);
   Ref(GetX,GetY,v,angle,color,delay);
      }
   loop(48){
 let base = rand(-180,180);
  let x = GetX+offsetX(16,base);
  let y = GetY+offsetY(16,base);
    let angle = 30*(rand_int(0,4)-2);
  let color = BLUE02;
   let delay = 0;
 let v = rand(3,5);
  sc(255,255,255);
   Ref(x,y,v,angle,color,delay);
   }
   loop(100){
 let base = rand(-180,180);
  let x = GetX+offsetX(32,base);
  let y = GetY+offsetY(32,base);
    let angle = 30*(rand_int(0,4)-2);
  let color = BLUE04;
   let delay = 0;
 let v = rand(0.5,4);
  sc(128,128,255);
   Ref(x,y,v,angle,color,delay);
   }
  PlaySE(seshot1);
   SetMovePosition02(GetX-30,GetY,5);
    }

   function ShotC{
     ascent(a in 0..5){
  let v = 5;
  let angle = 180+30*(a-2);
  let color = BLUE03;
   let delay = 0;
  sc(128,255,255);
   Ref(GetX,GetY,v,angle,color,delay);
      }
   loop(48){
 let base = rand(-180,180);
  let x = GetX+offsetX(16,base);
  let y = GetY+offsetY(16,base);
    let angle = 180+30*(rand_int(0,4)-2);
  let color = BLUE02;
   let delay = 0;
 let v = rand(3,5);
  sc(255,255,255);
   Ref(x,y,v,angle,color,delay);
   }
   loop(100){
 let base = rand(-180,180);
  let x = GetX+offsetX(32,base);
  let y = GetY+offsetY(32,base);
    let angle = 180+30*(rand_int(0,4)-2);
  let color = BLUE04;
   let delay = 0;
 let v = rand(0.5,4);
  sc(128,128,255);
   Ref(x,y,v,angle,color,delay);
   }
  PlaySE(seshot1);
   SetMovePosition02(GetX+30,GetY,5);
    }

   function ShotD{
  loop(6){
  let d = rand(-10,10);
    ascent(a in 0..1){
  let v = 5;
  let angle = -90+d;
  let color = BLUE03;
   let delay = 0;
  sc(128,255,255);
   Ref(GetX,GetY,v,angle,color,delay);
    }
  loop(10){
 let base = rand(-180,180);
  let x = GetX+offsetX(16,base);
  let y = GetY+offsetY(16,base);
    let angle = -90+d;
  let color = BLUE02;
   let delay = 0;
 let v = rand(3,5);
  sc(255,255,255);
   Ref(x,y,v,angle,color,delay);
   }
 loop(15){
 let base = rand(-180,180);
  let x = GetX+offsetX(32,base);
  let y = GetY+offsetY(32,base);
    let angle = -90+d;
  let color = BLUE04;
   let delay = 0;
 let v = rand(0.5,4);
  sc(128,128,255);
   Ref(x,y,v,angle,color,delay);
   }
  PlaySE(seshot1);
  wait(20);
  }
   }
  }

    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);
         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;  let ree = 0;
     while(!Obj_BeDeleted(p)){
    let basex = Obj_GetX(p);
   wait(1);
 if(Obj_GetX(p) < minx-20 && re == 0){Obj_SetAngle(p,180-Obj_GetAngle(p)); re += 1;}
 else if(Obj_GetX(p) > maxx+20 && re == 0){Obj_SetAngle(p,180-Obj_GetAngle(p)); re += 1;}
 else if(Obj_GetX(p) < minx-20 && re == 1){Obj_SetX(p,maxx+20); re += 1;}
 else if(Obj_GetX(p) > maxx+20 && re == 1){Obj_SetX(p,minx-20); re += 1;}
 if(Obj_GetY(p) < miny-20){Obj_SetAngle(p,-Obj_GetAngle(p));}
 else if(Obj_GetY(p) > maxy+20){Obj_SetY(p,miny-20); ree += 1;}
  if(re >= 3 || ree >= 2){Obj_Delete(p);}
      }
   }

    task Move{
 loop{
   let f = 1.2;
  let r = rand_int(0,1);
 if(GetX < minx+64){r = 0;}
    else if(GetX > maxx-64){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);
     wait(180);
      }
    }

  task Slip{
  let hs = GetPlayerInfo(PLAYER_SPEED_HIGH);
  let ls = GetPlayerInfo(PLAYER_SPEED_LOW);
  let dx = 0; let dy = 0; 
  let f = 10;
  loop{
 let x = GetPlayerX; let y = GetPlayerY;
 if(GetKeyState(VK_LEFT) && !GetKeyState(VK_UP) && !GetKeyState(VK_DOWN) && !GetKeyState(VK_SLOWMOVE)){dx -= hs/f;}
 else if(GetKeyState(VK_LEFT)  && !GetKeyState(VK_UP) && !GetKeyState(VK_DOWN) && GetKeyState(VK_SLOWMOVE)){dx -= ls/f;}
 else if(GetKeyState(VK_RIGHT) && !GetKeyState(VK_UP) && !GetKeyState(VK_DOWN) && !GetKeyState(VK_SLOWMOVE)){dx += hs/f;}
 else if(GetKeyState(VK_RIGHT) && !GetKeyState(VK_UP) && !GetKeyState(VK_DOWN) && GetKeyState(VK_SLOWMOVE)){dx += ls/f;}
 else if(GetKeyState(VK_UP) && !GetKeyState(VK_LEFT) && !GetKeyState(VK_RIGHT) && !GetKeyState(VK_SLOWMOVE)){dy -= hs/f;}
 else if(GetKeyState(VK_UP) && !GetKeyState(VK_LEFT) && !GetKeyState(VK_RIGHT) && GetKeyState(VK_SLOWMOVE)){dy -= ls/f;}
 else if(GetKeyState(VK_DOWN) && !GetKeyState(VK_LEFT) && !GetKeyState(VK_RIGHT) && !GetKeyState(VK_SLOWMOVE)){dy += hs/f}
 else if(GetKeyState(VK_DOWN) && !GetKeyState(VK_LEFT) && !GetKeyState(VK_RIGHT) && GetKeyState(VK_SLOWMOVE)){dy += ls/f;}
 else if(GetKeyState(VK_LEFT) && GetKeyState(VK_UP) && !GetKeyState(VK_SLOWMOVE)){dx -= hs/f/2^0.5; dy -= hs/f/2^0.5;}
 else if(GetKeyState(VK_LEFT)  && GetKeyState(VK_UP) && GetKeyState(VK_SLOWMOVE)){dx -= ls/f/2^0.5; dy -= ls/f/2^0.5;}
 else if(GetKeyState(VK_RIGHT) && GetKeyState(VK_UP) && !GetKeyState(VK_SLOWMOVE)){dx += hs/f/2^0.5; dy -= hs/f/2^0.5;}
 else if(GetKeyState(VK_RIGHT) && GetKeyState(VK_UP) && GetKeyState(VK_SLOWMOVE)){dx += ls/f/2^0.5; dy -= ls/f/2^0.5;}
 else if(GetKeyState(VK_LEFT) && GetKeyState(VK_DOWN) && !GetKeyState(VK_SLOWMOVE)){dx -= hs/f/2^0.5; dy += hs/f/2^0.5;}
 else if(GetKeyState(VK_LEFT)  && GetKeyState(VK_DOWN) && GetKeyState(VK_SLOWMOVE)){dx -= ls/f/2^0.5; dy += ls/f/2^0.5;}
 else if(GetKeyState(VK_RIGHT) && GetKeyState(VK_DOWN) && !GetKeyState(VK_SLOWMOVE)){dx += hs/f/2^0.5; dy += hs/f/2^0.5;}
 else if(GetKeyState(VK_RIGHT) && GetKeyState(VK_DOWN) && GetKeyState(VK_SLOWMOVE)){dx += ls/f/2^0.5; dy += ls/f/2^0.5;}
 else if(OnPlayerMissed){dx = 0; dy = 0;}
 else{dx = dx*0.99; dy = dy*0.99;}
   SetPlayerX(x+dx); SetPlayerY(y+dy);
   wait(1);
    }
 }

   function con(frame){
    Concentration01(frame);
     PlaySE(secon);
    wait(frame);
    }

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