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

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


  1
  2二四六.警戒模式(按键盘F8键主角自动巡逻并战斗,再按F8键恢复手动控制
  3
  4	MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/player_common.lua文件,在inst:AddComponent("playercontroller")的下一行插入以下内容:
  5
  6local items = { SWORD = "swap_spear" }
  7local function EquipItem(inst, item)
  8	if item then
  9	   inst.AnimState:OverrideSymbol("swap_object", item, item)
 10	   inst.AnimState:Show("ARM_carry") 
 11	   inst.AnimState:Hide("ARM_normal")
 12	end
 13end
 14	inst.items = items
 15	inst.equipfn = EquipItem
 16	EquipItem(inst)
 17TheInput:AddKeyUpHandler(KEY_F8, function()
 18	 if not inst:HasTag("patrol") then
 19		inst.components.locomotor:Stop()
 20		inst:SetBrain(nil)
 21		inst.components.talker:ShutUp()
 22		inst.components.playercontroller:Enable(false)
 23		inst:DoTaskInTime(0.3, function() 
 24			inst:AddTag("patrol")
 25			inst.SoundEmitter:PlaySound("dontstarve/HUD/research_available")
 26			inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
 27			inst:AddComponent("knownlocations")
 28			local brain = require "brains/frogbrain"
 29			inst:SetBrain(brain)
 30			inst:RestartBrain()
 31			local head = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
 32			inst.components.inventory:DropItem(head)
 33			local hands = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
 34			inst.components.inventory:DropItem(hands)
 35			local body = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
 36			inst.components.inventory:DropItem(body)
 37			inst.AnimState:OverrideSymbol("swap_object", "swap_spear", "swap_spear")
 38			inst.AnimState:Show("ARM_carry")
 39			inst.AnimState:Hide("ARM_normal")
 40			inst.HUD.controls.crafttabs:Hide()
 41			inst.HUD.controls.inv:Hide()
 42			local light = inst.entity:AddLight()
 43			light:SetIntensity(.8)
 44			light:SetRadius(20)
 45			light:SetFalloff(.6)
 46			light:Enable(true)
 47			light:SetColour(255/255,255/255,0/255)
 48			inst.components.locomotor.walkspeed = 10
 49			inst.components.locomotor.runspeed = 15
 50			inst:SetStateGraph("SGshadowwaxwell")
 51			inst.components.health:SetInvincible(true)
 52			inst.components.hunger:Pause()
 53			inst.components.sanity.ignore = true
 54			inst.components.temperature:SetTemp(20)
 55			inst.components.combat:SetDefaultDamage(200)
 56			inst.components.combat:SetAttackPeriod(0.25)
 57			inst.components.combat:SetRange(3)
 58			inst.components.combat:SetRetargetFunction(1, function(inst)
 59				if not inst.components.health:IsDead() then
 60					return FindEntity(inst, 50, function(guy)
 61						if guy.components.health and not guy.components.health:IsDead() then
 62						   return guy.components.combat.target == inst or inst.components.combat.target == guy or guy:HasTag("monster")
 63						end
 64					end )
 65				end
 66			end )
 67			inst.components.combat:SetKeepTargetFunction(function(inst, target) return target and target:IsValid() end )
 68		end )
 69	 else
 70		inst.components.locomotor:Stop()
 71		inst:SetBrain(nil)
 72		inst.components.talker:ShutUp()
 73		inst:DoTaskInTime(0.3, function() 
 74			inst:RemoveTag("patrol")
 75			inst.SoundEmitter:PlaySound("dontstarve/HUD/research_available")
 76			inst.AnimState:SetBloomEffectHandle("")
 77			local brain = require "brains/wilsonbrain"
 78			inst:SetBrain(brain)
 79			inst:RestartBrain()
 80			inst:RemoveComponent("knownlocations")
 81			inst.AnimState:Hide("ARM_carry")
 82			inst.AnimState:Show("ARM_normal")
 83			inst.HUD.controls.crafttabs:Show()
 84			inst.HUD.controls.inv:Show()
 85			inst.Light:Enable(false)
 86			inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED
 87			inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED
 88			inst:SetStateGraph("SGwilson")
 89			inst.components.health:SetInvincible(false)
 90			inst.components.hunger:Resume()
 91			inst.components.sanity.ignore = false
 92			inst.components.temperature:SetTemp(nil)
 93			inst.components.hunger:DoDelta(-inst.components.hunger.max*.5)
 94			inst.components.combat:SetDefaultDamage(TUNING.UNARMED_DAMAGE)
 95			inst.components.combat:SetAttackPeriod(TUNING.WILSON_ATTACK_PERIOD)
 96			inst.components.combat:SetRange(2)
 97			inst.components.combat:SetTarget(nil)
 98			inst.components.combat:SetRetargetFunction(nil, nil )
 99			inst.components.combat:SetKeepTargetFunction(nil)
100			inst.components.playercontroller:Enable(true)
101		end )
102	end
103end )
104
105	即可按键盘F8键,开启主角警戒模式,CPU将接管你对主角的控制权,让主角自动巡逻并保护所在地区(夜晚站岗),一旦发现敌人,会自行战斗,由于主角的生命、脑、饥饿值已全部锁死,并且无惧寒冷和黑暗,所以你无须担心主角的安全。再次按键盘F8键即可恢复手动操作,并扣减一半饥饿值,注意及时补充食物。开启警戒模式前,请将身上的装备全部卸下,否则将掉落在地上