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

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



二一六.鬼影重重(夜晚墓地有大量亡魂游荡,购买亡魂之心让主角变身亡魂)

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

local function createghostnpc(inst)
	for k = 1,math.random(3,7) do
		local pt = inst:GetPosition()
		local ghostnpc = SpawnPrefab("deadlyfeast")
		ghostnpc.Transform:SetPosition(pt.x+(math.random(20)-math.random(20)), 0, pt.z+(math.random(20)-math.random(20)))
		ghostnpc.entity:AddSoundEmitter()
		ghostnpc.AnimState:SetBank("ghost")
		ghostnpc.AnimState:SetBuild("ghost_build")
		ghostnpc.AnimState:PlayAnimation("idle", true)
		ghostnpc.Transform:SetFourFaced()
		MakeGhostPhysics(ghostnpc, 1, .5)
		local minimap = ghostnpc.entity:AddMiniMapEntity()
		minimap:SetIcon("whitespider_den.png")
		ghostnpc.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
		local light = ghostnpc.entity:AddLight()
		light:SetIntensity(.6)
		light:SetRadius(.5)
		light:SetFalloff(.6)
		light:Enable(true)
		light:SetColour(180/255, 195/255, 225/255)
		local brain = require "brains/leifbrain"
		ghostnpc:SetBrain(brain)
		ghostnpc:AddComponent("locomotor")
		ghostnpc.components.locomotor.walkspeed = 5
		ghostnpc.components.locomotor.runspeed = 5
		ghostnpc:SetStateGraph("SGghost")
		ghostnpc:AddTag("ghostnpcs")
		ghostnpc:RemoveComponent("edible")
		ghostnpc:RemoveComponent("inventoryitem")
		ghostnpc:RemoveTag("meat")
		ghostnpc:AddComponent("sanityaura")
		ghostnpc.components.sanityaura.aura = -TUNING.SANITYAURA_SMALL
		ghostnpc:AddComponent("health")
		ghostnpc.components.health:SetMaxHealth(500)
		ghostnpc:AddComponent("trader")
		ghostnpc.components.trader:SetAcceptTest(function(ghostnpc, item) 
			if GetPlayer().components.inventory:Has("goldnugget", 30) then
			   if item.prefab == "goldnugget" then
				  return true
			   end
			end
			return false
		end )
		ghostnpc.components.trader.onaccept = function(ghostnpc, giver, item)
			GetPlayer().components.inventory:ConsumeByName("goldnugget", 29)
			local ghostheart = SpawnPrefab("deadlyfeast")
			ghostheart.AnimState:SetBank("egg")
			ghostheart.AnimState:SetBuild("tallbird_egg")
			ghostheart.AnimState:PlayAnimation("cooked")
			ghostheart.components.inventoryitem:ChangeImageName("tallbirdegg_cooked")
			ghostheart:RemoveComponent("edible")
			ghostheart:RemoveTag("meat")
			ghostheart:AddComponent("equippable")
			ghostheart.components.equippable:SetOnEquip( function(ghostheart, owner)
				if owner == GetPlayer() and not GetPlayer():HasTag("poisoning") and not GetPlayer():HasTag("fever") then
				   owner.components.locomotor:Stop()
				   owner.components.playercontroller:Enable(false)
				   owner:DoTaskInTime(0.3, function()
					   SpawnPrefab("collapse_big").Transform:SetPosition(owner.Transform:GetWorldPosition())
					   owner.AnimState:SetBank("ghost")
					   owner.AnimState:SetBuild("ghost_build")
					   owner:SetStateGraph("SGghost")
					   owner.AnimState:PlayAnimation("idle", true)
					   owner.Transform:SetScale(1.2,1.2,1.2)
					   local shadow = owner.entity:AddDynamicShadow()
					   shadow:SetSize( 0, 0 )
					   owner.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
					   local light = owner.entity:AddLight()
					   light:SetIntensity(.6)
					   light:SetRadius(20)
					   light:SetFalloff(.6)
					   light:Enable(true)
					   light:SetColour(180/255, 195/255, 225/255)
					   owner.components.locomotor.walkspeed = 20
					   owner.components.locomotor.runspeed = 20
					   owner.components.health:DoDelta(owner.components.health.maxhealth)
					   owner.components.health:SetInvincible(true)
					   owner.components.hunger:DoDelta(owner.components.hunger.max)
					   owner.components.hunger:Pause()
					   owner.components.sanity:DoDelta(owner.components.sanity.max)
					   owner.components.sanity.ignore = true
					   owner.components.combat:SetDefaultDamage(1000)
					   owner:AddComponent("aura")
					   owner.components.aura.radius = 3
					   owner.components.aura.tickperiod = 0.5
					   owner.components.aura.ignoreallies = true
					   owner.components.aura.auratestfn = function(ghostnpc, target)
						   if target.components.combat.target == GetPlayer() or GetPlayer().components.combat.target == target then
							  return true
						   end
						   return false
					   end
					   owner.components.temperature:SetTemp(20)
					   owner.components.playercontroller:Enable(true)
				   end )
				end
			end )
			ghostheart.components.equippable:SetOnUnequip( function(ghostheart, owner)
				SpawnPrefab("collapse_big").Transform:SetPosition(owner.Transform:GetWorldPosition())
				owner.components.locomotor:Stop()
				owner.components.playercontroller:Enable(false)
				owner:DoTaskInTime(0.3, function() 
					owner.AnimState:SetBank("wilson")
					if GetPlayer().prefab == "wilson" then owner.AnimState:SetBuild("wilson") end
					if GetPlayer().prefab == "wendy" then owner.AnimState:SetBuild("wendy") end
					if GetPlayer().prefab == "wes" then owner.AnimState:SetBuild("wes") end
					if GetPlayer().prefab == "wickerbottom" then owner.AnimState:SetBuild("wickerbottom") end
					if GetPlayer().prefab == "willow" then owner.AnimState:SetBuild("willow") end
					if GetPlayer().prefab == "wolfgang" then owner.AnimState:SetBuild("wolfgang") end
					if GetPlayer().prefab == "wx78" then owner.AnimState:SetBuild("wx78") end
					if GetPlayer().prefab == "woodie" then owner.AnimState:SetBuild("woodie") end
					if GetPlayer().prefab == "waxwell" then owner.AnimState:SetBuild("waxwell") end
					if GetPlayer().prefab == "wathgrithr" then owner.AnimState:SetBuild("wathgrithr") end
					if GetPlayer().prefab == "webber" then owner.AnimState:SetBuild("webber") end
					owner:SetStateGraph("SGwilson")
					owner.AnimState:PlayAnimation("idle")
					owner.Transform:SetScale(1,1,1)
					local shadow = owner.entity:AddDynamicShadow()
					shadow:SetSize( 1.3, .6 )
					owner.AnimState:SetBloomEffectHandle("")
					owner.Light:Enable(false)
					owner.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED
					owner.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED
					owner.components.health:SetInvincible(false)
					owner.components.hunger:Resume()
					owner.components.sanity.ignore = false
					owner.components.combat:SetDefaultDamage(TUNING.UNARMED_DAMAGE)
					owner.components.combat.hiteffectsymbol = "torso"
					owner:RemoveComponent("aura")
					owner.components.temperature:SetTemp(nil)
					owner.components.playercontroller:Enable(true)
					ghostheart:Remove()
				end )
			end )
			ghostheart.components.equippable.equipslot = EQUIPSLOTS.HANDS
			ghostheart:AddTag("ghostheart")
			GetPlayer().components.inventory:GiveItem(ghostheart)
		end
		ghostnpc:AddComponent("combat")
		ghostnpc.components.combat:SetDefaultDamage(3)
		ghostnpc.components.combat:SetAttackPeriod(1)
		ghostnpc.components.combat:SetRetargetFunction(2, function(ghostnpc)
			if not ghostnpc.components.health:IsDead() then
				return FindEntity(ghostnpc, 25, function(guy)
					if guy:HasTag("monster") then return guy end
					if guy:HasTag("player") and guy.components.inventory and not guy.components.inventory:FindItem(function(item) return item.prefab == "nightmarefuel" end ) then return guy end
				end )
			end
		end )
		ghostnpc:ListenForEvent("attacked", function(ghostnpc, data)
			ghostnpc.components.combat:SetTarget(data.attacker)
			ghostnpc.components.combat:ShareTarget(data.attacker, 35, function(dude) return dude:HasTag("ghostnpcs") and not dude.components.health:IsDead() end, 5)
		end )
		ghostnpc:AddComponent("aura")
		ghostnpc.components.aura.radius = 2
		ghostnpc.components.aura.tickperiod = 1
		ghostnpc.components.aura.ignoreallies = true
		ghostnpc.components.aura.auratestfn = function(ghostnpc, target)
		  if target:HasTag("ghostnpcs") then return false end
		  if target.components.combat.target == ghostnpc or ghostnpc.components.combat.target == target then
			 return true
		  end
		end
		ghostnpc:ListenForEvent("death", function()
			local gems = {"purplegem","bluegem","redgem","orangegem","yellowgem","greengem","thulecite","goldnugget"}
			local gem = gems[math.random(#gems)]
			SpawnPrefab(gem).Transform:SetPosition(ghostnpc.Transform:GetWorldPosition())
		end )
		ghostnpc:ListenForEvent( "daytime", function() ghostnpc:Remove() end , GetWorld())
	end
end
	inst:ListenForEvent( "dusktime", function() createghostnpc(inst) end , GetWorld())


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

local function onsave(inst, data)
	if inst:HasTag("ghostnpcs") then
		data.ghostnpcs = true
	end
	if inst:HasTag("ghostheart") then
		data.ghostheart = true
	end
end
local function onload(inst, data)
	if data and data.ghostnpcs then
		inst.entity:AddSoundEmitter()
		inst.AnimState:SetBank("ghost")
		inst.AnimState:SetBuild("ghost_build")
		inst.AnimState:PlayAnimation("idle", true)
		inst.Transform:SetFourFaced()
		MakeGhostPhysics(inst, 1, .5)
		local minimap = inst.entity:AddMiniMapEntity()
		minimap:SetIcon("whitespider_den.png")
		inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
		local light = inst.entity:AddLight()
		light:SetIntensity(.6)
		light:SetRadius(.5)
		light:SetFalloff(.6)
		light:Enable(true)
		light:SetColour(180/255, 195/255, 225/255)
		local brain = require "brains/leifbrain"
		inst:SetBrain(brain)
		inst:AddComponent("locomotor")
		inst.components.locomotor.walkspeed = 5
		inst.components.locomotor.runspeed = 5
		inst:SetStateGraph("SGghost")
		inst:AddTag("ghostnpcs")
		inst:RemoveComponent("edible")
		inst:RemoveComponent("inventoryitem")
		inst:RemoveTag("meat")
		inst:AddComponent("sanityaura")
		inst.components.sanityaura.aura = -TUNING.SANITYAURA_SMALL
		inst:AddComponent("health")
		inst.components.health:SetMaxHealth(500)
		inst:AddComponent("trader")
		inst.components.trader:SetAcceptTest(function(inst, item) 
			if GetPlayer().components.inventory:Has("goldnugget", 30) then
			   if item.prefab == "goldnugget" then
				  return true
			   end
			end
			return false
		end )
		inst.components.trader.onaccept = function(inst, giver, item)
			GetPlayer().components.inventory:ConsumeByName("goldnugget", 29)
			local ghostheart = SpawnPrefab("deadlyfeast")
			ghostheart.AnimState:SetBank("egg")
			ghostheart.AnimState:SetBuild("tallbird_egg")
			ghostheart.AnimState:PlayAnimation("cooked")
			ghostheart.components.inventoryitem:ChangeImageName("tallbirdegg_cooked")
			ghostheart:RemoveComponent("edible")
			ghostheart:RemoveTag("meat")
			ghostheart:AddComponent("equippable")
			ghostheart.components.equippable:SetOnEquip( function(ghostheart, owner)
				if owner == GetPlayer() and not GetPlayer():HasTag("poisoning") and not GetPlayer():HasTag("fever") then
				   owner.components.locomotor:Stop()
				   owner.components.playercontroller:Enable(false)
				   owner:DoTaskInTime(0.3, function()
					   SpawnPrefab("collapse_big").Transform:SetPosition(owner.Transform:GetWorldPosition())
					   owner.AnimState:SetBank("ghost")
					   owner.AnimState:SetBuild("ghost_build")
					   owner:SetStateGraph("SGghost")
					   owner.AnimState:PlayAnimation("idle", true)
					   owner.Transform:SetScale(1.2,1.2,1.2)
					   local shadow = owner.entity:AddDynamicShadow()
					   shadow:SetSize( 0, 0 )
					   owner.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
					   local light = owner.entity:AddLight()
					   light:SetIntensity(.6)
					   light:SetRadius(20)
					   light:SetFalloff(.6)
					   light:Enable(true)
					   light:SetColour(180/255, 195/255, 225/255)
					   owner.components.locomotor.walkspeed = 20
					   owner.components.locomotor.runspeed = 20
					   owner.components.health:DoDelta(owner.components.health.maxhealth)
					   owner.components.health:SetInvincible(true)
					   owner.components.hunger:DoDelta(owner.components.hunger.max)
					   owner.components.hunger:Pause()
					   owner.components.sanity:DoDelta(owner.components.sanity.max)
					   owner.components.sanity.ignore = true
					   owner.components.combat:SetDefaultDamage(1000)
					   owner:AddComponent("aura")
					   owner.components.aura.radius = 3
					   owner.components.aura.tickperiod = 0.5
					   owner.components.aura.ignoreallies = true
					   owner.components.aura.auratestfn = function(inst, target)
						   if target.components.combat.target == GetPlayer() or GetPlayer().components.combat.target == target then
							  return true
						   end
						   return false
					   end
					   owner.components.temperature:SetTemp(20)
					   owner.components.playercontroller:Enable(true)
				   end )
				end
			end )
			ghostheart.components.equippable:SetOnUnequip( function(ghostheart, owner)
				SpawnPrefab("collapse_big").Transform:SetPosition(owner.Transform:GetWorldPosition())
				owner.components.locomotor:Stop()
				owner.components.playercontroller:Enable(false)
				owner:DoTaskInTime(0.3, function() 
					owner.AnimState:SetBank("wilson")
					if GetPlayer().prefab == "wilson" then owner.AnimState:SetBuild("wilson") end
					if GetPlayer().prefab == "wendy" then owner.AnimState:SetBuild("wendy") end
					if GetPlayer().prefab == "wes" then owner.AnimState:SetBuild("wes") end
					if GetPlayer().prefab == "wickerbottom" then owner.AnimState:SetBuild("wickerbottom") end
					if GetPlayer().prefab == "willow" then owner.AnimState:SetBuild("willow") end
					if GetPlayer().prefab == "wolfgang" then owner.AnimState:SetBuild("wolfgang") end
					if GetPlayer().prefab == "wx78" then owner.AnimState:SetBuild("wx78") end
					if GetPlayer().prefab == "woodie" then owner.AnimState:SetBuild("woodie") end
					if GetPlayer().prefab == "waxwell" then owner.AnimState:SetBuild("waxwell") end
					if GetPlayer().prefab == "wathgrithr" then owner.AnimState:SetBuild("wathgrithr") end
					if GetPlayer().prefab == "webber" then owner.AnimState:SetBuild("webber") end
					owner:SetStateGraph("SGwilson")
					owner.AnimState:PlayAnimation("idle")
					owner.Transform:SetScale(1,1,1)
					local shadow = owner.entity:AddDynamicShadow()
					shadow:SetSize( 1.3, .6 )
					owner.AnimState:SetBloomEffectHandle("")
					owner.Light:Enable(false)
					owner.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED
					owner.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED
					owner.components.health:SetInvincible(false)
					owner.components.hunger:Resume()
					owner.components.sanity.ignore = false
					owner.components.combat:SetDefaultDamage(TUNING.UNARMED_DAMAGE)
					owner.components.combat.hiteffectsymbol = "torso"
					owner:RemoveComponent("aura")
					owner.components.temperature:SetTemp(nil)
					owner.components.playercontroller:Enable(true)
					ghostheart:Remove()
				end )
			end )
			ghostheart.components.equippable.equipslot = EQUIPSLOTS.HANDS
			ghostheart:AddTag("ghostheart")
			GetPlayer().components.inventory:GiveItem(ghostheart)
		end
		inst:AddComponent("combat")
		inst.components.combat:SetDefaultDamage(3)
		inst.components.combat:SetAttackPeriod(1)
		inst.components.combat:SetRetargetFunction(2, function(inst)
			if not inst.components.health:IsDead() then
				return FindEntity(inst, 25, function(guy)
					if guy:HasTag("monster") then return guy end
					if guy:HasTag("player") and guy.components.inventory and not guy.components.inventory:FindItem(function(item) return item.prefab == "nightmarefuel" end ) then return guy end
				end )
			end
		end )
		inst:ListenForEvent("attacked", function(inst, data)
			inst.components.combat:SetTarget(data.attacker)
			inst.components.combat:ShareTarget(data.attacker, 35, function(dude) return dude:HasTag("ghostnpcs") and not dude.components.health:IsDead() end, 5)
		end )
		inst:AddComponent("aura")
		inst.components.aura.radius = 2
		inst.components.aura.tickperiod = 1
		inst.components.aura.ignoreallies = true
		inst.components.aura.auratestfn = function(inst, target)
		  if target:HasTag("ghostnpcs") then return false end
		  if target.components.combat.target == inst or inst.components.combat.target == target then
			 return true
		  end
		end
		inst:ListenForEvent("death", function()
			local gems = {"purplegem","bluegem","redgem","orangegem","yellowgem","greengem","thulecite","goldnugget"}
			local gem = gems[math.random(#gems)]
			SpawnPrefab(gem).Transform:SetPosition(inst.Transform:GetWorldPosition())
		end )
		inst:ListenForEvent( "daytime", function() inst:Remove() end , GetWorld())
	end
	if data and data.ghostheart then
			inst.AnimState:SetBank("egg")
			inst.AnimState:SetBuild("tallbird_egg")
			inst.AnimState:PlayAnimation("cooked")
			inst.components.inventoryitem:ChangeImageName("tallbirdegg_cooked")
			inst:RemoveComponent("edible")
			inst:RemoveTag("meat")
			inst:AddComponent("equippable")
			inst.components.equippable:SetOnEquip( function(inst, owner)
				if owner == GetPlayer() and not GetPlayer():HasTag("poisoning") and not GetPlayer():HasTag("fever") then
				   owner.components.locomotor:Stop()
				   owner.components.playercontroller:Enable(false)
				   owner:DoTaskInTime(0.3, function()
					   SpawnPrefab("collapse_big").Transform:SetPosition(owner.Transform:GetWorldPosition())
					   owner.AnimState:SetBank("ghost")
					   owner.AnimState:SetBuild("ghost_build")
					   owner:SetStateGraph("SGghost")
					   owner.AnimState:PlayAnimation("idle", true)
					   owner.Transform:SetScale(1.2,1.2,1.2)
					   local shadow = owner.entity:AddDynamicShadow()
					   shadow:SetSize( 0, 0 )
					   owner.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
					   local light = owner.entity:AddLight()
					   light:SetIntensity(.6)
					   light:SetRadius(20)
					   light:SetFalloff(.6)
					   light:Enable(true)
					   light:SetColour(180/255, 195/255, 225/255)
					   owner.components.locomotor.walkspeed = 20
					   owner.components.locomotor.runspeed = 20
					   owner.components.health:DoDelta(owner.components.health.maxhealth)
					   owner.components.health:SetInvincible(true)
					   owner.components.hunger:DoDelta(owner.components.hunger.max)
					   owner.components.hunger:Pause()
					   owner.components.sanity:DoDelta(owner.components.sanity.max)
					   owner.components.sanity.ignore = true
					   owner.components.combat:SetDefaultDamage(1000)
					   owner:AddComponent("aura")
					   owner.components.aura.radius = 3
					   owner.components.aura.tickperiod = 0.5
					   owner.components.aura.ignoreallies = true
					   owner.components.aura.auratestfn = function(ghostnpc, target)
						   if target.components.combat.target == GetPlayer() or GetPlayer().components.combat.target == target then
							  return true
						   end
						   return false
					   end
					   owner.components.temperature:SetTemp(20)
					   owner.components.playercontroller:Enable(true)
				   end )
				end
			end )
			inst.components.equippable:SetOnUnequip( function(inst, owner)
				SpawnPrefab("collapse_big").Transform:SetPosition(owner.Transform:GetWorldPosition())
				owner.components.locomotor:Stop()
				owner.components.playercontroller:Enable(false)
				owner:DoTaskInTime(0.3, function() 
					owner.AnimState:SetBank("wilson")
					if GetPlayer().prefab == "wilson" then owner.AnimState:SetBuild("wilson") end
					if GetPlayer().prefab == "wendy" then owner.AnimState:SetBuild("wendy") end
					if GetPlayer().prefab == "wes" then owner.AnimState:SetBuild("wes") end
					if GetPlayer().prefab == "wickerbottom" then owner.AnimState:SetBuild("wickerbottom") end
					if GetPlayer().prefab == "willow" then owner.AnimState:SetBuild("willow") end
					if GetPlayer().prefab == "wolfgang" then owner.AnimState:SetBuild("wolfgang") end
					if GetPlayer().prefab == "wx78" then owner.AnimState:SetBuild("wx78") end
					if GetPlayer().prefab == "woodie" then owner.AnimState:SetBuild("woodie") end
					if GetPlayer().prefab == "waxwell" then owner.AnimState:SetBuild("waxwell") end
					if GetPlayer().prefab == "wathgrithr" then owner.AnimState:SetBuild("wathgrithr") end
					if GetPlayer().prefab == "webber" then owner.AnimState:SetBuild("webber") end
					owner:SetStateGraph("SGwilson")
					owner.AnimState:PlayAnimation("idle")
					owner.Transform:SetScale(1,1,1)
					local shadow = owner.entity:AddDynamicShadow()
					shadow:SetSize( 1.3, .6 )
					owner.AnimState:SetBloomEffectHandle("")
					owner.Light:Enable(false)
					owner.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED
					owner.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED
					owner.components.health:SetInvincible(false)
					owner.components.hunger:Resume()
					owner.components.sanity.ignore = false
					owner.components.combat:SetDefaultDamage(TUNING.UNARMED_DAMAGE)
					owner.components.combat.hiteffectsymbol = "torso"
					owner:RemoveComponent("aura")
					owner.components.temperature:SetTemp(nil)
					owner.components.playercontroller:Enable(true)
					inst:Remove()
				end )
			end )
			inst.components.equippable.equipslot = EQUIPSLOTS.HANDS
			inst:AddTag("ghostheart")
	end
end
	inst.OnSave = onsave
	inst.OnLoad = onload

	即可在夜晚时,墓地有大量亡魂游荡,会主动攻击你,且靠近它们会降脑,消灭亡魂可获得宝石、铥矿石、黄金。亡魂在小地图上显示为白蜘蛛的图标。如果身上携带了噩梦燃料,亡魂就不会主动攻击你,给亡魂30个黄金(拿着黄金对亡魂点鼠标左键),可购买亡魂之心,装备它即可变身亡魂,生命、饥饿、脑全部锁死,按Ctrl + 鼠标左键攻击,且任何攻击你的敌人都会被自动烫死。卸载亡魂之心即可变回人,同时亡魂之心消失,想再变身就多买一些吧。如果修改了“小病小灾”,在主角感冒和食物中毒时,装备亡魂之心也不会变身