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

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



二六一.智能路灯(用蓝色护身符种智能路灯,夜晚自动点亮,白天熄灭,可手动开关、防雷击)

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

local function makestreetlight(inst)
	local pt = inst:GetPosition()
	local streetlight = SpawnPrefab("blueamulet")
	streetlight.Transform:SetPosition(pt.x, pt.y, pt.z)
	streetlight.AnimState:SetBank("nightstick")
	streetlight.AnimState:SetBuild("nightstick")
	streetlight.AnimState:PlayAnimation("idle")
	streetlight.Transform:SetScale(1.5, 1.5, 1.5)
	streetlight.AnimState:SetMultColour(255/255,255/255,0/255,1)
	MakeObstaclePhysics(streetlight, .5 )
	streetlight:RemoveComponent("equippable")
	streetlight:RemoveComponent("inventoryitem")
	streetlight:RemoveComponent("heater")
	streetlight:RemoveComponent("fueled")
	streetlight:RemoveComponent("deployable")
	streetlight:AddComponent("named")
	streetlight.components.named:SetName("Street Light")
	streetlight:AddComponent("workable")
	streetlight.components.workable:SetWorkAction(ACTIONS.HAMMER)
	streetlight.components.workable:SetWorkLeft(3)
	streetlight.components.workable:SetOnFinishCallback(function(streetlight, worker)
		SpawnPrefab("collapse_big").Transform:SetPosition(streetlight.Transform:GetWorldPosition())
		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
		local pos = Vector3(streetlight.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 2)
		for k,v in pairs(ents) do
			if v:HasTag("bulbs") then
			   v:Remove()
			end
		end
		streetlight:Remove()
	end )
	streetlight:AddTag("streetlights")
	local bulb = SpawnPrefab("blueamulet")
	bulb.AnimState:SetBank("bulb")
	bulb.AnimState:SetBuild("bulb")
	bulb.AnimState:PlayAnimation("idle")
	bulb.Transform:SetScale(1.6, 1.6, 1.6)
	bulb.Physics:SetActive(false)
	bulb:RemoveComponent("equippable")
	bulb:RemoveComponent("inventoryitem")
	bulb:RemoveComponent("heater")
	bulb:RemoveComponent("fueled")
	bulb:RemoveComponent("deployable")
	bulb.persists = false
	bulb:AddComponent("named")
	bulb.components.named:SetName("Street Light")
	bulb.entity:AddSoundEmitter()
	local light = bulb.entity:AddLight()
	light:SetFalloff(1)
	light:SetIntensity(.8)
	light:SetRadius(10)
	light:SetColour(255/255,255/255,255/255)
	light:Enable(false)  
	local follower = bulb.entity:AddFollower()
	follower:FollowSymbol( streetlight.GUID, "swap_object", 15, -160, 0.1 )
	bulb:AddComponent("machine")
	bulb.components.machine.turnonfn = function()
		bulb.SoundEmitter:PlaySound("dontstarve/wilson/lantern_on")
		bulb.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
		bulb.Light:Enable(true)
	end
	bulb.components.machine.turnofffn = function()
		bulb.SoundEmitter:PlaySound("dontstarve/wilson/lantern_off")
		bulb.AnimState:SetBloomEffectHandle( "" )
		bulb.Light:Enable(false)
	end
	bulb:ListenForEvent( "daytime", function() bulb.components.machine:TurnOff() end , GetWorld() )
	bulb:ListenForEvent( "dusktime", function() bulb.components.machine:TurnOn() end , GetWorld() )
	bulb:AddTag("lightningrod")
	bulb:AddTag("bulbs")
	if not GetClock():IsDay() then
	   bulb.components.machine:TurnOn()
	else
	   bulb.components.machine:TurnOff()
	end
end
local function OnDeploy (inst, pt)
	makestreetlight(inst)
	inst:Remove()
end
	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = OnDeploy
local function onsave(inst, data)
	if inst:HasTag("streetlights") then
	   data.streetlights = true
	end
end
local function onload(inst, data)
	if data and data.streetlights then
	   makestreetlight(inst)
	   inst:Remove()
	end
end
	inst.OnSave = onsave
	inst.OnLoad = onload

	即可用蓝色护身符种智能路灯,夜晚自动点亮,白天自动熄灭。鼠标右键点灯泡,可手动开关。智能路灯靠太阳能和闪电做能源,无须手动充能,靠近可防雷击。不想要智能路灯了,用锤子砸掉即可。蓝色护身符在魔法选项(画着红骷髅)下,用3个黄金、1个蓝宝石制造