SourceOperateTargetAction = SourceOperateTargetAction or BaseClass(SourceOperateMove) local SourceOperateTargetAction = SourceOperateTargetAction --[[@ 功能: SourceOperateTargetAction的初始化函数 参数: target_compress_id 目标的压缩ID int32 range 范围 numeric action_func 动作函数 function action_period_func 动作周期函数,获取执行动作的周期 function action_end_func 结束动作的条件 function clear_func 清理函数 function 返回值: 无 其它: 无 作者: deadline ]] function SourceOperateTargetAction:__init(target_compress_id, range, action_func, action_period_func, action_end_func, clear_func,scene_obj) self.type = OperateManager.SourceOperateType.TargetAction self.init_time = 0 self.target_compress_id = target_compress_id self.range = range self.action_func = action_func self.action_period_func = action_period_func self.action_end_func = action_end_func self.clear_func = clear_func self.scene_obj = scene_obj self.is_path_valid = false self.path_list = nil self.total_pair = 0 --最后一次动作时间 self.last_action_time = 0 end function SourceOperateTargetAction:__delete( ) if self.clear_func ~= nil then self.clear_func() end end function SourceOperateTargetAction:GetTargetObj( ) return Scene.Instance:GetObjByCompressedId(self.target_compress_id) end --[[@ 功能: 执行初始化操作, 产生一些基准数据 参数: 无 返回值: 为true表示初始化成功, 为false表示初始化失败 其它: 无 作者: deadline ]] function SourceOperateTargetAction:Init() self.init_time = Status.NowTime self.path_list = {} --查看目标对象是否在场景中 local obj = self:GetTargetObj() if obj == nil then --print("SourceOperateTargetAction:Init can not find target obj") return false end --print("target action init suc!") return true end --[[@ 功能: 用于从SourceOperate中获取一个NowOperate控制角色行为 参数: 无 返回值: 当前操作包 NowOperate 其它: 无 作者: deadline ]] function SourceOperateTargetAction:GetOperImpl() --print("excute target action") local now_oper = nil local now_time = Status.NowTime --判断目标对象是否已经不存在 local target_obj = self:GetTargetObj() if target_obj == nil then self.is_finish = true --print("target action can not find target!") -- GlobalEventSystem:Fire(SceneEventType.ACTION_TARGET_MISSING) --目标不存在会导致auto_find_way不会重置 local scene_obj = self.scene_obj or Scene.Instance:GetMainRole() if scene_obj and scene_obj:GetType() == SceneBaseType.MainRole then GlobalEventSystem:Fire(EventName.FINDWAY_ENDED) end return self:GenerateNop(0.2) end --重新以主角当前位置作为寻路起始位置 local scene_obj = self.scene_obj or Scene.Instance:GetMainRole() local x1, y1 = scene_obj:GetRealPos() local x2, y2 = target_obj:GetRealPos() -- local distance = dir:length() local distance = GameMath.GetDistance(x1, y1, x2, y2, false) --print("= = = = SourceOperateTargetAction: dist:", distance, self.range) --距离在可动作范围内 if distance <= self.range * self.range then --在这停止移动 if scene_obj:GetType() == SceneBaseType.MainRole then local real_pos = co.Vector2(scene_obj:GetRealPos()) if scene_obj.move_flag == MOVE_TYPE.ACCELERATE then EventSystem.Fire(GlobalEventSystem, SceneEventType.MOVEREQUEST, real_pos.x, real_pos.y, MOVE_TYPE.ACCELERATE) else EventSystem.Fire(GlobalEventSystem, SceneEventType.MOVEREQUEST, real_pos.x, real_pos.y, MOVE_TYPE.NORMOL_MOVE) end end local dir = co.TempVector2(x2-x1, y2-y1) scene_obj:SetDirection(dir) scene_obj:WaitForNextMove() -- print(string.format("= = = = Action_func():(%.2f, %.2f), (%.2f, %.2f)", start_logic_pos.x, start_logic_pos.y, end_logic_pos.x, end_logic_pos.y)) if self.action_func then self.action_func() --执行相关动作 end self.last_action_time = Status.NowTime now_oper = self:GenerateNop(0.2) self.is_finish = true dir:DeleteV() else --当前的起始坐标和终点坐标,必须用RealPos计算才能准确 local start_logic_pos = co.Vector2(scene_obj:GetLogicPos()) local end_logic_pos = co.Vector2(target_obj:GetLogicPos()) --范围外,执行寻路 -- if not self.is_path_valid or self.now_use_pair > self.total_pair then --重新计算路径 self.path_list = {} -- print("= = = = 重新寻路") if not self:RecalcPathInfo(start_logic_pos, end_logic_pos, 0) then self.is_finish = true print("计算路径失败直接停止该操作包") return self:GenerateNop(0.2) --计算路径失败直接停止该操作包(怪卡障碍点或者自己卡障碍点) end -- end --取出下一个小路径执行 local move_pair = self.path_list[self.now_use_pair] if move_pair == nil then self.is_finish = true return self:GenerateNop(0.2) end -- print(string.format("= = = new pair:%d (%.2f, %.2f) (%.2f, %.2f)", self.now_use_pair, move_pair.start_point.x, move_pair.start_point.y, move_pair.end_point.x, move_pair.end_point.y)) now_oper = NowOperateTargetMove.New(now_time, move_pair.start_point, move_pair.end_point, target_obj, self.range,scene_obj) self.now_use_pair = self.now_use_pair + 1 end return now_oper end