
#Title[ǌ@uYx[gbvv]
#Text[EXy1]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main{
    #include_script".\Local.txt"
   let name        = " ǌ@uYx[gbvv";
   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(54);
           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){
      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(30);
   Move;
          wait(60);
  Enemy;
        Shot;
        Count;
    loop{
   if(OnBomb){
   SetAlpha(0);
   SetDamageRate(0,0);}
     else{
    SetAlpha(255);
   SetDamageRate(20,0);}
    wait(1);
      }
    }

   task Shot{
  Shot5;
  loop{
  let w = 240;
   loop(3){
     Shot1;  Shot2;
   wait(w);
   w -= 30;
    Shot3; Shot4;
    wait(w);
   w -= 30;
       }
   wait(240);
    Shot1;  Shot2; Shot3; Shot4;
    wait(360);
     }
    task Shot1{
    let way = 14;
   ascent(a in 0..way){
      let y1 = miny+(miny+maxy)/(way)*a;
      let y2 = maxy-(miny+maxy)/(way)*a;
    CreateShot01(minx,y1,1,atan2(cy-y1,cx-minx),PURPLE04,10);
    CreateShot01(maxx,y2,1,atan2(cy-y2,cx-maxx),PURPLE04,10);
          }
    PlaySE(seshot1);
       }
     task Shot2{
    let way = 12;
   ascent(a in 0..way){
      let x1 = minx+(minx+maxx)/(way)*a;
      let x2 = maxx-(minx+maxx)/(way)*a;
    CreateShot01(x1,miny,1,atan2(cy-miny,cx-x1),PURPLE04,10);
    CreateShot01(x2,maxy,1,atan2(cy-maxy,cx-x2),PURPLE04,10);
          }
    PlaySE(seshot1);
       }
    task Shot3{
    let way = 14;
   ascent(a in 0..way){
      let y1 = miny+(miny+maxy)/(way)*a;
      let y2 = maxy-(miny+maxy)/(way)*a;
    CreateShot01(minx,y1,1,atan2(GetPlayerY-y1,GetPlayerX-minx),BLUE01,10);
    CreateShot01(maxx,y2,1,atan2(GetPlayerY-y2,GetPlayerX-maxx),BLUE01,10);
          }
    PlaySE(seshot1);
       }
     task Shot4{
    let way = 12;
   ascent(a in 0..way){
      let x1 = minx+(minx+maxx)/(way)*a;
      let x2 = maxx-(minx+maxx)/(way)*a;
   CreateShot01(x1,miny,1,atan2(GetPlayerY-miny,GetPlayerX-x1),BLUE01,10);
    CreateShot01(x2,maxy,1,atan2(GetPlayerY-maxy,GetPlayerX-x2),BLUE01,10);
          }
    PlaySE(seshot1);
       }
   task Shot5{
  let radius = 64;
  let v = 0.2;
  let color = 17;
  let delay = 10;
  loop{
   let base = rand(-180,180);
   let x = GetPlayerX+offsetX(radius,base);
   let y = GetPlayerY+offsetY(radius,base);
   let angle = atan2(GetPlayerY-y,GetPlayerX-x);
    CreateShot01(x,y,v,angle,color,delay);
     wait(30);
       }
     }
    }

   task Enemy{
   let x = [minx+20,minx+20,maxx-20,maxx-20];
   let y = [miny+20,maxy-20,maxy-20,miny+20];
   let v = 2;
  let w = 60;
  loop{
  let i = rand_int(0,3);
  let angle = 0;
  if(i == 0){angle = 90*rand_int(0,1);}
  else if(i == 1){angle = 90*rand_int(0,1)-90;}
  else if(i == 2){angle = 90*rand_int(0,1)-180;}
  else if(i == 3){angle = 90*rand_int(0,1)+90;}
  CreateEnemyFromScript("S",x[i],y[i],v,angle,0);
   wait(w);
    }
  }

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

    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_yousei.png";
        let shotData = csd ~ "data_usershot.txt";
   let arg = GetArgument;
  let effect = csd ~ "img\circle.png";
	@Initialize
	{
         SetLife(9999);
            SetDamageRateEx(0,0,0,0);
            LoadGraphic(Enemy);
           LoadGraphic(effect);
      SetAlpha(0);
            LoadUserShotData(shotData);
      sgr(1,4);	
          SetInvincibility(0);
               Main;
	}
	
	@MainLoop{
           SetCollisionA(GetX(),GetY(),36);
		yield;
	}
	
       	@DrawLoop{
               SetTexture(Enemy);
		DrawGraphic(GetX,GetY);
	}

	@Finalize{ 
	}

         task Main{
             yield; 
  Circle(20);
       Shot;
  Move;
     }

    task Shot{
  let v = 3;
  let color = RED03;
  let delay = 0;
  loop{
   let base = GetAngle+180;
   let x = GetX;
   let y = GetY;
    CreateShot01(x,y,v,base,color,delay);
     wait(30);
       }
     }

  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{
       if(GetX > GetClipMaxX+20 || 
          GetX < GetClipMinX-20 ||  
          GetY < GetClipMinY-20 ||
          GetY > GetClipMaxY+20){
          VanishEnemy;
          }
        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*32,b*32,(a+1)*32,(b+1)*32);}

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