
#Title[時符　「永久機関の歯車」]
#Text[夢月・スペル8]
#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(800);
      SetDamageRate(20,0);
           SetTimer(70);
           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(64){
     CreateItem(ITEM_SCORE,rand(minx,maxx),miny+rand(16,150)); 
       }   
     }
 
     task TMain{
     wait(90);
  Enemy;
       Shot;
        Count;
    loop{
   if(OnBomb){
   SetAlpha(0);
   SetDamageRate(0,0);}
     else{
    SetAlpha(255);
   SetDamageRate(20,0);}
    wait(1);
      }
    }

   task Shot{
     wait(180);
 loop{
   ascent(a in 0..13){
   let angle = GetAngleToPlayer+15*(a-6);
     CreateShot01(GetX,GetY,1.5,angle,RED01,10);
      }
  ascent(b in 0..3){
   ascent(a in 0..39){
   let angle = 5*(a-19)-90+rand(-5,5);
    CreateShot01(GetX,GetY,4.5,angle,RED01,10*(b+1));
      }
   }
   wait(30);
       }
    }

   task Enemy{
   let x = cx;
   let y = cy;
   let v = 0;
  ascent(a in 0..2){
  let angle = 135+180*a;
  CreateEnemyFromScript("S",x,y,v,angle,a);
    }
  wait(120);
    ascent(a in 0..8){
  let angle = 45*a;
  CreateEnemyFromScript("S",x,y,v,angle,a+2);
    }
  }

    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";
 let dlaser = csd~ "se\laserdelay.wav"; 
	@Initialize
	{
         SetLife(9999);
            SetDamageRateEx(0,0,0,0);
            LoadGraphic(Enemy);
           LoadGraphic(effect);
  LoadSE(dlaser);
      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 d = -0.5*(-1)^arg;
  let color = BLUE01;
  let delay = 60;
  let length = 120;
  let width = 30;
 if(arg >= 2){d = 3*d; delay = 120;}
  ascent(a in 0..4){
   let angle = 90*a;
    CreateLaserB(0,length,width,color,delay);
   SetLaserDataB(0,0,0,0,0,0,0,angle,d);
   FireShot(0);
       }
  loop(3){
   PlaySE(dlaser);
   wait(1);
     }
    }

  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{
  let radius = 100;
 if(arg >= 2){radius = 200;}
   let angle = GetAngle+45;
     loop{
      SetX(GetCenterX+offsetX(radius,angle));
      SetY(GetCenterY+50+offsetY(radius,angle));
     angle -= 0.25;
   if(arg >= 2){angle += 1;}
        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;}
       }
}