require "prefabutil"

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

local prefabs =
{
}

local function onhammered(inst, worker)
	inst.components.lootdropper:DropLoot()
	SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
	inst.SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
	inst:Remove()
end

local function onhit(inst, worker)
	inst.AnimState:PlayAnimation("hit")
	inst.AnimState:PushAnimation("idle", true)
end

local function onbuilt(inst)
	inst.AnimState:PlayAnimation("place")
	inst.AnimState:PushAnimation("idle", true)
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddMiniMapEntity()
	inst.entity:AddNetwork()

	inst.MiniMapEntity:SetIcon("fyjiedeng.tex")

	MakeObstaclePhysics(inst, .5)

	local light = inst.entity:AddLight()
	inst.Light:SetRadius(7.5)
    inst.Light:SetFalloff(0.85)
    inst.Light:SetIntensity(.85)
    inst.Light:SetColour(100/255, 250/255, 100/255)
    inst.Light:Enable(true)

	anim:SetBank("fyjiedeng")
	anim:SetBuild("fyjiedeng")
	anim:PlayAnimation("idle")

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddTag("structure")
	inst:AddComponent("lootdropper")
	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
	inst.components.workable:SetWorkLeft(4)
	inst.components.workable:SetOnFinishCallback(onhammered)
	inst.components.workable:SetOnWorkCallback(onhit)
	local old_Destroy = inst.components.workable.Destroy
	function inst.components.workable:Destroy(destroyer)
	if destroyer.components.playercontroller == nil then return	end
	return old_Destroy(self,destroyer)
	end

	inst:AddComponent("inspectable")

	MakeSnowCovered(inst, .01)

return inst
end


return Prefab( "common/fyjiedeng", fn, assets, prefabs ),
MakePlacer("common/fyjiedeng_placer", "fyjiedeng", "fyjiedeng", "idle")
