script_enemy_main {
  #include_function ".\lib\enemy.dnh"
  #include_function ".\lib\utusho_animation.dnh"
  @Initialize {
    InitializeEnemy;
    InitializeUtsuhoAnimation;
    SetLife(3000);
    SetDamageRate(0, 0);
    mainTask;
  }
  
  @MainLoop {
    if(Spawned){
      SetCollisionA(GetX, GetY, 48);
      SetCollisionB(GetX, GetY, 32);
    }
    yield;
  }
  
  @DrawLoop {
    if(Spawned){
      DrawUtsuho;
    }
  }
  
  @Finalize {
    DeleteGraphic(imgUtsuho);
    FinalizeEnemy;
  }
  
  task mainTask {
    while(!Spawned || OnEvent){ yield; }
    Move(GetX, GetY-100);
    loop {
      while(GetWrapSpeed > 0){ yield; }
      Animate("utsuho", "start cast", false);
      Wait(GetAnimationTotalTime("utsuho", "start cast")+5);
      AddBulletSpam;
      SetDamageRate(100, 0);
      Wait(100);
      Animate("utsuho", "stop cast", false);
      Wait(GetAnimationTotalTime("utsuho", "stop cast"));
      Animate("utsuho", "idle", true);
      Wait(30);
      RandomMove;
    }
    
    task AddBulletSpam {
      loop(20){
        PlaySE(sfxShot);
        ascent(i in 0..30){
          AccelBullet(i*12);
        }
        Wait(5);
      }
      
      task AccelBullet(angle){
        let obj = WrapObjShot_Initialize(GetX-4, GetY-32, 0, angle, 201, 10, 180);
        ascent(i in 0..50){
          Obj_SetSpeed(obj, 1+i*0.1);
          yield;
        }
      }
    }
  }
  
  task RandomMove {
    let x = GetX+rand(-120, 120);
    let y = GetY+rand(-120, 120);
    while(Distance(GetX, GetY, x, y) < 50){
      x = GetX+rand(-120, 120);
      y = GetY+rand(-120, 120);
    }
    let angle = AngleFrom(GetX, GetY, x, y);
    let dist = Distance(GetX, GetY, x, y);
    SetWrapAngle(angle);
    while(dist > 1){
      SetWrapSpeed(dist/20);
      dist -= GetWrapSpeed;
      yield;
    }
    SetWrapSpeed(0);
  }
  
  task Move(x, y){
    let angle = AngleFrom(GetX, GetY, x, y);
    let dist = Distance(GetX, GetY, x, y);
    SetWrapAngle(angle);
    while(dist > 1){
      SetWrapSpeed(dist/20);
      dist -= GetWrapSpeed;
      yield;
    }
    SetWrapSpeed(0);
  }
}