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

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



一八四.家园防御系统(暗夜照明灯自动向靠近的敌人发射炮弹)

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

local function onattack(inst)
	local pos = Vector3(inst.Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 30)
	for k,v in pairs(ents) do
		if v.components.health and v.components.combat and not v.components.health:IsDead() then
		   if v.components.combat.target == GetPlayer() or GetPlayer().components.combat.target == v or v:HasTag("monster") then
			  inst.components.weapon:LaunchProjectile(inst, v)
			  v.components.health:DoDelta(-5000)
			  inst.SoundEmitter:PlaySound("dontstarve/creatures/eyeballturret/shotexplo")
			  SpawnPrefab("collapse_small").Transform:SetPosition(v.Transform:GetWorldPosition())
			  SpawnPrefab("explode_small").Transform:SetPosition(v.Transform:GetWorldPosition())
			  GetPlayer().components.playercontroller:ShakeCamera(inst, "FULL", 0.7, 0.02, .5, 40)
			  inst.components.fueled:InitializeFuelLevel(1000)
		   end
		end
	end
end
local function turnon(inst)
	inst.components.machine.ison = true
	inst.task = inst:DoPeriodicTask(.5, function() onattack(inst) end)
	inst.components.burnable:IsBurning()
	inst.components.fueled:InitializeFuelLevel(1000)
	end
local function turnoff(inst)
	inst.components.machine.ison = false
	if inst.task then inst.task:Cancel() inst.task = nil end
	inst.components.burnable:Extinguish()
	inst.components.fueled:InitializeFuelLevel(0)
end
	inst:AddComponent("machine")
	inst.components.machine.turnonfn = turnon
	inst.components.machine.turnofffn = turnoff
	inst:AddComponent("weapon")
	inst.components.weapon:SetDamage(0)
	inst.components.weapon:SetProjectile("eye_charge")

	即可对暗夜照明灯按鼠标右键开启家园防御系统,自动向靠近的敌人发射炮弹(不会攻击同伴),再按鼠标右键关闭。建在基地附近,无惧任何敌人。暗影照明灯在魔法选项(画着红骷髅)下,用8个黄金、2个噩梦燃料、1个红宝石建造