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

local prefabs = {
	"archer",
	"archer_bakuya",
	--"groundpound_fx",
	"caster_ring_fx",
	
}


-----------------重定位目标？猪哥说的，不懂----------------
local function RetargetFn(inst)
	local kingtarget = nil
	kingtarget = FindEntity(inst, 30, function(guy)
		return guy:HasTag("monster")  
	end)


	return kingtarget
end

local function KeepTargetFn(inst, target)
    return inst.components.combat:CanTarget(target)     
end

-------------------------------------------------------------

------------------猪哥吊炸天的碰撞系统--------------------------
local function oncollide(inst, other)
	if not other then
		return
	else
		if not other:IsValid() then
			return
		end
		----遇到障碍物就跳跃
		if inst.components.health and not inst.components.health:IsDead() and not (other:HasTag("shadowboss") or other:HasTag("projectile") or other:HasTag("character")) then
			if not inst.sg:HasStateTag("attack") then
				--inst.sg:GoToState("jump")
			end
		end
	end
end

------------------------------------------------------------------------------
------------------攻击的特殊效果------------------------------------------------------


local function onattack(inst,data)
    local victim = data.target
if victim then
	local x,y,z = victim:GetPosition():Get()
	SpawnPrefab("collapse_big").Transform:SetPosition(x,y,z)
	SpawnPrefab("archer_bakuya").Transform:SetPosition(x,y,z)
	SpawnPrefab("archer_bakuya").Transform:SetPosition(x+3,y,z)
	SpawnPrefab("archer_bakuya").Transform:SetPosition(x-3,y,z)
	SpawnPrefab("archer_bakuya").Transform:SetPosition(x,y+3,z)
	SpawnPrefab("archer_bakuya").Transform:SetPosition(x,y-3,z)
	SpawnPrefab("archer_bakuya").Transform:SetPosition(x,y,z+3)
	SpawnPrefab("archer_bakuya").Transform:SetPosition(x,y,z-3)
end
	if victim and victim:HasTag("monster") then
		----僵直一下
		if not victim.components.health:IsDead() then
			victim.sg:GoToState("idle")
		end
	end

	local target = data.target
	inst.components.combat:ShareTarget(target, 100, function(dude)
		return dude:HasTag("gate_of_babylon") and dude.components.follower.leader == inst.components.follower.leader
	end, 20)

end

------------------------------------------------------------------------

local function OnAttacked(inst, data)
	local attacker = data.attacker
    inst.components.combat:SetTarget(attacker)
    inst.components.combat:ShareTarget(attacker, 100, function(dude)
		return dude:HasTag("gate_of_babylon") and dude.components.follower.leader == inst.components.follower.leader
	end, 20)
end


-----------------以下是否应该和function onattack 合并 ，这是个问题，等继续做就知道了-----------
--local function OnAttackOther(inst, data)
--	local target = data.target
--	inst.components.combat:ShareTarget(target, 100, function(dude)
--		return dude:HasTag("gate_of_babylon") and dude.components.follower.leader == inst.components.follower.leader
--	end, 20)
--end


------------------------------------------------------------------------------
-------------------------------------------------------------------------------
--local function OnEquip(inst, owner)

 --   owner.AnimState:OverrideSymbol("swap_object", "swap_gkatana", "swap_gkatana")
 --   owner.AnimState:Hide("ARM_carry")
  --  owner.AnimState:Show("ARM_normal")

--end
  
--local function OnUnequip(inst, owner)
   

  --  owner.AnimState:Hide("ARM_carry")
   -- owner.AnimState:Show("ARM_normal")
--end
--local function onhit(inst, owner,target)
 --   OnDropped(inst)
 --   local pos = Vector3(owner.Transform:GetWorldPosition())
--end
--local function OnDropped(inst)
 --   inst.AnimState:PlayAnimation("idle")
--end
--local function OnThrown(inst, owner)
--inst.AnimState:PlayAnimation("planted")
--end

local function OnCaught(inst, catcher)
    if catcher then
        if catcher.components.inventory then
            if inst.components.equippable and not catcher.components.inventory:GetEquippedItem(inst.components.equippable.equipslot) then
                catcher.components.inventory:Equip(inst)
            else
                catcher.components.inventory:GiveItem(inst)
            end
            catcher:PushEvent("catch")
        end
    end
end

local function OnDropped(inst)
 --   inst.sg:GoToState("catchbreath")
    if inst.components.workable ~= nil then
        inst.components.workable:SetWorkLeft(1)
    end
   -- if inst.brain ~= nil then
    --    inst.brain:Start()
 --   end
   -- if inst.sg ~= nil then
   --     inst.sg:Start()
   -- end
    if inst.components.stackable ~= nil and inst.components.stackable:IsStack() then
        local x, y, z = inst.Transform:GetWorldPosition()
        while inst.components.stackable:IsStack() do
            local item = inst.components.stackable:Get()
            if item ~= nil then
                if item.components.inventoryitem ~= nil then
                    item.components.inventoryitem:OnDropped()
                end
                item.Physics:Teleport(x, y, z)
            end
        end
    end
end



------------------------创建实体--------------------------------------
---------------------------------------------------------------------
local function fn()
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddMiniMapEntity()
	inst.entity:AddSoundEmitter()
	inst.entity:AddDynamicShadow()
	inst.entity:AddNetwork()

	inst.entity:AddSoundEmitter()
------------------物理系统-----------------------------------------------
	local phys = inst.entity:AddPhysics()
	phys:SetMass(100)
    phys:SetCapsule(.5, 1.5)
    phys:SetFriction(0)
    phys:SetDamping(5)
    phys:SetCollisionGroup(COLLISION.CHARACTERS)
    phys:ClearCollisionMask()
	phys:CollidesWith(COLLISION.GROUND)
	phys:CollidesWith(COLLISION.OBSTACLES)
    phys:CollidesWith(COLLISION.SMALLOBSTACLES)
	phys:CollidesWith(COLLISION.CHARACTERS)
---------------------------------------------------------------------
	inst.DynamicShadow:SetSize(1, 1)
--------------------------------------------------------------------------	
	inst.Transform:SetFourFaced()
	inst.Transform:SetScale(1, 1, 1)

	inst.AnimState:SetBank("wilson")
	inst.AnimState:SetBuild("gate_of_babylon")
	inst.AnimState:PlayAnimation("idle")
	--inst.AnimState:Hide("ARM_carry")
	--inst.AnimState:Show("ARM_normal")
	inst.AnimState:Show("HAT")
	inst.AnimState:Hide("HAT_HAIR")
	inst.AnimState:Show("HAIR_NOHAT")
	inst.AnimState:Hide("HAIR")
	--inst.AnimState:OverrideSymbol("swap_body", "torso_dragonfly", "swap_body")


----------------添加标签-------------------------------------
	inst:AddTag("gate_of_babylon")
	inst:AddTag("king")
	--inst:AddTag("")   --备用
------------------------------------------------------------------
------------------联机版作用不明的代码---------------------------
	inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end 
----------------------碰撞-------------------------------------------
    inst.Physics:SetCollisionCallback(oncollide)
----------------------------------------------------------------

----	基本属性设置
    inst:AddComponent("locomotor")
    inst.components.locomotor.walkspeed= 5
    inst.components.locomotor.runspeed = 5
	inst.components.locomotor.fasteronroad = true
	inst.components.locomotor:SetTriggersCreep(false)


	inst:SetStateGraph("SGgate_of_babylon")
	local brain = require "brains/gate_of_babylonbrain"		
    inst:SetBrain(brain)

    inst:AddComponent("health")
    inst.components.health:SetMaxHealth(100)
	inst.components.health:StartRegen(30, 1)
	inst.components.health.fire_damage_scale = 0

	inst.components.health:SetAbsorptionAmount(.5)

-----------------------------------------------------------------------	
	MakeMediumBurnableCharacter(inst, "pig_torso")
------------------------------------------------------------------
-----------------战斗设置--------------------------------------
	inst:AddComponent("combat")
    inst.components.combat:SetDefaultDamage(30)						
    inst.components.combat:SetAttackPeriod(0.5)
    inst.components.combat:SetRange(100)
    inst.components.combat.hiteffectsymbol = "torso"
    inst.components.combat:SetRetargetFunction(2, RetargetFn)
    inst.components.combat:SetKeepTargetFunction(KeepTargetFn)

    --攻击特效
	inst:ListenForEvent("onattackother", onattack)

	----	监听事件
    inst:ListenForEvent("attacked", OnAttacked)

    inst:AddComponent("inspectable")
	inst.components.inspectable:SetDescription("")
--------------------------------------------------------------

	inst:AddComponent("stackable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.canbepickedup = true
    inst.components.inventoryitem.canbepickedupalive = true
    inst.components.inventoryitem.nobounce = true
        --  inst.components.inventoryitem.imagename = "bakuya"
    --inst.components.inventoryitem.atlasname = "images/inventoryimages/bakuya.xml"
     inst.components.inventoryitem.imagename = "gkatana"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/gkatana.xml"
-------------------------------------------------------------
    inst:AddComponent("follower")
  
    --inst.OnSave = OnSave
	--inst.OnLoad = OnLoad
---------------------------------------------------------------
    --inst:AddComponent("equippable")
    --inst.components.equippable:SetOnEquip( OnEquip )
    --inst.components.equippable:SetOnUnequip( OnUnequip )
	--inst.components.inventoryitem.keepondeath = true
	--inst.components.equippable.equipstack = true
  
    --inst:AddComponent("weapon")
    --inst.components.weapon:SetDamage(5)

       -- inst:AddComponent("projectile")
    --inst.components.projectile:SetSpeed(20)
   -- inst.components.projectile:SetCanCatch(false)
   -- inst.components.projectile:SetHoming(false)
   -- inst.components.projectile:SetOnThrownFn(OnThrown)
   -- inst.components.projectile:SetOnHitFn(onhit)
  --  inst.components.projectile:SetHitDist(3)
   -- inst.components.projectile:SetOnCaughtFn(OnCaught)

   inst:ListenForEvent("ondropped", OnDropped)

	return inst
end
	
return Prefab("gate_of_babylon", fn, assets, prefabs)