local make_sound = defense_mob_api.library.make_sound

local do_observers = defense_mob_api.library.set_up_observers("register_on_punch")



function defense_mob_api.library.on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)

	do_observers(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)

	-- Weapon wear code adapted from TenPlus1's mobs redo (https://github.com/tenplus1/mobs)
	if puncher
	then
		local weapon = puncher:get_wielded_item()
		if tool_capabilities
		then
			local wear = (0.01) * (self.armor / 100) * 65534 + 1
			weapon:add_wear(wear)
			puncher:set_wielded_item(weapon)
		end
	end
	make_sound(self, "hurt")

	dir.y = dir.y + 1
	local m = self.mass or 1
	local knockback = vector.multiply(vector.normalize(dir), 10 / (1 + m))
	self.object:setvelocity(vector.add(self.object:getvelocity(), knockback))
	self.pause_timer = 0.3
	
	if self.object:get_hp() - damage <= 0
	then
		self:die(puncher)
	end
end
