local Entity = require "scenes.levels.entities.parent"

local Debris = Entity:extend()

function Debris:new(level, x, y, speed, dir, timelimit)
  Debris.super.new(self, level, "debris", x - 4, y - 4, 8, 8)
  self.life = 0;
  self.timer = 0
  self.timelimit = timelimit or 2
  self:setMotionDirection(dir, speed)
  self.grav = 1
  self.bounce = 0.5
  self.frc = 0.046875*60*1.5
  self.rotation = love.math.random(0, 360)
end

function Debris:setFilter()
  self.filter = function(item, other)
    if     ((other.collType=="wall"))   then
      return 'bounce'
    else
      return nil
    end
  end
end

function Debris:update(dt)
  self:setFilter()
  self:gravity(dt)
  self:friction(dt)

  --self.x, self.y, cols, cols_len = currentLevel.world:move(self, self.x + self.xsp*dt, self.y + self.ysp*dt, bulletFilter)
  cols, cols_len = self:move(dt)

  self.life = 1

  local isInsideView = self:isInsideView()
  if (isInsideView == false) then
    self:destroy()
  end

  self.timer = self.timer + dt
  if (self.timer >= self.timelimit) then
    self:destroy()
  end

end

function Debris:draw(dt)
  local rotation = math.floor(self.rotation/45)*45
  drawx, drawy = self:getCenter()
  love.graphics.draw(self.level.assets.images["debris"], drawx, drawy, rotation, 1, 1, 4, 4)
end

return Debris
