#東方弾幕風
#Title[ホーミング弾]
#Text[]
#ScriptVersion[2]

script_enemy_main {
 let img = "script\img\ExRumia.png";
 let name = "ホーミング";

 @Initialize {
  CutIn(KOUMA, name, "", 0, 0, 0, 0);
  SetX(GetCenterX);
  SetY(GetCenterY);
  SetLife(5000);
  SetScore(1000000);
  SetTimer(99);

  LoadGraphic(img);
  SetTexture(img);
  SetGraphicRect(64, 0, 128, 64);

  TMain;
 }

 @MainLoop {
  SetCollisionA(GetX, GetY, 24);
  SetCollisionB(GetX, GetY, 24);

  yield;
 }

 @DrawLoop {
  DrawGraphic(GetX, GetY);
 }

 @Finalize {
  DeleteGraphic(img);
 }

 //以下タスク(マイクロスレッド)

 task TMain {
  yield;

  TMove;
  TShot; //何個足してもおｋ
 }

 task TMove {
  yield;
 }

 task TShot {
  yield;

  loop {
   loop(10) { yield; }
   CreateHomingShot(GetX, GetY, 1, rand(0, 360), 1, 240, RED11);
  }
 }

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

 task CreateHomingShot(x, y, speed, angle, mo, limit, graphic) {
  let obj = Obj_Create(OBJ_SHOT);

  Obj_SetPosition(obj, x, y);
  Obj_SetSpeed(obj, speed);
  Obj_SetAngle(obj, angle);
  ObjShot_SetGraphic(obj, graphic);

  loop(limit) {
   if(Obj_BeDeleted(obj)) {
    break;
   }

   let angleToPlayer = atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj));

   if(sin(angleToPlayer - Obj_GetAngle(obj)) > 0) {
    Obj_SetAngle(obj, Obj_GetAngle(obj) + mo);
   }
   else {
    Obj_SetAngle(obj, Obj_GetAngle(obj) - mo);
   }
   yield;
  }
 }

}