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

local Debris = Entity:extend()

function Debris:new(world, x, y, speed, dir, timelimit)
  Debris.super.new(self, world, "debris", x - 4, y - 4, 8, 8)
  local timelimit = timelimit or 2

  self:addTimer("timelimit", timelimit)

  self:setMotionDirection(dir, speed)
  self:setGravityFlag(true)
  self:setBounceFactor(0.5)
  self.xfrc = 0.046875*60*1.5*60*4
  self.rotation = love.math.random(0, 360)
end

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

function Debris:updateEnd(dt)
  self.life = 1

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

function Debris:timerResponse(timer)
  if (timer == "timelimit") then
    self:destroy()
  end
end

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

return Debris
