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

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


  1
  2一七二.溜溜球(用陷阱种溜溜球,连续打击多个目标,打完自动回到手中)
  3
  4	MT管理器打开游戏目录/assets/scripts/prefabs/trap.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:
  5
  6local colours=
  7{
  8	{198/255,43/255,43/255},
  9	{79/255,153/255,68/255},
 10	{35/255,105/255,235/255},
 11	{233/255,208/255,69/255},
 12	{109/255,50/255,163/255},
 13	{222/255,126/255,39/255},
 14}
 15local function OnDeploy (inst, pt)
 16	local ball = SpawnPrefab("trap")
 17	ball.Transform:SetPosition(pt.x, pt.y, pt.z)
 18	ball.AnimState:SetBank("bulb")
 19	ball.AnimState:SetBuild("bulb")
 20	ball.AnimState:PlayAnimation("idle")
 21	ball.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
 22	ball:ClearStateGraph()
 23	RemovePhysicsColliders(ball)
 24	ball.components.inventoryitem:ChangeImageName("lightbulb")
 25	ball.colour_idx = math.random(#colours)
 26	ball.AnimState:SetMultColour(colours[ball.colour_idx][1],colours[ball.colour_idx][2],colours[ball.colour_idx][3],1)
 27	ball:RemoveComponent("finiteuses")
 28	ball:RemoveComponent("trap")
 29	ball:RemoveComponent("deployable")
 30	ball:RemoveTag("trap")
 31	ball:AddComponent("weapon")
 32	ball.components.weapon:SetDamage(30)
 33	ball.components.weapon:SetRange(20, 25)
 34	ball:AddComponent("equippable")
 35	ball.components.equippable.equipslot = EQUIPSLOTS.HANDS
 36	ball.components.equippable:SetOnEquip(function(ball)
 37		ball.SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
 38	end )
 39	ball:AddComponent("projectile")
 40	ball.components.projectile:SetSpeed(25)
 41	ball.components.projectile:SetOnHitFn(function(ball, owner, target)
 42		GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
 43		local target0 = FindEntity(owner, 25, function(guy) 
 44			if guy.components.combat and guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("player") then
 45			   return guy.components.combat.target == owner or owner.components.combat.target == guy or guy:HasTag("monster")
 46			end
 47		end )
 48		if target0 then
 49		   ball.components.projectile:Throw(owner, target0)
 50		else
 51		   owner.components.inventory:Equip(ball)
 52		end
 53	end )
 54	ball.components.projectile:SetOnMissFn(function(ball, owner)
 55		owner.components.inventory:Equip(ball)
 56	end )
 57	ball:ListenForEvent("onthrown", function(ball)
 58		local pt = ball:GetPosition()
 59		ball.Transform:SetPosition(pt.x, 2, pt.z)
 60		ball.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_throw")
 61	end )
 62	ball:AddComponent("workable")
 63	ball.components.workable:SetWorkAction(ACTIONS.HAMMER)
 64	ball.components.workable:SetWorkLeft(3)
 65	ball.components.workable:SetOnFinishCallback(function(ball)
 66		SpawnPrefab("ground_chunks_breaking").Transform:SetPosition(ball.Transform:GetWorldPosition())
 67		ball:Remove()
 68	end )
 69	ball:AddTag("projectile")
 70	ball:AddTag("balls")
 71	inst:Remove()
 72end
 73	inst:AddComponent("deployable")
 74	inst.components.deployable.ondeploy = OnDeploy
 75local function onsave(inst, data)
 76	if inst:HasTag("balls") then
 77	   data.balls = true
 78	end
 79	data.colour_idx = inst.colour_idx
 80end
 81local function onload(inst, data)
 82  if data and data.balls then
 83	inst.AnimState:SetBank("bulb")
 84	inst.AnimState:SetBuild("bulb")
 85	inst.AnimState:PlayAnimation("idle")
 86	inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
 87	inst:ClearStateGraph()
 88	RemovePhysicsColliders(inst)
 89	inst.components.inventoryitem:ChangeImageName("lightbulb")
 90	inst:RemoveComponent("finiteuses")
 91	inst:RemoveComponent("trap")
 92	inst:RemoveComponent("deployable")
 93	inst:RemoveTag("trap")
 94	inst:AddComponent("weapon")
 95	inst.components.weapon:SetDamage(30)
 96	inst.components.weapon:SetRange(20, 25)
 97	inst:AddComponent("equippable")
 98	inst.components.equippable.equipslot = EQUIPSLOTS.HANDS
 99	inst.components.equippable:SetOnEquip(function(inst)
100		inst.SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
101	end )
102	inst:AddComponent("projectile")
103	inst.components.projectile:SetSpeed(25)
104	inst.components.projectile:SetOnHitFn(function(inst, owner, target)
105		GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
106		local target0 = FindEntity(owner, 25, function(guy) 
107			if guy.components.combat and guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("player") then
108			   return guy.components.combat.target == owner or owner.components.combat.target == guy or guy:HasTag("monster")
109			end
110		end )
111		if target0 then
112		   inst.components.projectile:Throw(owner, target0)
113		else
114		   owner.components.inventory:Equip(inst)
115		end
116	end )
117	inst.components.projectile:SetOnMissFn(function(inst, owner)
118		owner.components.inventory:Equip(inst)
119	end )
120	inst:ListenForEvent("onthrown", function(inst)
121		local pt = inst:GetPosition()
122		inst.Transform:SetPosition(pt.x, 2, pt.z)
123		inst.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_throw")
124	end )
125	inst:AddComponent("workable")
126	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
127	inst.components.workable:SetWorkLeft(3)
128	inst.components.workable:SetOnFinishCallback(function(inst)
129		SpawnPrefab("ground_chunks_breaking").Transform:SetPosition(inst.Transform:GetWorldPosition())
130		inst:Remove()
131	end )
132	inst:AddTag("projectile")
133	inst:AddTag("balls")
134  end
135  if data and data.colour_idx then
136	 inst.colour_idx = math.min(#colours, data.colour_idx)
137	 inst.AnimState:SetMultColour(colours[inst.colour_idx][1],colours[inst.colour_idx][2],colours[inst.colour_idx][3],1)
138  end
139end
140	inst.OnSave = onsave
141	inst.OnLoad = onload
142
143	即可用陷阱种溜溜球(颜色随机),装备后对敌人按鼠标左键,可以扔出并连续打击附近所有敌人,消灭敌人后自动回到手中,完美代替回旋镖。不想要溜溜球时,用锤子砸掉即可。陷阱在生存选项(画着绳套)下,用2个树枝、6个草制造