script_enemy_main {
  let objs = 0;
  let enemyID = GetArgument;
  let shotsKey = "enemy" ~ ToString(enemyID) ~ " shots";
  let defeatedKey = "enemy" ~ ToString(enemyID) ~ " defeated";
  @Initialize {
    SetLife(65535);
  }
  
  @MainLoop {
    yield;
    let newShots = GetNewShots;
    ClearNewShots;
    
    ascent(i in 0..length(newShots)){
      let obj = newShots[i][0];
      let life = newShots[i][1];
      objs++;
      ManageShot(obj, life);
    }
    
    if(IsParentDefeated && objs == 0){
      VanishEnemy;
    }
  }
  
  task ManageShot(obj, lifetime){
    let counter = 0;
    while(!Obj_BeDeleted(obj)){
      Obj_SetPosition(obj, Obj_GetX(obj)-GetPlayerDisplaceX, Obj_GetY(obj)-GetPlayerDisplaceY);
      if(Obj_GetX(obj) > GetCenterX+320){
        Obj_SetX(obj, Obj_GetX(obj)-640);
      }
      if(Obj_GetX(obj) < GetCenterX-320){
        Obj_SetX(obj, Obj_GetX(obj)+640);
      }
      if(Obj_GetY(obj) > GetCenterY+320){
        Obj_SetY(obj, Obj_GetY(obj)-640);
      }
      if(Obj_GetY(obj) < GetCenterY-320){
        Obj_SetY(obj, Obj_GetY(obj)+640);
      }
      if(lifetime > 0){
        counter++;
        if(counter > lifetime){ break; }
      }
      yield;
    }
    Obj_Delete(obj);
    objs--;
  }
  
  function IsParentDefeated {
    return GetCommonDataDefaultEx("temp", defeatedKey, false);
  }
  function GetNewShots {
    return GetCommonDataDefaultEx("temp", shotsKey, []);
  }
  sub ClearNewShots {
    SetCommonDataEx("temp", shotsKey, []);
  }
  function GetPlayerDisplaceX {
    return GetCommonDataEx("temp", "xDisplace");
  }
  function GetPlayerDisplaceY {
    return GetCommonDataEx("temp", "yDisplace");
  }
}