
#Title[@u̍킹v]
#Text[EXy3]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
    #include_script".\Local.txt"
   let name        = " @u̍킹v";
   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(600);
      SetDamageRate(20,0);
           SetTimer(64);
           SetInvincibility(360);
         sgr(0,0);
         SetMovePosition03(GetCenterX,GetClipMinY+120,20,5);
        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(60);
   CreateLaserA(0,cx,miny-30,0,10,192,4000);
   SetLaserDataA(0,0,90,0,5,0,0);
   SetLaserDataA(0,120,NULL,0,0,0,0);
   FireShot(0);
          wait(120);
  CreateEnemyFromScript("S",GetX,GetY,0,90,0);
  PlaySE(sefreeze);
 loop(3){Explosion01(GetX,GetY,0,10,10);}
 if(GetPlayerX > cx){
  SetMovePosition03(GetCenterX+100,GetClipMinY+150,15,5); 
   }
 else if(GetPlayerX <= cx){
  SetMovePosition03(GetCenterX-100,GetClipMinY+150,15,5); 
   }
  wait(120);
        Shot;
        Count;
   Move;
    loop{
   if(OnBomb){
   SetAlpha(0);
   SetDamageRate(0,0);}
     else{
    SetAlpha(255);
   SetDamageRate(20,0);}
    wait(1);
      }
    }

   task Shot{
  let n = 10;
   loop{
    let way = 24;
    loop(n*2){
      ascent(a in 0..way){
      let base = 360/way*a;
      let x = GetX;
      let y = GetY;
     ascent(v in 0..1){
      Ref(x,y,2-v,base,PURPLE12,10);
          }
      }
      PlaySE(seshot1);
       wait(30);
       }
   n -= 1;
  if(n < 1){n = 1;}
      ascent(a in 0..way){
      let base = GetAngleToPlayer+360/way*a;
      let x = GetX;
      let y = GetY;
     ascent(v in 0..4){
      Ref(x,y,4-v,base+180/way*v,BLUE02,10);
          }
         }
       }
    }

   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,false);
         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(basex <= cx && Obj_GetX(p) >= cx){Obj_Delete(p);}
 else if(basex >= cx && Obj_GetX(p) <= cx){Obj_Delete(p);}
 else if(Obj_GetX(p) < minx-20){Obj_SetX(p,maxx+20); ree += 1;}
 else if(Obj_GetX(p) > maxx+20){Obj_SetX(p,minx-20); ree += 1;}
  if(ree == 2){Obj_Delete(p);}
      }
   }

    task Move{
 let basex = GetX;
 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;
  if(basex < cx && x >= cx){x = GetX + 32*(-1)^1;}
  else if(basex > cx && x <= cx){x = GetX + 32*(-1)^0;}
    let y = GetY + rand(-48,48);
    if(y < miny+48){y = miny+48;}
    else if(y > miny+180){y = miny+180;}
  /* if(GetX > x){sgr(2,1);}
   else if(GetX < x){sgr(2,3);}  */
    SetMovePosition03(x,y,15,f);
     wait(120);
      }
    }

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

script_enemy S{
      #include_script".\Local.txt"
  let csd = GetCurrentScriptDirectory;
	let Enemy=csd~"img\dot_mugetu.png";
        let shotData = csd ~ "data_usershot.txt";
   let arg = GetArgument;
  let effect = csd ~ "img\circle.png";
  let cx = GetCenterX;
  let cy = GetCenterY;
   let minx = GetClipMinX; 
   let maxx = GetClipMaxX;
	@Initialize
	{
         SetLife(3000);
        SetDamageRateEx(100,0,0,0);
            LoadGraphic(Enemy);
           LoadGraphic(effect);
      SetAlpha(255);
            LoadUserShotData(shotData);
      sgr(0,0);	
          SetInvincibility(240);
               Main;
	}
	
	 @MainLoop{
   if(OnEvent){break;}
      SetCollisionA(GetX, GetY, 24);
      SetCollisionB(GetX, GetY, 18);
    yield;
     }
	
       	@DrawLoop{
               SetTexture(Enemy);
		DrawGraphic(GetX,GetY);
	}

	@Finalize{ 
   CreateItem(ITEM_1UP,GetX,GetY); 
	}

         task Main{
            yield;
    Circle(25);
       Shot;
  Shot2;
    Move;
     }

  task Shot{
  wait(120);
    let way = 24;
    loop{
      ascent(a in 0..way){
      let base = 360/way*a;
      let x = GetX;
      let y = GetY;
     ascent(v in 0..1){
      Ref2(x,y,2-v,base,PURPLE12,10);
          }
      }
       wait(30);
       }
    }

    task Shot2{
  wait(120);
  let v = 2;
  let color = RED31;
  let delay = 10;
  let d = 0;
  loop{
    let base = GetAngleToPlayer+180;
   let x = GetX;
   let y = GetY;
  ascent(a in 0..2){
   let angle = base + d - 2*d*a;
   Ref(x,y,v,angle,color,delay);
    }
   d += 10;
     wait(10);
       }
     }

   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(basex <= cx && Obj_GetX(p) >= cx){Obj_SetAngle(p,180-Obj_GetAngle(p)); wait(2);}
 else if(basex >= cx && Obj_GetX(p) <= cx){Obj_SetAngle(p,180-Obj_GetAngle(p)); wait(2);}
 else if(Obj_GetX(p) < minx-20){Obj_SetX(p,maxx+20); ree += 1;}
 else if(Obj_GetX(p) > maxx+20){Obj_SetX(p,minx-20); ree += 1;}
 if(Obj_GetY(p) < GetClipMinY-20){Obj_SetAngle(p,-Obj_GetAngle(p));}
  if(ree == 2){Obj_Delete(p);}
      }
   }

   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,false);
         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(basex <= cx && Obj_GetX(p) >= cx){Obj_Delete(p);}
 else if(basex >= cx && Obj_GetX(p) <= cx){Obj_Delete(p);}
 else if(Obj_GetX(p) < minx-20){Obj_SetX(p,maxx+20); ree += 1;}
 else if(Obj_GetX(p) > maxx+20){Obj_SetX(p,minx-20); ree += 1;}
  if(ree == 2){Obj_Delete(p);}
      }
   }

  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)*128+128,sin(i*6)*128+128);
                ObjEffect_SetVertexColor(c,i+1,255,255,255,255);
            }
      Set; 
     task Set{
         while(!Obj_BeDeleted(c)){
            Obj_SetX(c,GetX); Obj_SetY(c,GetY);
            wait(1);
           }
         }
    }

   task Move{
     loop{
       SetX(2*cx-GetEnemyX);
       SetY(GetEnemyY);
        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;}
       }
}