源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

491 lines
15 KiB

пре 1 месец
  1. AutoFightManager= AutoFightManager or BaseClass(EventDispatcher)
  2. local AutoFightManager = AutoFightManager
  3. local math_random = math.random
  4. AutoFightManager.CHANGE_AUTO_FIGHT_STATE = "CHANGE_AUTO_FIGHT_STATE"
  5. AutoFightManager.CHANGE_AUTO_FIND_WAY_STATE = "CHANGE_AUTO_FIND_WAY_STATE"
  6. AutoFightManager.UPDATE_EXPERIENCE_DATA = "UPDATE_EXPERIENCE_DATA" --更新自动挂机数据,历练经验时间
  7. AutoFightManager.CLOSE_AUTO_FIGHT_TIP = "CLOSE_AUTO_FIGHT_TIP" --关闭挂机buff tips界面
  8. AutoFightManager.TIME_GOODS = 240402 --时光卷轴
  9. AutoFightManager.KAO_ROU = 230201 --神秘烤肉
  10. AutoFightManager.FlyGoodsID = 102602 --飞鞋物品
  11. function AutoFightManager:__init()
  12. AutoFightManager.Instance=self
  13. self.auto_fight_state = false--是否开启自动打怪
  14. self.auto_find_way_state = false --是否在自动寻路状态,在挂机的过程中,自动寻路优先
  15. -- 怪物列表
  16. self.monsterList = nil
  17. self.is_temp_mode = false --是否临时手动中
  18. self.now_auto_fight_buff = nil --挂机buff数据
  19. self.auto_fight_cache_pos = {x = 0, y = 0} --开启自动挂机的时候记录下位置
  20. self.not_show_buy_alert = false --不提示购买烤肉
  21. self.game_running = false
  22. self.go_to_pos = nil --技能技能后摇没结束前的移动坐标
  23. self.not_show_alert = false --本次登录是否不在显示开启10倍经验提醒界面
  24. self.last_show_alert_bone_fire_id = nil --上次弹提醒界面对应的篝火id
  25. self.auto_begin_x = 0 --记录开始挂机前的坐标
  26. self.auto_begin_y = 0
  27. self.next_fly_pos = nil --飞鞋位置
  28. self.fly_require_time = 0 --上次使用飞鞋时间
  29. self.fly_shoe_data = nil --飞鞋寻路数据
  30. self.is_flying_shoe = false -- 正在使用小飞鞋
  31. self.change_scene_fly_time = 0 --切场景使用飞鞋的时间
  32. self.is_transfer_out = false --是否通过传送门传送走
  33. end
  34. function AutoFightManager:__delete()
  35. end
  36. function AutoFightManager:getInstance()
  37. if AutoFightManager.Instance==nil then
  38. AutoFightManager.New()
  39. end
  40. return AutoFightManager.Instance
  41. end
  42. function AutoFightManager:GetBeginAutoPos( )
  43. return self.auto_begin_x, self.auto_begin_y
  44. end
  45. function AutoFightManager:SetBeginAutoPos( x, y )
  46. self.auto_begin_x = x
  47. self.auto_begin_y = y
  48. end
  49. --设置挂机配置里面选定的怪物
  50. function AutoFightManager:SetMonsterList(val)
  51. self.monsterList = val
  52. end
  53. --获取挂机配置的里面设定的怪物
  54. function AutoFightManager:GetmonsterList()
  55. return self.monsterList
  56. end
  57. --自动挂机状态,开启之后停止寻路
  58. function AutoFightManager:SetAutoFightState(value)
  59. if value ~= self.auto_fight_state then
  60. self.auto_fight_state = value
  61. GlobalEventSystem:Fire(AutoFightManager.CHANGE_AUTO_FIGHT_STATE)
  62. end
  63. end
  64. --获取是否在自动挂机的状态值
  65. function AutoFightManager:GetAutoFightState()
  66. return self.auto_fight_state
  67. end
  68. --自动寻路状态,开启之后停止挂机
  69. function AutoFightManager:SetAutoFindWayState(value)
  70. if self.auto_find_way_state ~= value then
  71. self.auto_find_way_state = value
  72. GlobalEventSystem:Fire(AutoFightManager.CHANGE_AUTO_FIND_WAY_STATE)
  73. end
  74. end
  75. --获取自动寻路状态值
  76. function AutoFightManager:GetAutoFindWayState( )
  77. return self.auto_find_way_state
  78. end
  79. --开始自动战斗
  80. function AutoFightManager:StartAutoFight(not_save_cookies)
  81. self:SetAutoFightState(true) -- 设置自动战斗状态为true
  82. -- if not_save_cookies == nil or not_save_cookies == false then
  83. -- self:SetAutoFightStateCookie(true)
  84. -- end
  85. end
  86. -- 停止自动战斗
  87. function AutoFightManager:StopAutoFight(not_save_cookies)
  88. self:SetAutoFightState(false)
  89. -- if not_save_cookies == nil or not_save_cookies == false then
  90. -- self:SetAutoFightStateCookie(false)
  91. -- end
  92. end
  93. --设置挂机cookie
  94. function AutoFightManager:SetAutoFightStateCookie(bool)
  95. CookieWrapper.Instance:SaveCookie(CookieLevelType.Account, CookieTimeType.TYPE_ALWAYS, CookieKey.AUTO_FIGHT_STATE, bool)
  96. end
  97. --获取挂机cookie
  98. function AutoFightManager:GetAutoFightStateCookie()
  99. return CookieWrapper.Instance:GetCookie(CookieLevelType.Account, CookieKey.AUTO_FIGHT_STATE)
  100. end
  101. --把挂机攻击优先状态保存入cookies
  102. function AutoFightManager:SetAttackFirstCookies(_unLimit, _npcFirst, _roleFirst)
  103. local list = {unLimit = _unLimit, npcFirst = _npcFirst, roleFirst = _roleFirst}
  104. CookieWrapper.Instance:SaveCookie(CookieLevelType.Account, CookieTimeType.TYPE_ALWAYS, CookieKey.AUTO_FIGHT_ATTACK_FIRST_STATE, list)
  105. end
  106. --从cookies中获取挂机攻击优先状态
  107. function AutoFightManager:GetAttackFirstCookies()
  108. local state_list = CookieWrapper.Instance:GetCookie(CookieLevelType.Account, CookieKey.AUTO_FIGHT_ATTACK_FIRST_STATE)
  109. if state_list == nil then
  110. return false, true, false
  111. else
  112. return state_list.unLimit, state_list.npcFirst, state_list.roleFirst
  113. end
  114. end
  115. --把挂机中吃烤肉状态保存入cookies
  116. function AutoFightManager:SetAutoEatCookies(_autoEat, _autoBuy)
  117. local list = {autoEat = _autoEat, autoBuy = _autoBuy}
  118. CookieWrapper.Instance:SaveCookie(CookieLevelType.Account, CookieTimeType.TYPE_ALWAYS, CookieKey.AUTO_EAT_STATE, list)
  119. end
  120. --从cookies中获取挂机中吃烤肉状态
  121. function AutoFightManager:GetAutoEatCookies()
  122. local state_list = CookieWrapper.Instance:GetCookie(CookieLevelType.Account, CookieKey.AUTO_EAT_STATE)
  123. if state_list == nil then
  124. return true, false
  125. else
  126. return state_list.autoEat, state_list.autoBuy
  127. end
  128. end
  129. function AutoFightManager:SetAutoFightBuff(data)
  130. self.now_auto_fight_buff = data
  131. self:Fire(AutoFightManager.UPDATE_EXPERIENCE_DATA)
  132. self:ShowExperienceIcon(self.now_auto_fight_buff.time)
  133. end
  134. --是否吃了烤肉
  135. function AutoFightManager:HaveBuff(buff_list)
  136. if buff_list then
  137. if #buff_list > 0 then
  138. self:ShowExperienceIcon()
  139. end
  140. end
  141. end
  142. function AutoFightManager:ShowExperienceIcon(time)
  143. GlobalEventSystem:Fire(EventName.UPDATE_BUFF_VIEW)
  144. end
  145. --保存下开始自动挂机的位置
  146. function AutoFightManager:CacheAutoFightPos(x, y)
  147. if Scene.Instance:GetMainRole() then
  148. local real_x, real_y = Scene.Instance:GetMainRole():GetRealPos()
  149. if real_x ~= 0 and real_y ~= 0 then
  150. local x = x
  151. local y = y
  152. if x == nil or y == nil then
  153. x = real_x
  154. y = real_y
  155. end
  156. self.auto_fight_cache_pos.x = x
  157. self.auto_fight_cache_pos.y = y
  158. -- print("自动挂机记录下的坐标是==========:", x, y)
  159. end
  160. Scene.Instance:ClearLastAttack()
  161. end
  162. end
  163. --判断技能是否是允许自动释放状态,默认是允许
  164. function AutoFightManager:FindSkillIsAuto(id)
  165. local is_auto = true
  166. if self.auto_skill_list then
  167. local result = self.auto_skill_list[id]
  168. --只有手动取消勾选过的,才剔除
  169. if result and result == 0 then
  170. is_auto = false
  171. end
  172. return is_auto
  173. end
  174. self.auto_skill_list = self.auto_skill_list or {}
  175. local is_auto = true
  176. if SkillManager:getInstance():GetHitSkillVo() and id == SkillManager:getInstance():GetHitSkillVo().id and is_auto then --羁绊技能默认不自动勾选
  177. is_auto = true
  178. end
  179. local auto_skill_list = CookieWrapper.Instance:GetCookie(CookieLevelType.Account, CookieKey.AUTO_FIAGHT_SKILL_LIST)
  180. if auto_skill_list then
  181. for i,v in ipairs(auto_skill_list) do
  182. self.auto_skill_list[v.id] = v.state and 1 or 0
  183. if v.id == id then
  184. is_auto = v.state
  185. end
  186. end
  187. end
  188. return is_auto
  189. end
  190. --保存自动挂机的技能设置
  191. function AutoFightManager:SetAutoFightSkillList(id, state)
  192. self.auto_skill_list = self.auto_skill_list or {}
  193. if id then
  194. local result = state and 1 or 0
  195. self.auto_skill_list[id] = result
  196. end
  197. local auto_skill_list = CookieWrapper.Instance:GetCookie(CookieLevelType.Account, CookieKey.AUTO_FIAGHT_SKILL_LIST)
  198. local find = false
  199. if auto_skill_list then
  200. for i,v in ipairs(auto_skill_list) do
  201. if v.id == id then
  202. find = true
  203. v.state = state
  204. break
  205. end
  206. end
  207. else
  208. auto_skill_list = {}
  209. end
  210. if find == false then
  211. local list = {}
  212. list.id = id
  213. list.state = state
  214. table.insert(auto_skill_list, list)
  215. end
  216. CookieWrapper.Instance:SaveCookie(CookieLevelType.Account, CookieTimeType.TYPE_ALWAYS, CookieKey.AUTO_FIAGHT_SKILL_LIST, auto_skill_list)
  217. CookieWrapper.Instance:WriteAll()
  218. end
  219. --获取优先攻击哪类目标
  220. function AutoFightManager:GetTargetFirstAttack()
  221. local unLimit, npcFirst, roleFirst = self:GetAttackFirstCookies()
  222. if unLimit then
  223. return Scene.FIND_TARGET_TYPE.all
  224. end
  225. if npcFirst then
  226. return Scene.FIND_TARGET_TYPE.monster
  227. end
  228. if roleFirst then
  229. return Scene.FIND_TARGET_TYPE.role
  230. end
  231. return Scene.FIND_TARGET_TYPE.all
  232. end
  233. --登录后设置为上次挂机状态
  234. function AutoFightManager:SceneStartRestoreAutoState()
  235. local state = self:GetAutoFightStateCookie()
  236. if state == nil or state == false then
  237. return false
  238. end
  239. return state
  240. end
  241. --挂机过程中,玩家点击地图或地面,保存下坐标,技能后摇结束后,前往该坐标
  242. function AutoFightManager:CacheGoToPos(x, y, scene_id)
  243. if x == nil and y == nil then
  244. self.go_to_pos = false
  245. else
  246. self.go_to_pos = {x = x, y = y, scene_id = scene_id}
  247. end
  248. end
  249. --根据玩家等级获取自动挂机的场景和怪物堆
  250. function AutoFightManager:GetAutoFightSceneAndMonster(role_lv,scene_Id)
  251. local target_cfg = nil
  252. for i,v in ipairs(Config.ConfigWorldMon.AutoFightAera) do
  253. if role_lv >= v.role_lv then
  254. target_cfg = v
  255. -- break
  256. end
  257. end
  258. local scene_id = nil
  259. local monster_id = nil
  260. local pos = nil
  261. if target_cfg then
  262. if target_cfg.scene and #target_cfg.scene > 0 then
  263. local rmd = scene_Id or target_cfg.scene[math_random(1, #target_cfg.scene)]
  264. local cfg = Config.ConfigWorldMon.SceneMons[rmd]
  265. local len = TableSize(cfg.mon_list)
  266. if cfg and len > 0 then
  267. scene_id = cfg.scene_id
  268. local obj = {}
  269. for k,v in pairs(cfg.mon_list) do
  270. local tmp = {}
  271. tmp.id = v.id
  272. table.insert(obj, tmp)
  273. end
  274. local random_id = obj[math_random(1, len)].id
  275. if random_id then
  276. local heap_cfg = cfg.mon_list[random_id]
  277. if heap_cfg and #heap_cfg.way_point > 0 then
  278. monster_id = heap_cfg.id
  279. pos = heap_cfg.way_point[math_random(1, #heap_cfg.way_point)]
  280. end
  281. end
  282. end
  283. end
  284. end
  285. return scene_id, monster_id, pos
  286. end
  287. --大多数活动,需要在安全区域和主城洛兰王国才能报名、参加活动,进入到活动场景
  288. --在野外场景需要寻路到安全区
  289. function AutoFightManager:FindSafePosAndGoActivity(call_back)
  290. local real_pos_x, real_pos_y = Scene.Instance:GetMainRole():GetRealPos()
  291. if SceneManager:getInstance():IsOutdoorScene() and not SceneManager:getInstance():IsSafePos(real_pos_x, real_pos_y) then
  292. local birth_x, birth_y = SceneManager.Instance:GetSceneBirthPos()
  293. if birth_x and birth_y then
  294. Message.show("战斗状态不可参加,正寻路到安全区")
  295. GlobalEventSystem:Fire(EventName.STOPAUTOFIGHT, false, true)--寻路要先切换掉自动挂机状态
  296. if self.timer then
  297. GlobalTimerQuest:CancelQuest(self.timer)
  298. self.timer = nil
  299. end
  300. local main_role = Scene.Instance:GetMainRole()
  301. if main_role and main_role:CanMove() then
  302. self:GoFun(birth_x, birth_y, call_back)
  303. else
  304. local timer_func = function ()
  305. if main_role and main_role:CanMove() then
  306. self:GoFun(birth_x, birth_y, call_back)
  307. if self.timer then
  308. GlobalTimerQuest:CancelQuest(self.timer)
  309. self.timer = nil
  310. end
  311. end
  312. end
  313. self.timer = GlobalTimerQuest:AddPeriodQuest(timer_func, 0.1, -1)
  314. end
  315. end
  316. end
  317. end
  318. function AutoFightManager:GoFun(birth_x, birth_y, call_back)
  319. if birth_x and birth_y then
  320. local findVo = FindVo.New()
  321. findVo.type = FindVo.POINT
  322. findVo.sceneId = SceneManager.Instance:GetSceneId()
  323. findVo.id = 0
  324. findVo.x = birth_x / SceneObj.LogicRealRatio.x
  325. findVo.y = birth_y / SceneObj.LogicRealRatio.y
  326. findVo.call_back = call_back
  327. print("------野外场景的安全区-------", findVo.sceneId, findVo.x, findVo.y)
  328. GlobalEventSystem:Fire(EventName.FIND,findVo)
  329. if self.event_id then
  330. GlobalEventSystem:UnBind(self.event_id)
  331. self.event_id = nil
  332. end
  333. --脱离战斗状态5秒,允许进入活动
  334. local quit_fun = function ()
  335. if call_back then
  336. call_back()
  337. if self.event_id then
  338. GlobalEventSystem:UnBind(self.event_id)
  339. self.event_id = nil
  340. end
  341. call_back = nil
  342. if findVo then
  343. findVo.call_back = nil
  344. end
  345. end
  346. end
  347. self.event_id = GlobalEventSystem:Bind(EventName.QUIT_FIGHT_STATE, quit_fun)
  348. end
  349. end
  350. function AutoFightManager:StartOutdoorXiuLian(scene_Id,monster_Id,position)
  351. local scene_id, monster_id, pos = nil,nil,nil
  352. if scene_Id and monster_Id and position then
  353. scene_id, monster_id, pos = scene_Id,monster_Id,position
  354. else
  355. scene_id, monster_id, pos = AutoFightManager:getInstance():GetAutoFightSceneAndMonster(RoleManager.Instance.mainRoleInfo.level)
  356. end
  357. if scene_id and monster_id and pos then
  358. local findVo = FindVo.New()
  359. -- findVo.type = FindVo.MONSTER
  360. findVo.type = FindVo.POINT
  361. findVo.sceneId = scene_id
  362. findVo.id = monster_id
  363. findVo.x = pos.x / SceneObj.LogicRealRatio.x
  364. findVo.y = pos.y / SceneObj.LogicRealRatio.y
  365. findVo.call_type = FindVo.CallType.FiledGuaJi
  366. print("-------随机挂机点-------", findVo.sceneId, findVo.id, findVo.x, findVo.y)
  367. findVo.call_back = function ()
  368. GlobalEventSystem:Fire(EventName.STARTAUTOFIGHT)
  369. if RoleManager.Instance.mainRoleInfo.team_id == 0 then
  370. local function ok()
  371. GlobalEventSystem:Fire(EventName.OPEN_AROUND_TEAM_VIEW)
  372. end
  373. Alert.show("是否查看附近队伍加入挂机?",Alert.Type.One,ok)
  374. end
  375. end
  376. if self.timer then
  377. GlobalTimerQuest:CancelQuest(self.timer)
  378. self.timer = nil
  379. end
  380. local main_role = Scene.Instance:GetMainRole()
  381. if main_role and main_role:CanMove() then
  382. GlobalEventSystem:Fire(EventName.FIND,findVo)
  383. else
  384. local timer_func = function ()
  385. if main_role and main_role:CanMove() then
  386. GlobalEventSystem:Fire(EventName.FIND,findVo)
  387. if self.timer then
  388. GlobalTimerQuest:CancelQuest(self.timer)
  389. self.timer = nil
  390. end
  391. end
  392. end
  393. self.timer = GlobalTimerQuest:AddPeriodQuest(timer_func, 0.1, -1)
  394. end
  395. else
  396. Message.show("您等级太高了,找不到合适的挂机区域")
  397. end
  398. end
  399. --寻路的过程中,在战斗状态下不能切场景,
  400. function AutoFightManager:FindVoCallTypeFun(_type)
  401. if _type == FindVo.CallType.FiledGuaJi then --野外挂机
  402. local call = function ()
  403. self:StartOutdoorXiuLian()
  404. end
  405. self:FindSafePosAndGoActivity(call)
  406. end
  407. end
  408. function AutoFightManager:SetFlyData(scene_id,x,y,callback)
  409. if scene_id then
  410. self.fly_shoe_data = {}
  411. self.fly_shoe_data.scene_id = scene_id
  412. self.fly_shoe_data.x = x
  413. self.fly_shoe_data.y = y
  414. self.fly_shoe_data.callback = callback
  415. else
  416. self.fly_shoe_data = nil
  417. end
  418. end
  419. function AutoFightManager:SetChangeSceneFlyTime(time)
  420. self.change_scene_fly_time = time
  421. end
  422. function AutoFightManager:GetChangeSceneFlyTime()
  423. return self.change_scene_fly_time
  424. end
  425. --定点挂机指定坐标,否则取人物当前坐标为定点坐标
  426. function AutoFightManager:SetFixedPointPos( tablexy )
  427. self.fixed_point = tablexy
  428. end
  429. function AutoFightManager:GetFixedPointPos( )
  430. return self.fixed_point
  431. end