#include_function ".\lib\common.dnh"

function WrapObj_Create(type){
  let obj = Obj_Create(type);
  ManageWrapping;
  return obj;

  task ManageWrapping {
    while(!Obj_BeDeleted(obj)){
      Obj_SetPosition(obj, Obj_GetX(obj)-GetCommonDataEx("temp", "xDisplace"), Obj_GetY(obj)-GetCommonDataEx("temp", "yDisplace"));
      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);
      }
      yield;
    }
  }
}

function WrapObjShot_Create(lifetime){
  if(GetLife <= 0){ return NULL; }

  let obj = Obj_Create(OBJ_SHOT);
  Obj_SetAutoDelete(obj, false);
  ManageWrapping;
  return obj;
  
  task ManageWrapping {
    let counter = 0;
    while(!Obj_BeDeleted(obj)){
      Obj_SetPosition(obj, Obj_GetX(obj)-GetCommonDataEx("temp", "xDisplace"), Obj_GetY(obj)-GetCommonDataEx("temp", "yDisplace"));
      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);
  }
}

function WrapObjShot_Initialize(x, y, speed, angle, graphic, delay, lifetime){
  let obj = WrapObjShot_Create(lifetime);
  Obj_SetPosition(obj, x, y);
  Obj_SetSpeed(obj, speed);
  Obj_SetAngle(obj, angle);
  ObjShot_SetGraphic(obj, graphic);
  ObjShot_SetDelay(obj, delay);
  return obj;
}

task WrapObj_SetX(id, x){
  Obj_SetX(id, x);
}

task WrapObj_SetY(id, y){
  Obj_SetY(id, y);
}

function WrapObj_SetPosition(id, x, y){
  WrapObj_SetX(id, x);
  WrapObj_SetY(id, y);
}