代码来源于“易宁”大佬的分享,仅供学习,不要直接复制粘贴。
原帖链接:http://bbs.3dmgame.com/thread-3859071-1-1.html
1
2二一八.潘多拉魔盒(用硬骨头种潘多拉魔盒,每60秒有一批怪物来袭,展开塔防吧)
3
4 用MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/boneshard.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:
5
6local function makemagicbox(inst)
7 local pt = inst:GetPosition()
8 local magicbox = SpawnPrefab("boneshard")
9 magicbox.Transform:SetPosition(pt.x, pt.y, pt.z)
10 magicbox.AnimState:SetBank("pandoras_chest")
11 magicbox.AnimState:SetBuild("pandoras_chest")
12 magicbox.AnimState:PlayAnimation("closed")
13 magicbox.AnimState:SetMultColour(255/255,0/255,0/255,1)
14 magicbox.Transform:SetScale(0.8, 0.8, 0.8)
15 magicbox.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
16 local minimap = magicbox.entity:AddMiniMapEntity()
17 minimap:SetIcon( "pandoras_chest.png" )
18 magicbox:RemoveComponent("inventoryitem")
19 magicbox:RemoveComponent("stackable")
20 magicbox:RemoveComponent("deployable")
21 magicbox:AddTag("magicbox")
22 magicbox:AddComponent("workable")
23 magicbox.components.workable:SetWorkAction(ACTIONS.HAMMER)
24 magicbox.components.workable:SetWorkLeft(3)
25 magicbox.components.workable:SetOnFinishCallback(function(magicbox)
26 local pos = Vector3(magicbox.Transform:GetWorldPosition())
27 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3000)
28 for k,v in pairs(ents) do
29 if v:HasTag("bogey") then
30 SpawnPrefab("collapse_big").Transform:SetPosition(v.Transform:GetWorldPosition())
31 v:Remove()
32 end
33 end
34 SpawnPrefab("collapse_big").Transform:SetPosition(magicbox.Transform:GetWorldPosition())
35 GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
36 magicbox:Remove()
37 end )
38 magicbox.components.inspectable.getstatus = function(magicbox)
39 if not magicbox:HasTag("makebogey") then
40 magicbox:AddTag("makebogey")
41 magicbox.AnimState:PlayAnimation("open")
42 magicbox:DoTaskInTime(0.5, function(magicbox)
43 SpawnPrefab("collapse_small").Transform:SetPosition(magicbox.Transform:GetWorldPosition())
44 GetPlayer().SoundEmitter:PlaySound("dontstarve/common/horn_beefalo")
45 GetPlayer().components.playercontroller:ShakeCamera(magicbox, "FULL", 2.5, 0.03, 2, 30)
46 local pos = Vector3(magicbox.Transform:GetWorldPosition())
47 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3000)
48 for k,v in pairs(ents) do
49 if v:HasTag("bogey") then
50 SpawnPrefab("collapse_big").Transform:SetPosition(v.Transform:GetWorldPosition())
51 v:Remove()
52 end
53 end
54 local names = {"spiderqueen","hound","firehound","icehound","killerbee","walrus","leif","leif_sparse","koalefant_summer","koalefant_winter","deerclops","merm","knight","bishop","bat"}
55 magicbox.name = names[math.random(#names)]
56 for k = 1,math.random(10,30) do
57 local pt0 = magicbox:GetPosition()
58 local ground = GetWorld()
59 local x = pt0.x+(math.random(50)-math.random(50))
60 local z = pt0.z+(math.random(50)-math.random(50))
61 local tile = ground.Map:GetTileAtPoint(x,0, z)
62 if tile ~= GROUND.IMPASSABLE and tile ~= GROUND.INVALID then
63 local bogey = SpawnPrefab(magicbox.name)
64 bogey.Transform:SetPosition(x,0,z)
65 bogey:AddTag("bogey")
66 bogey.components.combat:SetRetargetFunction(1, function(bogey)
67 if not bogey.components.health:IsDead() then
68 return FindEntity(bogey, 100, function(guy)
69 if guy.components.health and not guy.components.health:IsDead() then
70 return guy:HasTag("player")
71 end
72 end )
73 end
74 end )
75 bogey.components.combat:SetKeepTargetFunction(function(bogey, target) return target and target:IsValid() end )
76 bogey:ListenForEvent("attacked", function(bogey, data)
77 bogey.components.combat:SetTarget(GetPlayer())
78 end )
79 end
80 end
81 end )
82 magicbox.task = magicbox:DoPeriodicTask(60, function(magicbox)
83 SpawnPrefab("collapse_small").Transform:SetPosition(magicbox.Transform:GetWorldPosition())
84 GetPlayer().SoundEmitter:PlaySound("dontstarve/common/horn_beefalo")
85 GetPlayer().components.playercontroller:ShakeCamera(magicbox, "FULL", 2.5, 0.03, 2, 30)
86 local pos = Vector3(magicbox.Transform:GetWorldPosition())
87 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3000)
88 for k,v in pairs(ents) do
89 if v:HasTag("bogey") then
90 SpawnPrefab("collapse_big").Transform:SetPosition(v.Transform:GetWorldPosition())
91 v:Remove()
92 end
93 end
94 local names = {"spiderqueen","hound","firehound","icehound","killerbee","walrus","leif","leif_sparse","koalefant_summer","koalefant_winter","deerclops","merm","knight","bishop","bat"}
95 magicbox.name = names[math.random(#names)]
96 for k = 1,math.random(10,30) do
97 local pt0 = magicbox:GetPosition()
98 local ground = GetWorld()
99 local x = pt0.x+(math.random(50)-math.random(50))
100 local z = pt0.z+(math.random(50)-math.random(50))
101 local tile = ground.Map:GetTileAtPoint(x,0, z)
102 if tile ~= GROUND.IMPASSABLE and tile ~= GROUND.INVALID then
103 local bogey = SpawnPrefab(magicbox.name)
104 bogey.Transform:SetPosition(x,0,z)
105 bogey:AddTag("bogey")
106 bogey.components.combat:SetRetargetFunction(1, function(bogey)
107 if not bogey.components.health:IsDead() then
108 return FindEntity(bogey, 100, function(guy)
109 if guy.components.health and not guy.components.health:IsDead() then
110 return guy:HasTag("player")
111 end
112 end )
113 end
114 end )
115 bogey.components.combat:SetKeepTargetFunction(function(bogey, target) return target and target:IsValid() end )
116 bogey:ListenForEvent("attacked", function(bogey, data)
117 bogey.components.combat:SetTarget(GetPlayer())
118 end )
119 end
120 end
121 end )
122 else
123 magicbox:RemoveTag("makebogey")
124 magicbox.AnimState:PlayAnimation("close")
125 GetPlayer().SoundEmitter:PlaySound("dontstarve/HUD/get_gold")
126 if magicbox.task then magicbox.task:Cancel() magicbox.task = nil end
127 local pos = Vector3(magicbox.Transform:GetWorldPosition())
128 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3000)
129 for k,v in pairs(ents) do
130 if v:HasTag("bogey") then
131 SpawnPrefab("collapse_big").Transform:SetPosition(v.Transform:GetWorldPosition())
132 v:Remove()
133 end
134 end
135 end
136 end
137end
138local function OnDeploy (inst, pt)
139 makemagicbox(inst)
140 inst.components.stackable:Get():Remove()
141end
142 inst:AddComponent("deployable")
143 inst.components.deployable.ondeploy = OnDeploy
144local function onsave(inst, data)
145 if inst:HasTag("magicbox") then
146 data.magicbox = true
147 end
148end
149local function onload(inst, data)
150 if data and data.magicbox then
151 makemagicbox(inst)
152 inst:Remove()
153 end
154end
155 inst.OnSave = onsave
156 inst.OnLoad = onload
157
158 即可用硬骨头种潘多拉魔盒,(拿着1个硬骨头对地面点鼠标右键,如果拿着多个,则不会种出来)。鼠标左键点魔盒,可打开盒子,每60秒将吹响一次号角,预示着新一批怪物来袭,且源源不绝,充分满足你求死不得的需要。鼠标左键再次点击魔盒,可让怪物回到盒中。建议围绕魔盒修建防御设施(如牙齿陷阱、眼睛炮塔等),否则你挺不过几个60秒的。潘多拉魔盒在小地图上显示为远古箱子图标,不想要潘多拉魔盒了,用锤子砸掉即可。硬骨头靠砸碎地图上的骨架获得