代码来源于“易宁”大佬的分享,仅供学习,不要直接复制粘贴。
原帖链接:http://bbs.3dmgame.com/thread-3859071-1-1.html
1
2二九八.我的果汁店(用治疗药膏种果汁店,自己制作果汁,顾客上门购买)
3
4 用MT管理器打开游戏目录/assets/scripts/prefabs/healingsalve.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:
5
6local function itemtest(inst, item, slot)
7 if item.prefab == "cave_banana" or item.prefab == "pomegranate" or item.prefab == "dragonfruit" then
8 return true
9 end
10 return false
11end
12local slotpos = { Vector3(0,32+4,0), Vector3(0,-(32+4),0), Vector3(0,-(64+32+8+4),0)}
13local widgetbuttoninfo = {
14 text = "Start",
15 position = Vector3(0, -165, 0),
16 fn = function(inst)
17 if inst:HasTag("juicer") and inst.components.container:Has("cave_banana", 1) and inst.components.container:Has("pomegranate", 1) and inst.components.container:Has("dragonfruit", 1) then
18 inst.components.container:ConsumeByName("cave_banana", 1)
19 inst.components.container:ConsumeByName("pomegranate", 1)
20 inst.components.container:ConsumeByName("dragonfruit", 1)
21 GetPlayer().SoundEmitter:PlaySound("dontstarve/HUD/research_available")
22 local sweets = SpawnPrefab("healingsalve")
23 sweets.AnimState:SetBank("food")
24 sweets.AnimState:SetBuild("cook_pot_food")
25 sweets.AnimState:PlayAnimation("fruitmedley", false)
26 sweets:RemoveComponent("stackable")
27 sweets:RemoveComponent("deployable")
28 sweets.components.healer:SetHealthAmount(TUNING.HEALING_SUPERHUGE)
29 sweets.components.inventoryitem:ChangeImageName("fruitmedley")
30 sweets.Transform:SetScale(1.2, 1.2, 1.2)
31 sweets:AddTag("sweets")
32 GetPlayer().components.inventory:GiveItem(sweets)
33 end
34 end }
35 inst:AddComponent("container")
36 inst.components.container.widgetbuttoninfo = widgetbuttoninfo
37 inst.components.container:SetNumSlots(#slotpos)
38 inst.components.container.widgetslotpos = slotpos
39 inst.components.container.widgetpos = Vector3(0,180,0)
40 inst.components.container.side_align_tip = 160
41 inst.components.container.itemtestfn = itemtest
42 inst.components.container.canbeopened = false
43local function sweetshop(inst)
44 local pt = inst:GetPosition()
45 local shop = SpawnPrefab("healingsalve")
46 shop.Transform:SetPosition(pt.x, pt.y, pt.z)
47 shop.AnimState:SetBank("rabbithouse")
48 shop.AnimState:SetBuild("rabbit_house")
49 shop.AnimState:PlayAnimation("idle", true)
50 shop.Transform:SetScale(0.8, 0.8, 0.8)
51 shop:RemoveComponent("stackable")
52 shop:RemoveComponent("inventoryitem")
53 shop:RemoveComponent("healer")
54 shop:RemoveComponent("deployable")
55 local light = shop.entity:AddLight()
56 light:SetFalloff(1)
57 light:SetIntensity(.8)
58 light:SetRadius(5)
59 light:SetColour(180/255, 195/255, 50/255)
60 light:Enable(true)
61 MakeLargeBurnable(shop)
62 MakeLargePropagator(shop)
63 shop:AddTag("shop")
64 shop:DoPeriodicTask(2, function(shop)
65 shop.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
66 shop:DoTaskInTime(1, function() shop.AnimState:SetBloomEffectHandle("") end )
67 end )
68 shop:AddComponent("trader")
69 shop.components.trader:SetAcceptTest(function(shop, item)
70 if item.prefab == "goldnugget" then
71 return true
72 end
73 return false
74 end )
75 shop.components.trader.onaccept = function(shop, giver, item)
76 local names = {"cave_banana","pomegranate","dragonfruit"}
77 local name = names[math.random(#names)]
78 local veggie = SpawnPrefab(name)
79 if item.prefab == "goldnugget" then
80 giver.components.inventory:GiveItem(veggie)
81 end
82 end
83 local counter = SpawnPrefab("healingsalve")
84 counter.Transform:SetPosition(pt.x+2+1.8, 0, pt.z+2-1.8)
85 counter.AnimState:SetBank("winter_meter")
86 counter.AnimState:SetBuild("winter_meter")
87 counter.AnimState:SetPercent("meter", 0)
88 counter.Transform:SetScale(1.2, 1.2, 1.2)
89 counter.AnimState:SetOrientation( ANIM_ORIENTATION.OnGround )
90 counter.AnimState:SetLayer( LAYER_BACKGROUND )
91 counter.AnimState:SetSortOrder( 1 )
92 counter.Transform:SetRotation( 135 )
93 counter.AnimState:SetMultColour(255/255,255/255,255/255,0)
94 counter:RemoveComponent("stackable")
95 counter:RemoveComponent("inventoryitem")
96 counter:RemoveComponent("healer")
97 counter:RemoveComponent("deployable")
98 MakeLargeBurnable(counter)
99 MakeLargePropagator(counter)
100 counter:AddTag("NOCLICK")
101 counter:AddTag("goodbye")
102 local juicer = SpawnPrefab("healingsalve")
103 juicer.Transform:SetPosition(pt.x+1-2, 0, pt.z+1+2)
104 juicer.AnimState:SetBank("researchlab2")
105 juicer.AnimState:SetBuild("researchlab2")
106 juicer.AnimState:PlayAnimation("idle")
107 juicer.Transform:SetScale(0.8, 0.8, 0.8)
108 juicer.AnimState:SetMultColour(255/255,105/255,0/255,1)
109 juicer:RemoveComponent("stackable")
110 juicer:RemoveComponent("inventoryitem")
111 juicer:RemoveComponent("healer")
112 juicer:RemoveComponent("deployable")
113 juicer.components.container.canbeopened = true
114 MakeLargeBurnable(juicer)
115 MakeLargePropagator(juicer)
116 juicer:AddTag("juicer")
117 juicer:AddTag("goodbye")
118 local sweetsign = SpawnPrefab("healingsalve")
119 sweetsign.Transform:SetPosition(pt.x+1+2, 0, pt.z+1-2)
120 sweetsign.AnimState:SetBank("sign_home")
121 sweetsign.AnimState:SetBuild("sign_home")
122 sweetsign.AnimState:PlayAnimation("idle")
123 sweetsign.Transform:SetScale(0.8, 0.8, 0.8)
124 sweetsign.AnimState:SetMultColour(255/255,0/255,0/255,1)
125 MakeLargeBurnable(sweetsign)
126 MakeLargePropagator(sweetsign)
127 sweetsign:AddTag("goodbye")
128 sweetsign:RemoveComponent("stackable")
129 sweetsign:RemoveComponent("inventoryitem")
130 sweetsign:RemoveComponent("healer")
131 sweetsign:RemoveComponent("deployable")
132 sweetsign:ListenForEvent( "nighttime", function()
133 sweetsign:RemoveTag("openshop")
134 GetPlayer().SoundEmitter:KillSound("beavermusic")
135 sweetsign.AnimState:SetMultColour(255/255,0/255,0/255,1)
136 local pos = Vector3(sweetsign.Transform:GetWorldPosition())
137 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3000)
138 for k,v in pairs(ents) do
139 if v:HasTag("customers") then
140 v:Remove()
141 end
142 end
143 end, GetWorld())
144 sweetsign.components.inspectable.getstatus = function(sweetsign)
145 if not sweetsign:HasTag("openshop") then
146 local target0 = FindEntity(sweetsign, 10, function(guy)
147 return guy:HasTag("sweets") and guy.components.inventoryitem and not guy.components.inventoryitem:IsHeld()
148 end )
149 if target0 then
150 sweetsign:AddTag("openshop")
151 GetPlayer().SoundEmitter:PlaySound("dontstarve/music/music_hoedown", "beavermusic")
152 sweetsign.AnimState:SetMultColour(0/255,255/255,0/255,1)
153 for k = 1, math.random(8,16) do
154 local pt1 = sweetsign:GetPosition()
155 local customer = SpawnPrefab("healingsalve")
156 customer.Transform:SetPosition(pt1.x+(math.random(30)-math.random(30)), 0, pt1.z+(math.random(30)-math.random(30)))
157 customer.AnimState:SetBank("wilson")
158 local names = {"wilson","wendy","wes","wickerbottom","willow","wolfgang","wx78"}
159 local buildname = names[math.random(#names)]
160 customer.AnimState:SetBuild(buildname)
161 local hats = {"hat_bee","hat_beefalo","hat_bush","hat_earmuffs","hat_feather","hat_flower","hat_football","hat_miner","hat_ruins","hat_slurper","hat_slurtle","hat_spider","hat_straw","hat_top","hat_walrus","hat_winter"}
162 local hat = hats[math.random(#hats)]
163 customer.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat")
164 local armors = {"armor_grass","armor_marble","armor_onemanband","armor_ruins","armor_sanity","armor_slurper","armor_slurtleshell","armor_sweatervest","armor_trunkvest_summer","armor_trunkvest_winter","armor_wood"}
165 local armor = armors[math.random(#armors)]
166 customer.AnimState:OverrideSymbol("swap_body", armor, "swap_body")
167 customer.AnimState:Show("HAT")
168 customer.AnimState:Show("HAT_HAIR")
169 customer.AnimState:Hide("HAIR_NOHAT")
170 customer.AnimState:Hide("HAIR")
171 customer.AnimState:Hide("ARM_carry")
172 customer.AnimState:Show("ARM_normal")
173 customer.AnimState:PlayAnimation("idle")
174 customer.Transform:SetFourFaced()
175 local sound = customer.entity:AddSoundEmitter()
176 local shadow = customer.entity:AddDynamicShadow()
177 shadow:SetSize( 1.3, .6 )
178 customer:RemoveComponent("stackable")
179 customer:RemoveComponent("inventoryitem")
180 customer:RemoveComponent("healer")
181 customer:RemoveComponent("deployable")
182 customer:AddComponent("locomotor")
183 customer.components.locomotor.walkspeed = 4
184 customer.components.locomotor.runspeed = 8
185 customer:SetStateGraph("SGshadowwaxwell")
186 local brain = require "brains/frogbrain"
187 customer:SetBrain(brain)
188 customer:AddComponent("health")
189 customer.components.health:SetMaxHealth(1000)
190 customer.components.health:SetInvincible(true)
191 customer.components.health.nofadeout = true
192 customer:AddComponent("combat")
193 customer:AddComponent("knownlocations")
194 customer:AddTag("customers")
195 customer:AddTag("goodbye")
196 customer.task1 = customer:DoPeriodicTask(3, function(customer)
197 if math.random() < 0.1 then
198 local target = FindEntity(customer, 100, function(guy)
199 return guy:HasTag("sweets") and guy.components.inventoryitem and not guy.components.inventoryitem:IsHeld()
200 end )
201 if target then
202 customer.components.locomotor:Stop()
203 customer:SetBrain(nil)
204 customer.components.locomotor:GoToEntity(target)
205 customer.task2 = customer:DoPeriodicTask(.25, function(customer)
206 local target2 = FindEntity(customer, 1, function(guy)
207 return guy:HasTag("sweets") and guy.components.inventoryitem and not guy.components.inventoryitem:IsHeld()
208 end )
209 if target2 then
210 customer.AnimState:PlayAnimation("pickup")
211 for k = 1, 6 do
212 SpawnPrefab("goldnugget").Transform:SetPosition(target2.Transform:GetWorldPosition())
213 end
214 target2:Remove()
215 local brain = require "brains/frogbrain"
216 customer:SetBrain(brain)
217 customer:RestartBrain()
218 if customer.task2 then customer.task2:Cancel() customer.task2 = nil end
219 end
220 end )
221 customer:DoTaskInTime(8, function(customer)
222 local brain = require "brains/frogbrain"
223 customer:SetBrain(brain)
224 customer:RestartBrain()
225 if customer.task2 then customer.task2:Cancel() customer.task2 = nil end
226 end )
227 else
228 local brain = require "brains/frogbrain"
229 customer:SetBrain(brain)
230 customer:RestartBrain()
231 if customer.task2 then customer.task2:Cancel() customer.task2 = nil end
232 end
233 end
234 end )
235 end
236 end
237 else
238 sweetsign:RemoveTag("openshop")
239 GetPlayer().SoundEmitter:KillSound("beavermusic")
240 sweetsign.AnimState:SetMultColour(255/255,0/255,0/255,1)
241 local pos = Vector3(sweetsign.Transform:GetWorldPosition())
242 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3000)
243 for k,v in pairs(ents) do
244 if v:HasTag("customers") then
245 v:Remove()
246 end
247 end
248 end
249 end
250end
251local function OnDeploy (inst, pt)
252 sweetshop(inst)
253 inst.components.stackable:Get():Remove()
254end
255 inst:AddComponent("deployable")
256 inst.components.deployable.ondeploy = OnDeploy
257local function onsave(inst, data)
258 if inst:HasTag("shop") then
259 data.shop = true
260 end
261 if inst:HasTag("goodbye") then
262 data.goodbye = true
263 end
264 if inst:HasTag("sweets") then
265 data.sweets = true
266 end
267end
268local function onload(inst, data)
269 if data and data.shop then
270 sweetshop(inst)
271 inst:Remove()
272 end
273 if data and data.goodbye then
274 inst:Remove()
275 end
276 if data and data.sweets then
277 inst.AnimState:SetBank("food")
278 inst.AnimState:SetBuild("cook_pot_food")
279 inst.AnimState:PlayAnimation("fruitmedley", false)
280 inst:RemoveComponent("stackable")
281 inst:RemoveComponent("deployable")
282 inst.components.healer:SetHealthAmount(TUNING.HEALING_SUPERHUGE)
283 inst.components.inventoryitem:ChangeImageName("fruitmedley")
284 inst.Transform:SetScale(1.2, 1.2, 1.2)
285 inst:AddTag("sweets")
286 end
287end
288 inst.OnSave = onsave
289 inst.OnLoad = onload
290
291 即可用治疗药膏种我的果汁店(手里拿着1个治疗药膏对地面点鼠标右键,如果拿着多个治疗药膏,则不会种出来),拿着黄金对库房(大胡萝卜)点鼠标左键,可以购入果汁原料,分别是火龙果、香蕉、石榴,每个水果1个黄金,当然你也可以用自己种的水果。将三种水果放入右边的榨汁机(拿着水果对榨汁机点鼠标左键),然后按Start按钮,将开始榨汁,榨完后果汁自动装入主角物品栏。将果汁放在柜台上(地上半透明的长方形),点左边的牌子就可以开张了(柜台上没有果汁则无法开张),会有顾客上门购买,每份售价6个黄金。想打烊,就再次点击牌子即可。黑夜来临时会自动打烊,回家休息吧,当然你也可以再次点牌子开张,但夜里的顾客都昏昏欲睡。出售的果汁自己也可以喝(在物品栏里对果汁点鼠标右键),可大幅补充生命值。不想要果汁店时,烧掉即可,记得先打烊哦。治疗药膏在生存选项(画着绳套)下,用2个灰、1个石头、1个蜘蛛腺体制造