---------------------
-- CENTERED OBJECT --
---------------------
-- for all intents, this behaves as a rectangle with the physics
-- it may be interesting to add some physics to manage this
-- but I am not sure, due to complexity spiraling out of control

CenteredObject = class(Object, function(o, x, y, width, height)
  Object.init(o, x, y, width, height)
  o.scale = 1
  o.rot = 0
end)
function CenteredObject:draw()
  local flip = 0
  if self.flip then flip = self.flip end
  local cameras = self.cameras or bludG.cameras
  if not self.cam then
    self.cam = Rectangle(0, 0, bludG.camera.w, bludG.camera.h)
  end
  if self.drawType then
    if self.drawType == "svg" and self.svg then
      self.svg:setPosition(self.pos.x, self.pos.y)
      self.svg:resetTransform()
      self.svg:scale(self.scale, self.scale)
      self.svg:rotate(self.rot)
      return self.svg:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale)
    end
  end
  if self.sprite then
    -- loop through all the cameras
    for i,camera in pairs(cameras) do
      local pos = Vec2(self.pos.x, self.pos.y)
      self.pos.x, self.pos.y = self:getCameraOffsetZoom(camera)
      -- draw the sprite if it is on the current camera
      if self:doesRectangleTouch(camera) or self.forceDraw then
        -- calculate the position on the current camera
        self.sheet:addCenterRotatedTile(self.sprite, self.pos.x, self.pos.y, self.layer, flip, self.scale*(camera.scale*self.scrollFactor.x), self.rot, self.tint.r*(self.tint.a/255.0), self.tint.g*(self.tint.a/255.0), self.tint.b*(self.tint.a/255.0), self.tint.a)
      end
      self.pos.x, self.pos.y = pos.x, pos.y
    end
  else
    Rectangle.draw(self)
  end
end

-- creates an "svg" of a circle
-- assume centered on 0, 0
function circleSvgGen(radius, segments, width)
  width = width or 2
  width = width/2
  segments = segments or 20
  local svgObject = ObjectGroup()
  svgObject:setSheet(sheets['tiles'])
  local outerR = radius+width
  local innerR = radius-width
  for i=0,segments do
    local x1, y1 = math.cos(i/segments*math.pi*2), math.sin(i/segments*math.pi*2)
    local x2, y2 = math.cos((i+1)/segments*math.pi*2), math.sin((i+1)/segments*math.pi*2)
    local x11, y11, x12, y12 = x1*outerR, y1*outerR, x1*innerR, y1*innerR
    local x21, y21, x22, y22 = x2*outerR, y2*outerR, x2*innerR, y2*innerR
    svgObject:addCorners(sprites['tiles']['white.png'], x11, y11,x21, y21, x22, y22, x12, y12, bludColor(255, 255, 255, 255))
  end
  return svgObject
end