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

local Bullet = Entity:extend()

function Bullet:new(level, x, y, w, h, speed, dir)
  Bullet.super.new(self, level, "bullet", x - (w / 2), y - (h / 2), w, h)
  self.life = 0;
  self:setMotionDirection(dir, speed)
end

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

function Bullet:update(dt)
  self:setFilter()

  local cols, cols_len
  self.x, self.y, cols, cols_len = self:move(dt)

  for j=1,cols_len do
    local other = cols[j].other
    if other.collType=="wall" and (self.life == 1) then
      self:destroy()
    end
  end

  self.life = 1

  local isInsideView = self:isInsideView()
  if (isInsideView == false) or (self.xsp == 0) then
    self:destroy()
  end
end

function Bullet:draw(dt)
  currentLevel:drawHitbox(self, 0,128,0)
end

return Bullet
