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

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



二四六.警戒模式(按键盘F8键主角自动巡逻并战斗,再按F8键恢复手动控制)

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

local items = { SWORD = "swap_spear" }
local function EquipItem(inst, item)
	if item then
	   inst.AnimState:OverrideSymbol("swap_object", item, item)
	   inst.AnimState:Show("ARM_carry") 
	   inst.AnimState:Hide("ARM_normal")
	end
end
	inst.items = items
	inst.equipfn = EquipItem
	EquipItem(inst)
TheInput:AddKeyUpHandler(KEY_F8, function()
	 if not inst:HasTag("patrol") then
		inst.components.locomotor:Stop()
		inst:SetBrain(nil)
		inst.components.talker:ShutUp()
		inst.components.playercontroller:Enable(false)
		inst:DoTaskInTime(0.3, function() 
			inst:AddTag("patrol")
			inst.SoundEmitter:PlaySound("dontstarve/HUD/research_available")
			inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
			inst:AddComponent("knownlocations")
			local brain = require "brains/frogbrain"
			inst:SetBrain(brain)
			inst:RestartBrain()
			local head = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
			inst.components.inventory:DropItem(head)
			local hands = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
			inst.components.inventory:DropItem(hands)
			local body = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
			inst.components.inventory:DropItem(body)
			inst.AnimState:OverrideSymbol("swap_object", "swap_spear", "swap_spear")
			inst.AnimState:Show("ARM_carry")
			inst.AnimState:Hide("ARM_normal")
			inst.HUD.controls.crafttabs:Hide()
			inst.HUD.controls.inv:Hide()
			local light = inst.entity:AddLight()
			light:SetIntensity(.8)
			light:SetRadius(20)
			light:SetFalloff(.6)
			light:Enable(true)
			light:SetColour(255/255,255/255,0/255)
			inst.components.locomotor.walkspeed = 10
			inst.components.locomotor.runspeed = 15
			inst:SetStateGraph("SGshadowwaxwell")
			inst.components.health:SetInvincible(true)
			inst.components.hunger:Pause()
			inst.components.sanity.ignore = true
			inst.components.temperature:SetTemp(20)
			inst.components.combat:SetDefaultDamage(200)
			inst.components.combat:SetAttackPeriod(0.25)
			inst.components.combat:SetRange(3)
			inst.components.combat:SetRetargetFunction(1, function(inst)
				if not inst.components.health:IsDead() then
					return FindEntity(inst, 50, function(guy)
						if guy.components.health and not guy.components.health:IsDead() then
						   return guy.components.combat.target == inst or inst.components.combat.target == guy or guy:HasTag("monster")
						end
					end )
				end
			end )
			inst.components.combat:SetKeepTargetFunction(function(inst, target) return target and target:IsValid() end )
		end )
	 else
		inst.components.locomotor:Stop()
		inst:SetBrain(nil)
		inst.components.talker:ShutUp()
		inst:DoTaskInTime(0.3, function() 
			inst:RemoveTag("patrol")
			inst.SoundEmitter:PlaySound("dontstarve/HUD/research_available")
			inst.AnimState:SetBloomEffectHandle("")
			local brain = require "brains/wilsonbrain"
			inst:SetBrain(brain)
			inst:RestartBrain()
			inst:RemoveComponent("knownlocations")
			inst.AnimState:Hide("ARM_carry")
			inst.AnimState:Show("ARM_normal")
			inst.HUD.controls.crafttabs:Show()
			inst.HUD.controls.inv:Show()
			inst.Light:Enable(false)
			inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED
			inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED
			inst:SetStateGraph("SGwilson")
			inst.components.health:SetInvincible(false)
			inst.components.hunger:Resume()
			inst.components.sanity.ignore = false
			inst.components.temperature:SetTemp(nil)
			inst.components.hunger:DoDelta(-inst.components.hunger.max*.5)
			inst.components.combat:SetDefaultDamage(TUNING.UNARMED_DAMAGE)
			inst.components.combat:SetAttackPeriod(TUNING.WILSON_ATTACK_PERIOD)
			inst.components.combat:SetRange(2)
			inst.components.combat:SetTarget(nil)
			inst.components.combat:SetRetargetFunction(nil, nil )
			inst.components.combat:SetKeepTargetFunction(nil)
			inst.components.playercontroller:Enable(true)
		end )
	end
end )

	即可按键盘F8键,开启主角警戒模式,CPU将接管你对主角的控制权,让主角自动巡逻并保护所在地区(夜晚站岗),一旦发现敌人,会自行战斗,由于主角的生命、脑、饥饿值已全部锁死,并且无惧寒冷和黑暗,所以你无须担心主角的安全。再次按键盘F8键即可恢复手动操作,并扣减一半饥饿值,注意及时补充食物。开启警戒模式前,请将身上的装备全部卸下,否则将掉落在地上