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

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



二二0.象之树(用紫色护身符种象之树,白天周围有巨象群活动,狩猎它们吧)

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

local function makebananatree(inst)
	local pt = inst:GetPosition()
	local bananatree = SpawnPrefab("purpleamulet")
	bananatree.Transform:SetPosition(pt.x, pt.y, pt.z)
	bananatree.AnimState:SetBank("cave_banana_tree")
	bananatree.AnimState:SetBuild("cave_banana_tree")
	bananatree.AnimState:PlayAnimation("idle_loop",true)
	bananatree.Transform:SetScale(1.5, 1.5, 1.5)
	bananatree:AddComponent("named")
	bananatree.components.named:SetName("Banana Tree")
	bananatree:RemoveComponent("equippable")
	bananatree:RemoveComponent("inventoryitem")
	bananatree:RemoveComponent("fueled")
	bananatree:RemoveComponent("deployable")
	bananatree:AddComponent("workable")
	bananatree.components.workable:SetWorkAction(ACTIONS.CHOP)
	bananatree.components.workable:SetWorkLeft(10)
	bananatree.components.workable:SetOnFinishCallback(function()
		SpawnPrefab("collapse_big").Transform:SetPosition(bananatree.Transform:GetWorldPosition())
		GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
		bananatree:Remove()
	end )
	bananatree:ListenForEvent( "daytime", function()
		local pos = Vector3(bananatree.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 5)
		for k,v in pairs(ents) do
			if v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then
			   if v.prefab == "cave_banana" then
				  v:Remove()
			   end
			end
		end
		bananatree:DoTaskInTime(0.3, function(bananatree)
			bananatree:StartThread(function()
				for k = 1,math.random(4,8) do
					local pt1 = bananatree:GetPosition()
					local banana = SpawnPrefab("cave_banana")
					banana.Transform:SetPosition(pt1.x+(math.random(5)-math.random(5)), 5, pt1.z+(math.random(5)-math.random(5)))
					Sleep(0.3)
				end
			end )
		end )
		for k = 1,math.random(5,10) do
			local elephant = SpawnPrefab("purpleamulet")
			elephant.Transform:SetPosition(pt.x+(math.random(30)-math.random(30)), 0, pt.z+(math.random(30)-math.random(30)))
			elephant.AnimState:SetBank("koalefant")
			elephant.AnimState:SetBuild("koalefant_winter_build")
			elephant.AnimState:PlayAnimation("idle_loop", true)
			local sound = elephant.entity:AddSoundEmitter()
			local shadow = elephant.entity:AddDynamicShadow()
			shadow:SetSize( 4.5, 2 )
			elephant.Transform:SetFourFaced()
			MakeCharacterPhysics(elephant, 500, 1.5)
			elephant:AddComponent("named")
			elephant.components.named:SetName("Elephant")
			elephant.Transform:SetScale(1.5, 1.5, 1.5)
			local minimap = elephant.entity:AddMiniMapEntity()
			minimap:SetIcon( "cave_banana_tree.png" )
			elephant:RemoveComponent("equippable")
			elephant:RemoveComponent("inventoryitem")
			elephant:RemoveComponent("fueled")
			elephant:RemoveComponent("deployable")
			elephant:AddComponent("knownlocations")
			elephant:AddComponent("health")
			elephant.components.health:SetMaxHealth(1000)
			elephant:AddComponent("combat")
			elephant.components.combat.hiteffectsymbol = "beefalo_body"
			elephant.components.combat:SetDefaultDamage(30)
			elephant.components.combat:SetAttackPeriod(2)
			elephant.components.combat:SetRetargetFunction(3, function(elephant)
				if not elephant.components.health:IsDead() then
					return FindEntity(elephant, 25, function(guy)
					   return guy:HasTag("monster")
					end )
				end
			end )
			elephant.components.combat:SetKeepTargetFunction(function(elephant, target) return target and target:IsValid() end )
			elephant:ListenForEvent("attacked", function(elephant, data)
				elephant.components.combat:SetTarget(data.attacker)
				elephant.components.combat:ShareTarget(data.attacker, 10, function(dude) return dude:HasTag("elephants") and not dude.components.health:IsDead() end, 1)
			end )
			elephant:AddComponent("lootdropper")
			elephant.components.lootdropper:SetLoot({"meat","meat","meat","meat","meat","meat","meat","meat","meat","meat","trunk_winter"})
			elephant:AddComponent("locomotor")
			elephant.components.locomotor.walkspeed = 2
			elephant.components.locomotor.runspeed = 4
			elephant:SetStateGraph("SGkoalefant")
			local brain = require "brains/frogbrain"
			elephant:SetBrain(brain)
			elephant:ListenForEvent( "nighttime", function() elephant:Remove() end , GetWorld())
			elephant:DoPeriodicTask(math.random(30, 60), function(elephant)
				if not elephant.components.combat.target then
				   local sgnames = {"bellow","graze","alert","shake"}
				   local sgname = sgnames[math.random(#sgnames)]
				   elephant.sg:GoToState(sgname)
				end
			end )
			elephant:DoPeriodicTask(math.random(120, 240), function(inst)
				SpawnPrefab("poop").Transform:SetPosition(elephant.Transform:GetWorldPosition())
			end )
			if math.random() < 0.5 then
			   elephant:AddTag("redelephants")
			   elephant.AnimState:SetBuild("koalefant_summer_build")
			   elephant.components.lootdropper:SetLoot({"meat","meat","meat","meat","meat","meat","meat","meat","meat","meat","trunk_summer"})
			end
			elephant:AddTag("elephants")
		end
	end , GetWorld())
	bananatree:AddTag("bananatree")
end
local function OnDeploy (inst, pt)
	makebananatree(inst)
	inst:Remove()
end
	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = OnDeploy
local function onsave(inst, data)
	if inst:HasTag("bananatree") then
		data.bananatree = true
	end
	if inst:HasTag("elephants") then
		data.elephants = true
	end
	if inst:HasTag("redelephants") then
		data.redelephants = true
	end
end
local function onload(inst, data)
	if data and data.bananatree then
	   makebananatree(inst)
	   inst:Remove()
	end
	if data and data.elephants then
	   inst.AnimState:SetBank("koalefant")
	   inst.AnimState:SetBuild("koalefant_winter_build")
	   inst.AnimState:PlayAnimation("idle_loop", true)
	   local sound = inst.entity:AddSoundEmitter()
	   local shadow = inst.entity:AddDynamicShadow()
	   shadow:SetSize( 4.5, 2 )
	   inst.Transform:SetFourFaced()
	   MakeCharacterPhysics(inst, 500, 1.5)
	   inst:AddComponent("named")
	   inst.components.named:SetName("Elephant")
	   inst.Transform:SetScale(1.5, 1.5, 1.5)
	   local minimap = inst.entity:AddMiniMapEntity()
	   minimap:SetIcon( "cave_banana_tree.png" )
	   inst:RemoveComponent("equippable")
	   inst:RemoveComponent("inventoryitem")
	   inst:RemoveComponent("fueled")
	   inst:RemoveComponent("deployable")
	   inst:AddComponent("knownlocations")
	   inst:AddComponent("health")
	   inst.components.health:SetMaxHealth(1000)
	   inst:AddComponent("combat")
	   inst.components.combat.hiteffectsymbol = "beefalo_body"
	   inst.components.combat:SetDefaultDamage(30)
	   inst.components.combat:SetAttackPeriod(2)
	   inst.components.combat:SetRetargetFunction(3, function(inst)
		   if not inst.components.health:IsDead() then
			   return FindEntity(inst, 25, function(guy)
				  return guy:HasTag("monster")
			   end )
		   end
	   end )
	   inst.components.combat:SetKeepTargetFunction(function(inst, target) return target and target:IsValid() end )
	   inst:ListenForEvent("attacked", function(inst, data)
		   inst.components.combat:SetTarget(data.attacker)
		   inst.components.combat:ShareTarget(data.attacker, 10, function(dude) return dude:HasTag("elephants") and not dude.components.health:IsDead() end, 1)
	   end )
	   inst:AddComponent("lootdropper")
	   inst.components.lootdropper:SetLoot({"meat","meat","meat","meat","meat","meat","meat","meat","meat","meat","trunk_winter"})
	   inst:AddComponent("locomotor")
	   inst.components.locomotor.walkspeed = 2
	   inst.components.locomotor.runspeed = 4
	   inst:SetStateGraph("SGkoalefant")
	   local brain = require "brains/frogbrain"
	   inst:SetBrain(brain)
	   inst:ListenForEvent( "nighttime", function() inst:Remove() end , GetWorld())
	   inst:DoPeriodicTask(math.random(30, 60), function(inst)
		   if not inst.components.combat.target then
			  local sgnames = {"bellow","graze","alert","shake"}
			  local sgname = sgnames[math.random(#sgnames)]
			  inst.sg:GoToState(sgname)
		   end
	   end )
	   inst:DoPeriodicTask(math.random(120, 240), function(inst)
		   SpawnPrefab("poop").Transform:SetPosition(inst.Transform:GetWorldPosition())
	   end )
	   inst:AddTag("elephants")
	end
	if data and data.redelephants then
	   inst:AddTag("redelephants")
	   inst.AnimState:SetBuild("koalefant_summer_build")
	   inst.components.lootdropper:SetLoot({"meat","meat","meat","meat","meat","meat","meat","meat","meat","meat","trunk_summer"})
	end
end
	inst.OnSave = onsave
	inst.OnLoad = onload

	即可用紫色护身符种象之树,白天会掉落香蕉,并有巨象群在周围活动,夜晚离开。巨象不会主动攻击你,想狩猎它们时,尽量选择落单的巨象,否则其他巨象会一起反击。巨象在小地图上显示为香蕉图标,它们是天然的肉库,杀死一只可获得10块大肉及象鼻,并且会不断产便便,缺少肥料的话,可以在象之树附近拾取。不想要象之树了,砍掉即可,象群会在黑夜离去。紫色护身符在魔法选项(画着红骷髅)下,用6个黄金、4个噩梦燃料、2个紫宝石制造