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

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


  1
  2二五九.制冰机(用草伞种制冰机,下雨时自动制冰,可加工成大冰块永久保存,大冰块可砸碎成冰)
  3
  4	MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/umbrella.lua文件,在inst:AddTag("show_spoilage")的下一行插入以下内容:
  5
  6local function itemtest(inst, item, slot)
  7	if item.prefab == "ice" then
  8	   return true
  9	end
 10	return false
 11end
 12local slotpos = { Vector3(0,-75,0)}
 13local widgetbuttoninfo = {
 14	text = "Make",
 15	position = Vector3(0, -135, 0),
 16	fn = function(inst)
 17		if inst:HasTag("icemaker") then
 18		   if inst.components.container:Has("ice", 10) then
 19			  GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/rock_break")
 20			  inst.components.container:ConsumeByName("ice", 10)
 21			  local pt = inst:GetPosition()
 22			  local bigice = SpawnPrefab("grass_umbrella")
 23			  bigice.Transform:SetPosition(pt.x+(math.random(3)-math.random(3)), 0, pt.z+(math.random(3)-math.random(3)))
 24			  bigice.AnimState:SetBank("icehat")
 25			  bigice.AnimState:SetBuild("hat_ice")
 26			  bigice.AnimState:PlayAnimation("anim")
 27			  bigice.Transform:SetScale(1.5, 1.5, 1.5)
 28			  bigice:RemoveComponent("waterproofer")
 29			  bigice:RemoveComponent("equippable")
 30			  bigice:RemoveComponent("insulator")
 31			  bigice:RemoveComponent("perishable")
 32			  bigice:RemoveComponent("fuel")
 33			  bigice:RemoveComponent("burnable")
 34			  bigice:RemoveComponent("propagator")
 35			  bigice:RemoveComponent("deployable")
 36			  bigice:RemoveComponent("container")
 37			  bigice:RemoveTag("nopunch")
 38			  bigice:RemoveTag("show_spoilage")
 39			  bigice:RemoveAllEventCallbacks()
 40			  bigice:AddComponent("named")
 41			  bigice.components.named:SetName("Big ice")
 42			  bigice.components.inventoryitem:ChangeImageName("icehat")
 43			  bigice:AddComponent("heater")
 44			  bigice.components.heater.iscooler = true
 45			  bigice.components.heater.heatfn = function(bigice) return -40 end
 46			  bigice:AddComponent("workable")
 47			  bigice.components.workable:SetWorkAction(ACTIONS.HAMMER)
 48			  bigice.components.workable:SetWorkLeft(1)
 49			  bigice.components.workable:SetOnFinishCallback(function(bigice)
 50				  SpawnPrefab("ground_chunks_breaking").Transform:SetPosition(bigice.Transform:GetWorldPosition())
 51				  GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
 52				  for k = 1, 10 do
 53					  local pt1 = bigice:GetPosition()
 54					  local ice = SpawnPrefab("ice")
 55					  ice.Transform:SetPosition(pt1.x+(math.random(3)-math.random(3)), 0, pt1.z+(math.random(3)-math.random(3)))
 56				  end
 57				  bigice:Remove()
 58			  end )
 59			  bigice:AddTag("bigice")
 60		   end
 61		end
 62	end }
 63local function OnDeploy (inst, pt)
 64	local icemaker = SpawnPrefab("grass_umbrella")
 65	icemaker.Transform:SetPosition(pt.x, pt.y, pt.z)
 66	icemaker.AnimState:SetBank("eyeball_turret_base")
 67	icemaker.AnimState:SetBuild("eyeball_turret_base")
 68	icemaker.AnimState:PlayAnimation("idle_loop")
 69	icemaker.Transform:SetScale(0.8, 0.8, 0.8)
 70	icemaker.AnimState:SetMultColour(0/255,255/255,0/255,1)
 71	icemaker:RemoveComponent("waterproofer")
 72	icemaker:RemoveComponent("inventoryitem")
 73	icemaker:RemoveComponent("equippable")
 74	icemaker:RemoveComponent("insulator")
 75	icemaker:RemoveComponent("perishable")
 76	icemaker:RemoveComponent("fuel")
 77	icemaker:RemoveComponent("burnable")
 78	icemaker:RemoveComponent("propagator")
 79	icemaker:RemoveComponent("deployable")
 80	icemaker:RemoveTag("nopunch")
 81	icemaker:RemoveTag("show_spoilage")
 82	icemaker:RemoveAllEventCallbacks()
 83	icemaker:AddComponent("named")
 84	icemaker.components.named:SetName("Ice maker")
 85	icemaker.components.container.canbeopened = true
 86	icemaker:AddComponent("workable")
 87	icemaker.components.workable:SetWorkAction(ACTIONS.HAMMER)
 88	icemaker.components.workable:SetWorkLeft(3)
 89	icemaker.components.workable:SetOnFinishCallback(function(icemaker)
 90		SpawnPrefab("collapse_big").Transform:SetPosition(icemaker.Transform:GetWorldPosition())
 91		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
 92		icemaker:Remove()
 93	end )
 94	icemaker:ListenForEvent("rainstart", function()
 95		icemaker.task = icemaker:DoPeriodicTask(10, function()
 96			local ice = SpawnPrefab("ice")
 97			icemaker.components.container:GiveItem(ice)
 98		end )
 99	end, GetWorld())
100	icemaker:ListenForEvent("rainstop", function()
101		if icemaker.task then icemaker.task:Cancel() icemaker.task = nil end
102	end, GetWorld())
103	icemaker:AddTag("icemaker")
104	inst:Remove()
105end
106	inst:AddComponent("deployable")
107	inst.components.deployable.ondeploy = OnDeploy
108	inst:AddComponent("container")
109	inst.components.container:SetNumSlots(#slotpos)
110	inst.components.container.widgetslotpos = slotpos
111	inst.components.container.widgetpos = Vector3(0,150,0)
112	inst.components.container.side_align_tip = 160
113	inst.components.container.itemtestfn = itemtest
114	inst.components.container.widgetbuttoninfo = widgetbuttoninfo
115	inst.components.container.canbeopened = false
116	inst.components.container.onopenfn = function(inst) GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold") end
117	inst.components.container.onclosefn = function(inst) GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold") end
118	inst:AddTag("fridge")
119local function onsave(inst, data)
120	if inst:HasTag("icemaker") then
121		data.icemaker = true
122	end
123	if inst:HasTag("bigice") then
124		data.bigice = true
125	end
126end
127local function onload(inst, data)
128  if data and data.icemaker then
129	inst.AnimState:SetBank("eyeball_turret_base")
130	inst.AnimState:SetBuild("eyeball_turret_base")
131	inst.AnimState:PlayAnimation("idle_loop")
132	inst.Transform:SetScale(0.8, 0.8, 0.8)
133	inst.AnimState:SetMultColour(0/255,255/255,0/255,1)
134	inst:RemoveComponent("waterproofer")
135	inst:RemoveComponent("inventoryitem")
136	inst:RemoveComponent("equippable")
137	inst:RemoveComponent("insulator")
138	inst:RemoveComponent("perishable")
139	inst:RemoveComponent("fuel")
140	inst:RemoveComponent("burnable")
141	inst:RemoveComponent("propagator")
142	inst:RemoveComponent("deployable")
143	inst:RemoveTag("nopunch")
144	inst:RemoveTag("show_spoilage")
145	inst:RemoveAllEventCallbacks()
146	inst:AddComponent("named")
147	inst.components.named:SetName("Ice maker")
148	inst.components.container.canbeopened = true
149	inst:AddComponent("workable")
150	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
151	inst.components.workable:SetWorkLeft(3)
152	inst.components.workable:SetOnFinishCallback(function(inst)
153		SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition())
154		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
155		inst:Remove()
156	end )
157	inst:ListenForEvent("rainstart", function()
158		inst.task = inst:DoPeriodicTask(10, function()
159			local ice = SpawnPrefab("ice")
160			inst.components.container:GiveItem(ice)
161		end )
162	end, GetWorld())
163	inst:ListenForEvent("rainstop", function()
164		if inst.task then inst.task:Cancel() inst.task = nil end
165	end, GetWorld())
166	inst:AddTag("icemaker")
167  end
168  if data and data.bigice then
169	inst.AnimState:SetBank("icehat")
170	inst.AnimState:SetBuild("hat_ice")
171	inst.AnimState:PlayAnimation("anim")
172	inst.Transform:SetScale(1.5, 1.5, 1.5)
173	inst:RemoveComponent("waterproofer")
174	inst:RemoveComponent("equippable")
175	inst:RemoveComponent("insulator")
176	inst:RemoveComponent("perishable")
177	inst:RemoveComponent("fuel")
178	inst:RemoveComponent("burnable")
179	inst:RemoveComponent("propagator")
180	inst:RemoveComponent("deployable")
181	inst:RemoveComponent("container")
182	inst:RemoveTag("nopunch")
183	inst:RemoveTag("show_spoilage")
184	inst:RemoveAllEventCallbacks()
185	inst:AddComponent("named")
186	inst.components.named:SetName("Big ice")
187	inst.components.inventoryitem:ChangeImageName("icehat")
188	inst:AddComponent("heater")
189	inst.components.heater.iscooler = true
190	inst.components.heater.heatfn = function(inst) return -40 end
191	inst:AddComponent("workable")
192	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
193	inst.components.workable:SetWorkLeft(1)
194	inst.components.workable:SetOnFinishCallback(function(inst)
195		SpawnPrefab("ground_chunks_breaking").Transform:SetPosition(inst.Transform:GetWorldPosition())
196		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
197		for k = 1, 10 do
198			local pt1 = inst:GetPosition()
199			local ice = SpawnPrefab("ice")
200			ice.Transform:SetPosition(pt1.x+(math.random(3)-math.random(3)), 0, pt1.z+(math.random(3)-math.random(3)))
201		end
202		inst:Remove()
203	end )
204	inst:AddTag("bigice")
205  end
206end
207	inst.OnSave = onsave
208	inst.OnLoad = onload
209
210	即可用草伞种制冰机,下雨时每10秒制成一块冰,鼠标左键点制冰机,可打开格子,在格子中拿取。当格子中至少有10块冰时,点格子下面的“Make”按钮,可将冰加工成大冰块,用以永久保存。靠近大冰块可降温,用锤子砸大冰块,可还原成小冰。不想要制冰机了,用锤子炸毁即可。草伞在生存选项(画着绳套)下,用4个树枝、3个草、6个花瓣制造