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

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


  1
  2二六一.智能路灯(用蓝色护身符种智能路灯,夜晚自动点亮,白天熄灭,可手动开关、防雷击)
  3
  4	MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/amulet.lua文件,在inst.AnimState:PlayAnimation("blueamulet")的下一行插入以下内容:
  5
  6local function makestreetlight(inst)
  7	local pt = inst:GetPosition()
  8	local streetlight = SpawnPrefab("blueamulet")
  9	streetlight.Transform:SetPosition(pt.x, pt.y, pt.z)
 10	streetlight.AnimState:SetBank("nightstick")
 11	streetlight.AnimState:SetBuild("nightstick")
 12	streetlight.AnimState:PlayAnimation("idle")
 13	streetlight.Transform:SetScale(1.5, 1.5, 1.5)
 14	streetlight.AnimState:SetMultColour(255/255,255/255,0/255,1)
 15	MakeObstaclePhysics(streetlight, .5 )
 16	streetlight:RemoveComponent("equippable")
 17	streetlight:RemoveComponent("inventoryitem")
 18	streetlight:RemoveComponent("heater")
 19	streetlight:RemoveComponent("fueled")
 20	streetlight:RemoveComponent("deployable")
 21	streetlight:AddComponent("named")
 22	streetlight.components.named:SetName("Street Light")
 23	streetlight:AddComponent("workable")
 24	streetlight.components.workable:SetWorkAction(ACTIONS.HAMMER)
 25	streetlight.components.workable:SetWorkLeft(3)
 26	streetlight.components.workable:SetOnFinishCallback(function(streetlight, worker)
 27		SpawnPrefab("collapse_big").Transform:SetPosition(streetlight.Transform:GetWorldPosition())
 28		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
 29		local pos = Vector3(streetlight.Transform:GetWorldPosition())
 30		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 2)
 31		for k,v in pairs(ents) do
 32			if v:HasTag("bulbs") then
 33			   v:Remove()
 34			end
 35		end
 36		streetlight:Remove()
 37	end )
 38	streetlight:AddTag("streetlights")
 39	local bulb = SpawnPrefab("blueamulet")
 40	bulb.AnimState:SetBank("bulb")
 41	bulb.AnimState:SetBuild("bulb")
 42	bulb.AnimState:PlayAnimation("idle")
 43	bulb.Transform:SetScale(1.6, 1.6, 1.6)
 44	bulb.Physics:SetActive(false)
 45	bulb:RemoveComponent("equippable")
 46	bulb:RemoveComponent("inventoryitem")
 47	bulb:RemoveComponent("heater")
 48	bulb:RemoveComponent("fueled")
 49	bulb:RemoveComponent("deployable")
 50	bulb.persists = false
 51	bulb:AddComponent("named")
 52	bulb.components.named:SetName("Street Light")
 53	bulb.entity:AddSoundEmitter()
 54	local light = bulb.entity:AddLight()
 55	light:SetFalloff(1)
 56	light:SetIntensity(.8)
 57	light:SetRadius(10)
 58	light:SetColour(255/255,255/255,255/255)
 59	light:Enable(false)  
 60	local follower = bulb.entity:AddFollower()
 61	follower:FollowSymbol( streetlight.GUID, "swap_object", 15, -160, 0.1 )
 62	bulb:AddComponent("machine")
 63	bulb.components.machine.turnonfn = function()
 64		bulb.SoundEmitter:PlaySound("dontstarve/wilson/lantern_on")
 65		bulb.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
 66		bulb.Light:Enable(true)
 67	end
 68	bulb.components.machine.turnofffn = function()
 69		bulb.SoundEmitter:PlaySound("dontstarve/wilson/lantern_off")
 70		bulb.AnimState:SetBloomEffectHandle( "" )
 71		bulb.Light:Enable(false)
 72	end
 73	bulb:ListenForEvent( "daytime", function() bulb.components.machine:TurnOff() end , GetWorld() )
 74	bulb:ListenForEvent( "dusktime", function() bulb.components.machine:TurnOn() end , GetWorld() )
 75	bulb:AddTag("lightningrod")
 76	bulb:AddTag("bulbs")
 77	if not GetClock():IsDay() then
 78	   bulb.components.machine:TurnOn()
 79	else
 80	   bulb.components.machine:TurnOff()
 81	end
 82end
 83local function OnDeploy (inst, pt)
 84	makestreetlight(inst)
 85	inst:Remove()
 86end
 87	inst:AddComponent("deployable")
 88	inst.components.deployable.ondeploy = OnDeploy
 89local function onsave(inst, data)
 90	if inst:HasTag("streetlights") then
 91	   data.streetlights = true
 92	end
 93end
 94local function onload(inst, data)
 95	if data and data.streetlights then
 96	   makestreetlight(inst)
 97	   inst:Remove()
 98	end
 99end
100	inst.OnSave = onsave
101	inst.OnLoad = onload
102
103	即可用蓝色护身符种智能路灯,夜晚自动点亮,白天自动熄灭。鼠标右键点灯泡,可手动开关。智能路灯靠太阳能和闪电做能源,无须手动充能,靠近可防雷击。不想要智能路灯了,用锤子砸掉即可。蓝色护身符在魔法选项(画着红骷髅)下,用3个黄金、1个蓝宝石制造