#東方弾幕風
#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 - 120);
  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;
  TShot2; //何個足してもおｋ
 }

 task TMove {
  yield;
 }

 task TShot {
  yield;

  loop {
   loop(10) { yield; }
   CreateReflectShot(GetX, GetY, rand(1, 2), rand(0, 360), AQUA11, 0, 3);
  }
 }

 task TShot2 {
  let angle = 50;
  yield;

  loop {
   loop(12) {
    CreateReflectShot(GetX, GetY, 1, angle, BLUE11, 0, 2);
    angle += 360 / 12;
   }
   loop(120) { yield; }
  }
 }

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

 task CreateReflectShot(x, y, speed, angle, graphic, delay, limit) {
  //limitは反射回数です

  let obj = Obj_Create(OBJ_SHOT);

  Obj_SetPosition(obj, x, y);
  Obj_SetSpeed(obj, speed);
  Obj_SetAngle(obj, angle);
  ObjShot_SetGraphic(obj, graphic);
  ObjShot_SetDelay(obj, delay);
        
  loop(delay) { yield; }
        
  while(! Obj_BeDeleted(obj)) {

   if(Obj_GetX(obj) < GetClipMinX) {
    Obj_SetX(obj, GetClipMinX * 2 - Obj_GetX(obj));
    Obj_SetAngle(obj, 180 - Obj_GetAngle(obj));
    limit -= 1;
   }

   if(Obj_GetX(obj) > GetClipMaxX) {
    Obj_SetX(obj, GetClipMaxX * 2 - Obj_GetX(obj));
    Obj_SetAngle(obj, 180 - Obj_GetAngle(obj));
    limit -= 1;
   }

   if(Obj_GetY(obj) < GetClipMinY){
    Obj_SetY(obj, GetClipMinY * 2 - Obj_GetY(obj));
    Obj_SetAngle(obj, 360 - Obj_GetAngle(obj));
    limit -= 1;
   }

   if(Obj_GetY(obj) > GetClipMaxY){
    Obj_SetY(obj, GetClipMaxY * 2 - Obj_GetY(obj));
    Obj_SetAngle(obj, 360 - Obj_GetAngle(obj));
    limit -= 1;
   }

   if(limit == 0) { //もう跳ね返らない弾は赤になります
    ObjShot_SetGraphic(obj, RED01);
   }

   if(limit <= 0) { break; }

   yield;
  }
 }

}