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

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



一八六.智能围墙(按键盘F11键造一圈大理石围墙,点围墙可开门,5秒自动关门,夜晚启动照明)

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

local function GetStatus(inst, viewer)
	local pt = inst:GetPosition()
	inst:Remove()
	inst:DoTaskInTime(5, function() 
		SpawnPrefab("marblepillar").Transform:SetPosition(pt.x, pt.y, pt.z)
		inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup")
	end)
end
local function LightsOn(inst)
	inst.Light:Enable(true)
end
local function LightsOff(inst)
	inst.Light:Enable(false)
end
	local light = inst.entity:AddLight()
	light:SetFalloff(1)
	light:SetIntensity(.8)
	light:SetRadius(10)
	light:Enable(false)
	light:SetColour(180/255, 195/255, 50/255)
	inst.components.inspectable.getstatus = GetStatus
	inst:ListenForEvent( "daytime", function() LightsOff(inst) end, GetWorld())
	inst:ListenForEvent( "dusktime", function() LightsOn(inst) end, GetWorld())


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

TheInput:AddKeyUpHandler(KEY_F11, function()
	local player = GetPlayer()
	local pt = Vector3(player.Transform:GetWorldPosition())
		for k = 1, 75 do
			local theta = 1 * 2 * PI
			local radius = 18
			local result_offset = FindValidPositionByFan(theta, radius, 75, function(offset)
				local x,y,z = (pt + offset):Get()
				local ents = TheSim:FindEntities(x,y,z , 1)
				return not next(ents) 
			end)
			if result_offset then
				local tentacle = SpawnPrefab("marblepillar")
				tentacle.Transform:SetPosition((pt + result_offset):Get())
				GetPlayer().components.playercontroller:ShakeCamera(inst, "FULL", 0.2, 0.02, .25, 40)
			end
			inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup")
		end
end)
TheInput:AddKeyUpHandler(KEY_F12, function()
	local player = GetPlayer()
	local range = 30
	local pos = Vector3(player.Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, range)
	for k,v in pairs(ents) do
		if v.prefab == "marblepillar" then
		   v:Remove()
		end
	end
	inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup")
end)

	即可在开阔的空地上按键盘F11键,建造一圈大理石围墙,按F12键可消去。对围墙按鼠标左键可开门,5秒后自动关门,夜晚自动启动照明系统,用作自宅围墙或圈养动物都适宜。注意不要拿着镐点围墙,除非你想获得大理石