local assets =
{
    Asset("ANIM", "anim/lunararmor.zip"),
    Asset("ATLAS", "inventoryimages/lunararmor.xml"),
	Asset("IMAGE", "inventoryimages/lunararmor.tex"),
}



local function OnBlocked(owner)


end

local absorbchance = 0.25
local function OnTakeDamage(inst, damage_amount)
if damage_amount > 1 then
  local owner = inst.components.inventoryitem.owner
if math.random() < absorbchance then
    if owner then
        local health = owner.components.health
        if health then
               owner.sg:GoToState("hit")
            local actualdamage = damage_amount * .20
            health:DoDelta(-actualdamage, false)
        end
    end
	else
	owner.SoundEmitter:PlaySound("dontstarve/wilson/hit_armour")

end
end
end




function PercentChanged(inst, data)

end


local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "lunararmor", "swap_body")
    inst:ListenForEvent("blocked", OnBlocked, owner)
    inst.base_applydamage = owner.components.inventory.ApplyDamage
    function owner.components.inventory:ApplyDamage(damage,...)
      if math.random() < absorbchance then
        return 0
      end
      return  inst.base_applydamage(self,damage,...)
    end

end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")
    inst:RemoveEventCallback("blocked", OnBlocked, owner)
    owner.components.inventory.ApplyDamage = inst.base_applydamage
end

local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("armor_marble")
    inst.AnimState:SetBuild("lunararmor")
    inst.AnimState:PlayAnimation("anim")

	  inst:AddTag("waterproofer")
    inst.foleysound = "dontstarve/movement/foley/nightarmour"
    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
	  inst.components.inventoryitem.imagename = "lunararmor"
    inst.components.inventoryitem.atlasname = "inventoryimages/lunararmor.xml"

    inst:AddComponent("armor")
    inst.components.armor:InitCondition(4500, 1.5)

    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.BODY
	   inst.components.equippable.dapperness = TUNING.DAPPERNESS_SMALL
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    MakeHauntableLaunch(inst)

	inst:AddTag("waterproofer")
	inst:AddComponent("waterproofer")
    inst.components.waterproofer:SetEffectiveness(TUNING.WATERPROOFNESS_LARGE)

    return inst
end

return Prefab("lunararmor", fn, assets)
