require "prefabutil"

	local assets=
	{
		Asset("ANIM", "anim/zhaohunfan.zip"),

		Asset("ATLAS", "images/inventoryimages/zhaohunfan_item.xml"),
			Asset("IMAGE", "images/inventoryimages/zhaohunfan_item.tex"),
	}

local function onhammered(inst, worker)
	if inst.components.stewer and inst.components.stewer.product and inst.components.stewer.done then
		inst.components.lootdropper:AddChanceLoot(inst.components.stewer.product, 1)
	end
	inst.components.lootdropper:DropLoot()
	SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
	inst.SoundEmitter:PlaySound("dontstarve/common/destroy_metal")
	inst:Remove()
end

local function onhit(inst, worker)
	inst.AnimState:PlayAnimation("idle")

end



--anim and sound callbacks

local function onbuilt(inst)
	inst.AnimState:PlayAnimation("idle")
end

local function ondeploy(inst, pt, deployer)
	local pot = SpawnPrefab("zhaohunfan") 
	if pot then 
		pt = Vector3(pt.x, 0, pt.z)
		pot.Physics:SetCollides(false)
		pot.Physics:Teleport(pt.x, pt.y, pt.z) 
		pot.Physics:SetCollides(true)
		pot.SoundEmitter:PlaySound("dontstarve/common/place_structure_stone")
		pot.AnimState:PushAnimation("zhaohun", true)
		inst:RemoveComponent("inventoryitem")
		inst:Remove()
	end
end

local function pickupfn(inst)
	local player = GetPlayer()
	if player.components and player.components.inventory then
		--print("pickupfn true")
		local potitem = SpawnPrefab("zhaohunfan_item")
		player.components.inventory:GiveItem(potitem)
		inst:Remove()
		return true
	end
	--print("pickupfn false")
	inst.components.machine.ison = true
end

local function item_deploytest(inst, pt)
	local ground = GetWorld()
	local tile = GROUND.GRASS

	if ground and ground.Map then
		tile = ground.Map:GetTileAtPoint(pt:Get())
	end

	local onWater = ground.Map:IsWater(tile)

	return not onWater
end

local function item_droppedfn(inst)
	--If this is a valid place to be deployed, auto deploy yourself.
	if inst.components.deployable and inst.components.deployable:CanDeploy(inst:GetPosition()) then
		inst.components.deployable:Deploy(inst:GetPosition(), inst)
	end
end

local function itemfn(Sim)
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()

	MakeInventoryPhysics(inst)

	inst:AddTag("irreplaceable")

	inst.AnimState:SetBank("zhaohunfan")
	inst.AnimState:SetBuild("zhaohunfan")
	inst.AnimState:PlayAnimation("zhaohun", true)
	
	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "zhaohunfan_item.tex" )
	
	inst:AddComponent("inspectable")
	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "zhaohunfan_item"
	inst.components.inventoryitem.atlasname = "images/inventoryimages/zhaohunfan_item.xml"
	inst.components.inventoryitem:SetOnDroppedFn(item_droppedfn)

	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = ondeploy
	inst.components.deployable.placer = "zhaohunfan_placer"
	inst.components.deployable.test = item_deploytest

	--inst:AddComponent("characterspecific")
	--inst.components.characterspecific:SetOwner("nil")

	return inst
end

local function canpickup(inst)
	local stewer = inst.components.stewer
	local container = inst.components.container

	if not stewer then
		--Something has gone very wrong...
		return false
	end

	return container:IsEmpty() and not stewer.cooking and not stewer.product
end

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

	--local minimap = inst.entity:AddMiniMapEntity()
	--minimap:SetIcon( "zhaohunfan_item.tex" )

	local light = inst.entity:AddLight()
	inst.Light:Enable(false)
	inst.Light:SetRadius(1.5)
	inst.Light:SetFalloff(1)
	inst.Light:SetIntensity(.5)
	inst.Light:SetColour(235/255,62/255,12/255)

	inst:AddTag("structure")
	MakeObstaclePhysics(inst, .5)

	inst.AnimState:SetBank("zhaohunfan")
	inst.AnimState:SetBuild("zhaohunfan")
	inst.AnimState:PlayAnimation("zhaohun", true)

	--inst:AddComponent("stewer")
	--inst.components.stewer:SetCookerName("cookpot")
	--inst.components.stewer.onstartcooking = startcookfn
	--inst.components.stewer.oncontinuecooking = continuecookfn
	--inst.components.stewer.oncontinuedone = continuedonefn
	--inst.components.stewer.ondonecooking = donecookfn
	--inst.components.stewer.onharvest = harvestfn
	--inst.components.stewer.onspoil = spoilfn


	inst:AddComponent("inspectable")
	--inst.components.inspectable.getstatus = getstatus
	
	inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(function(inst, item) 
	return item:HasTag("zhaohuncailiao") end)
	
    inst.components.trader.onaccept=function(inst,giver,item)

local x,y,z = inst.Transform:GetWorldPosition()	

       if item.prefab=="flower_hua" then
local assasin = SpawnPrefab("assasin_wilson")
local pop = SpawnPrefab("explode_small")
local drakeangle = math.random(1, 360)
local offset = FindWalkableOffset(inst:GetPosition(), drakeangle*DEGREES, math.random(2,8), 30, false, false)
pop.Transform:SetScale(1.25, 1.25, 1.25)
assasin.Transform:SetPosition(x + offset.x, y + offset.y, z + offset.z)
pop.Transform:SetPosition(assasin:GetPosition():Get())
SpawnPrefab("statue_transition_2").Transform:SetPosition(assasin:GetPosition():Get()) 
	   elseif item.prefab=="flower_seed" then
local assasin2 = SpawnPrefab("assasin_kikyou")
assasin2.Transform:SetPosition(x+2, y+2, z+15)  
	   
	   --else
	   
	   
	   end

	end
    inst.components.trader.onrefuse=function(inst,giver,item) 
	if giver.components.talker then 
	   giver.components.talker:Say("需要放入招魂材料") end 
	end	



	MakeSnowCovered(inst, .01)
	inst:ListenForEvent( "onbuilt", onbuilt)

	inst:AddComponent("machine")
	inst.components.machine.ison = true
	inst.components.machine.turnofffn = pickupfn
	--inst.components.machine.caninteractfn = canpickup

	--inst:AddComponent("characterspecific")
	--inst.components.characterspecific:SetOwner("nil")

	return inst
end


return Prefab( "common/zhaohunfan", fn, assets, prefabs),
			 MakePlacer( "common/zhaohunfan_placer", "zhaohunfan", "zhaohunfan", "zhaohun", true ),
			 Prefab("common/zhaohunfan_item", itemfn, assets, prefabs)
