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

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



二四一.我的小屋(用巨鹿眼球种小屋,左键点击可睡觉,右键点击打开库房,点按钮可修理装备、恢复食物新鲜、烘干物品)

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

local slotpos = {}
for y = 2, 0, -1 do
	for x = 0, 11 do
		table.insert(slotpos, Vector3(75*x-75*2+75, 75*y-75*2+75,0))
		table.insert(slotpos, Vector3(75*x-75*2+75, 75*y-75*2-175,0))
		table.insert(slotpos, Vector3(75*x-75*2+75, 75*y-75*2-425,0))
	end
end
local widgetbuttoninfo = {
	text = "Repair",
	position = Vector3(741, 135, 0),
	fn = function(inst)
		GetPlayer().SoundEmitter:PlaySound("dontstarve/HUD/research_available")
		inst.components.container:Close(GetPlayer())
		for k,v in pairs(inst.components.container.slots) do
			if v.components.fueled then
			   v.components.fueled.currentfuel = v.components.fueled.maxfuel
			end
			if v.components.finiteuses then
			   v.components.finiteuses.current = v.components.finiteuses.total
			end
			if v.components.armor then
			   v.components.armor.condition = v.components.armor.maxcondition
			end
			if v.components.perishable then
			   v.components.perishable.perishremainingtime = v.components.perishable.perishtime
			end
			if v.components.moisturelistener then
			   v.components.moisturelistener.wet = false
			end
		end
		inst:DoTaskInTime(0.1, function() inst.components.container:Open(GetPlayer()) end )
end }
local function OnDeploy (inst, pt)
	local house = SpawnPrefab("deerclops_eyeball")
	house.Transform:SetPosition(pt.x, pt.y, pt.z)
	house.AnimState:SetBank("walrus_house")
	house.AnimState:SetBuild("walrus_house")
	house.AnimState:PlayAnimation("lit", true)
	house.Transform:SetScale(1.5, 1.5, 1.5)
	MakeObstaclePhysics(house, 1)
	local minimap = house.entity:AddMiniMapEntity()
	minimap:SetIcon( "igloo.png" )
	house:RemoveComponent("inventoryitem")
	house:RemoveComponent("edible")
	house:RemoveComponent("stackable")
	house:RemoveComponent("deployable")
	local light = house.entity:AddLight()
	light:SetFalloff(1)
	light:SetIntensity(.8)
	light:SetRadius(10)
	light:Enable(true)
	light:SetColour(180/255, 195/255, 50/255)
	house:AddComponent("named")
	house.components.named:SetName("My House")
	house:AddComponent("workable")
	house.components.workable:SetWorkAction(ACTIONS.HAMMER)
	house.components.workable:SetWorkLeft(3)
	house.components.workable:SetOnFinishCallback(function(house)
		house.Light:Enable(false)
		SpawnPrefab("collapse_big").Transform:SetPosition(house.Transform:GetWorldPosition())
		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
	house.components.container:DropEverything()
		house:Remove()
	end )
	house:AddComponent("machine")
	house.components.machine.turnonfn = function() house.components.container:Open(GetPlayer()) end
	house.components.machine.turnofffn = function() house.components.container:Open(GetPlayer()) end
	house:AddComponent("sleepingbag")
	house.components.sleepingbag.onsleep = function(house, sleeper)
		sleeper.components.health:SetInvincible(true)
		sleeper.components.playercontroller:Enable(false)
		GetPlayer().HUD:Hide()
		TheFrontEnd:Fade(false,1)
		house:DoTaskInTime(1.2, function() 
		   GetPlayer().HUD:Show()
		   TheFrontEnd:Fade(true,1) 
		   GetClock():MakeNextDay()
		   sleeper.components.health:SetInvincible(false)
		   sleeper.components.playercontroller:Enable(true)
		   sleeper.components.sanity:DoDelta(sleeper.components.sanity.max)
		   sleeper.components.hunger:DoDelta(sleeper.components.hunger.max)
		   sleeper.components.health:DoDelta(sleeper.components.health.maxhealth)
		   sleeper.components.temperature:SetTemperature(20)
		   if sleeper.components.moisture then sleeper.components.moisture:SetMoistureLevel(0) end
		end)
	end
	house:AddTag("houses")
	inst.components.stackable:Get():Remove()
end
	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = OnDeploy
local function onsave(inst, data)
	if inst:HasTag("houses") then
		data.houses = true
	end
end
local function onload(inst, data)
  if data and data.houses then
	inst.AnimState:SetBank("walrus_house")
	inst.AnimState:SetBuild("walrus_house")
	inst.AnimState:PlayAnimation("lit", true)
	inst.Transform:SetScale(1.5, 1.5, 1.5)
	MakeObstaclePhysics(inst, 1)
	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon( "igloo.png" )
	inst:RemoveComponent("inventoryitem")
	inst:RemoveComponent("edible")
	inst:RemoveComponent("stackable")
	inst:RemoveComponent("deployable")
	local light = inst.entity:AddLight()
	light:SetFalloff(1)
	light:SetIntensity(.8)
	light:SetRadius(10)
	light:Enable(true)
	light:SetColour(180/255, 195/255, 50/255)
	inst:AddComponent("named")
	inst.components.named:SetName("My House")
	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
	inst.components.workable:SetWorkLeft(3)
	inst.components.workable:SetOnFinishCallback(function(inst)
		inst.Light:Enable(false)
		SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition())
		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
	inst.components.container:DropEverything()
		inst:Remove()
	end )
	inst:AddComponent("machine")
	inst.components.machine.turnonfn = function() inst.components.container:Open(GetPlayer()) end
	inst.components.machine.turnofffn = function() inst.components.container:Open(GetPlayer()) end
	inst:AddComponent("sleepingbag")
	inst.components.sleepingbag.onsleep = function(inst, sleeper)
		sleeper.components.health:SetInvincible(true)
		sleeper.components.playercontroller:Enable(false)
		GetPlayer().HUD:Hide()
		TheFrontEnd:Fade(false,1)
		inst:DoTaskInTime(1.2, function() 
		   GetPlayer().HUD:Show()
		   TheFrontEnd:Fade(true,1) 
		   GetClock():MakeNextDay()
		   sleeper.components.health:SetInvincible(false)
		   sleeper.components.playercontroller:Enable(true)
		   sleeper.components.sanity:DoDelta(sleeper.components.sanity.max)
		   sleeper.components.hunger:DoDelta(sleeper.components.hunger.max)
		   sleeper.components.health:DoDelta(sleeper.components.health.maxhealth)
		   sleeper.components.temperature:SetTemperature(20)
		   if sleeper.components.moisture then sleeper.components.moisture:SetMoistureLevel(0) end
		end)
	end
	inst:AddTag("houses")
  end
end
	inst.OnSave = onsave
	inst.OnLoad = onload
	inst:AddComponent("container")
	inst.components.container:SetNumSlots(#slotpos)
	inst.components.container.widgetslotpos = slotpos
	inst.components.container.widgetpos = Vector3(-150,300,0)
	inst.components.container.side_align_tip = 160
	inst.components.container.widgetbuttoninfo = widgetbuttoninfo
	inst.components.container.canbeopened = false
	inst:AddTag("fridge")

	即可用巨鹿眼球种小屋,鼠标左键点小屋可睡觉(白天也可以睡),睡醒后会将脑、饥饿、生命全部补满,去除湿度,并解除中暑、冻伤状态。鼠标右键点击小屋,会打开库房(远离小屋自动关闭),共108格存储空间,具有冷藏功能。点格子右上角的“Repair”按钮,可修理小屋中的装备、恢复食物新鲜度、烘干物品。小屋日夜灯火通明,会照亮周边。不想要小屋时,用锤子砸掉即可,如果库房没有清空,里面的物品会掉在地上