-- 跑向动态位置行为 -- 该行为用来进行一次性的自动寻路并移动,期间如果玩家有操作就会打断行为,行为结束一定会关闭AI。 local Base = require("Battle/Logic/AI/Decision/Decision_Base") local Decision_RunToDynamicPos = class("Decision_RunToDynamicPos", Base) local ARRIVE_DIST_SQR = FP.newV1(4) --[[ 初始化 --]] function Decision_RunToDynamicPos:initialize(entity) --类型 self.Type = LuaConst.DecisionType.RunToDynamicPos --瞬时 self.IsOnce = false end --[[ 进入该Decision --]] function Decision_RunToDynamicPos:DecisionIn(entity, decisionData) if entity.components.aiBase.runToDynamicPos then SystemManager.FindPath(entity, entity.components.aiBase.runToDynamicPos , true) end end function Decision_RunToDynamicPos:DecisionOut(entity, decisionData) entity.components.aiBase:SetRunToDynamicPos(nil) --退出这个Decision时就清空DynamicPos SystemManager.StopAutoFight(entity) --退出这个Decision时需要停止自动战斗 end function Decision_RunToDynamicPos:CanEnter(entity) return entity.components.aiBase.runToDynamicPos ~= nil end --[[ 能否退出 退出条件:移动到固定目标点 --]] function Decision_RunToDynamicPos:CanExit(entity, decisionData, command) local canExit = false local pathComponent = entity.components.path local curPathPos = pathComponent.Path[pathComponent.CurPathPosIndex] if not curPathPos then canExit = true else if entity.components.aiBase.runToDynamicPos == nil then canExit = true else if entity.components.aiBase.runToDynamicPos:DistanceSqr(entity:GetLogicPos()) <= ARRIVE_DIST_SQR then canExit = true end end end return canExit end --[[ 输出指令 --]] function Decision_RunToDynamicPos:SetCommand(entity, decisionData, command) repeat local aiBaseComponent = entity.components.aiBase local decisionRecordDic = aiBaseComponent:GetDecisionRecordDic(decisionData) if self:ShouldBlockAI(decisionRecordDic) then break end if self:HasPlayerInput(command) then --如果有玩家输入,就直接把目标位置设为nil,这个会引发该Decision退出 entity.components.aiBase:SetRunToDynamicPos(nil) break end return self:RunToPathPoint(entity, command, decisionData) until true end return Decision_RunToDynamicPos