代码来源于“易宁”大佬的分享,仅供学习,不要直接复制粘贴。

原帖链接:http://bbs.3dmgame.com/thread-3859071-1-1.html



一七二.溜溜球(用陷阱种溜溜球,连续打击多个目标,打完自动回到手中)

	用MT管理器打开游戏目录/assets/scripts/prefabs/trap.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:

local colours=
{
	{198/255,43/255,43/255},
	{79/255,153/255,68/255},
	{35/255,105/255,235/255},
	{233/255,208/255,69/255},
	{109/255,50/255,163/255},
	{222/255,126/255,39/255},
}
local function OnDeploy (inst, pt)
	local ball = SpawnPrefab("trap")
	ball.Transform:SetPosition(pt.x, pt.y, pt.z)
	ball.AnimState:SetBank("bulb")
	ball.AnimState:SetBuild("bulb")
	ball.AnimState:PlayAnimation("idle")
	ball.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
	ball:ClearStateGraph()
	RemovePhysicsColliders(ball)
	ball.components.inventoryitem:ChangeImageName("lightbulb")
	ball.colour_idx = math.random(#colours)
	ball.AnimState:SetMultColour(colours[ball.colour_idx][1],colours[ball.colour_idx][2],colours[ball.colour_idx][3],1)
	ball:RemoveComponent("finiteuses")
	ball:RemoveComponent("trap")
	ball:RemoveComponent("deployable")
	ball:RemoveTag("trap")
	ball:AddComponent("weapon")
	ball.components.weapon:SetDamage(30)
	ball.components.weapon:SetRange(20, 25)
	ball:AddComponent("equippable")
	ball.components.equippable.equipslot = EQUIPSLOTS.HANDS
	ball.components.equippable:SetOnEquip(function(ball)
		ball.SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
	end )
	ball:AddComponent("projectile")
	ball.components.projectile:SetSpeed(25)
	ball.components.projectile:SetOnHitFn(function(ball, owner, target)
		GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
		local target0 = FindEntity(owner, 25, function(guy) 
			if guy.components.combat and guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("player") then
			   return guy.components.combat.target == owner or owner.components.combat.target == guy or guy:HasTag("monster")
			end
		end )
		if target0 then
		   ball.components.projectile:Throw(owner, target0)
		else
		   owner.components.inventory:Equip(ball)
		end
	end )
	ball.components.projectile:SetOnMissFn(function(ball, owner)
		owner.components.inventory:Equip(ball)
	end )
	ball:ListenForEvent("onthrown", function(ball)
		local pt = ball:GetPosition()
		ball.Transform:SetPosition(pt.x, 2, pt.z)
		ball.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_throw")
	end )
	ball:AddComponent("workable")
	ball.components.workable:SetWorkAction(ACTIONS.HAMMER)
	ball.components.workable:SetWorkLeft(3)
	ball.components.workable:SetOnFinishCallback(function(ball)
		SpawnPrefab("ground_chunks_breaking").Transform:SetPosition(ball.Transform:GetWorldPosition())
		ball:Remove()
	end )
	ball:AddTag("projectile")
	ball:AddTag("balls")
	inst:Remove()
end
	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = OnDeploy
local function onsave(inst, data)
	if inst:HasTag("balls") then
	   data.balls = true
	end
	data.colour_idx = inst.colour_idx
end
local function onload(inst, data)
  if data and data.balls then
	inst.AnimState:SetBank("bulb")
	inst.AnimState:SetBuild("bulb")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
	inst:ClearStateGraph()
	RemovePhysicsColliders(inst)
	inst.components.inventoryitem:ChangeImageName("lightbulb")
	inst:RemoveComponent("finiteuses")
	inst:RemoveComponent("trap")
	inst:RemoveComponent("deployable")
	inst:RemoveTag("trap")
	inst:AddComponent("weapon")
	inst.components.weapon:SetDamage(30)
	inst.components.weapon:SetRange(20, 25)
	inst:AddComponent("equippable")
	inst.components.equippable.equipslot = EQUIPSLOTS.HANDS
	inst.components.equippable:SetOnEquip(function(inst)
		inst.SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
	end )
	inst:AddComponent("projectile")
	inst.components.projectile:SetSpeed(25)
	inst.components.projectile:SetOnHitFn(function(inst, owner, target)
		GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
		local target0 = FindEntity(owner, 25, function(guy) 
			if guy.components.combat and guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("player") then
			   return guy.components.combat.target == owner or owner.components.combat.target == guy or guy:HasTag("monster")
			end
		end )
		if target0 then
		   inst.components.projectile:Throw(owner, target0)
		else
		   owner.components.inventory:Equip(inst)
		end
	end )
	inst.components.projectile:SetOnMissFn(function(inst, owner)
		owner.components.inventory:Equip(inst)
	end )
	inst:ListenForEvent("onthrown", function(inst)
		local pt = inst:GetPosition()
		inst.Transform:SetPosition(pt.x, 2, pt.z)
		inst.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_throw")
	end )
	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
	inst.components.workable:SetWorkLeft(3)
	inst.components.workable:SetOnFinishCallback(function(inst)
		SpawnPrefab("ground_chunks_breaking").Transform:SetPosition(inst.Transform:GetWorldPosition())
		inst:Remove()
	end )
	inst:AddTag("projectile")
	inst:AddTag("balls")
  end
  if data and data.colour_idx then
	 inst.colour_idx = math.min(#colours, data.colour_idx)
	 inst.AnimState:SetMultColour(colours[inst.colour_idx][1],colours[inst.colour_idx][2],colours[inst.colour_idx][3],1)
  end
end
	inst.OnSave = onsave
	inst.OnLoad = onload

	即可用陷阱种溜溜球(颜色随机),装备后对敌人按鼠标左键,可以扔出并连续打击附近所有敌人,消灭敌人后自动回到手中,完美代替回旋镖。不想要溜溜球时,用锤子砸掉即可。陷阱在生存选项(画着绳套)下,用2个树枝、6个草制造