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

local Weapon = Entity:extend()

function Weapon:new(world, x, y, id, xsp, ysp)
  Weapon.super.new(self, world, "weapon", x-8, y - 8, 16, 16)
  self.weaponid = id or 0
  self.xsp = xsp or 0
  self.ysp = ysp or 0
  self.rotation = 0
end

function Weapon:setFilter()
  self.filter = function(item, other)
    if     (other.type=="wall")   then
      return 'touch'
    elseif (other.type=="block")   then
      return "touch"
    elseif (other.type=="ennemy")   then
      return "cross"
    else
      return "cross"
    end
  end
end

function Weapon:updateStart(dt)
  self.rotation = self.rotation + (dt * 90)*8
end

function Weapon:updateEnd(dt)
  local isInsideView = self:isInsideView()
  if (isInsideView == false) then
    self:destroy()
  end

  self:getDirection()
end

function Weapon:collisionResponse(collision)
  local other = collision.other

  if other.type=="wall" then
    self:destroy()
  end
  if other.type=="block" then
    self:destroy()
    other:breakBlock()
  end
  if other.type=="ennemy" then
    self:destroy()
    other:getDamage(1)
  end
end

function Weapon:draw(dt)
  local rotation = math.floor(self.rotation/45)*45
  drawx, drawy = self:getCenter()
  self.scene.assets.tileset["weapon"]:drawTile(self.weaponid, drawx, drawy,
                                                rotation, self.direction, 1, 8, 8)
end

return Weapon
