SkillData.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. --[[
  2. 数据说明
  3. unlockSkillMap = {
  4. [skillId] = {
  5. ['skillId'] = skillId,
  6. ['skillLv'] = skillLv,
  7. }
  8. }
  9. slotSkills = {
  10. [1] = {
  11. ['skillId'] = skillId,
  12. ['isUnlock'] = isUnlock,
  13. ['defaultSkillId'] = defaultSkillId,
  14. }
  15. }
  16. ]]
  17. local SkillData = class("SkillData")
  18. function SkillData:ctor()
  19. self.unlockSkillMap = nil
  20. self.slotSkills = nil
  21. self.advanceSkillMap = nil
  22. self.currPress = 0
  23. self.maxPress = 0
  24. end
  25. function SkillData:Clear()
  26. self.unlockSkillMap = nil
  27. self.slotSkills = nil
  28. self.advanceSkillMap = nil
  29. self.currPress = 0
  30. self.maxPress = 0
  31. end
  32. function SkillData:Destroy()
  33. if self.Clear then
  34. self:Clear()
  35. end
  36. self:UnRegisterNetEvents()
  37. end
  38. function SkillData:InitData(data)
  39. self:Clear()
  40. self:InitJobSkillData(data.job_skill_list)
  41. self:InitSlotSkillData(data.skill_list)
  42. end
  43. function SkillData:InitJobSkillData(jobSkillList, newSkills, upLevelSkills, downLevelSkills)
  44. local unlockSkillMap = self.unlockSkillMap
  45. if not self.advanceSkillMap then
  46. self.advanceSkillMap = {}
  47. end
  48. if not unlockSkillMap then
  49. unlockSkillMap = {}
  50. self.unlockSkillMap = unlockSkillMap
  51. end
  52. for i = 1, #jobSkillList do
  53. local jobSkillItem = jobSkillList[i]
  54. local unlockSkillList = jobSkillItem.unlock_skill_list
  55. if unlockSkillList then
  56. for j = 1, #unlockSkillList do
  57. local skillInfo = unlockSkillList[j]
  58. local skillId = skillInfo.key or 0
  59. local skillLv = skillInfo.value or 0
  60. local unlockSkillItem = unlockSkillMap[skillId]
  61. if unlockSkillItem then
  62. if unlockSkillItem.skillLv < skillLv then
  63. if upLevelSkills then
  64. table.insert(upLevelSkills, { skillType = Enum.SkillType.Active, skillId = skillId })
  65. end
  66. elseif unlockSkillItem.skillLv > skillLv then
  67. if downLevelSkills then
  68. table.insert(downLevelSkills, { skillType = Enum.SkillType.Active, skillId = skillId })
  69. end
  70. -- TODO: 2020-05-18 出现道具洗技能等级,所以这个Log没用了
  71. -- LogError('[Wboy] skill can not lv degradation ' .. tostring(skillId) .. ' ' .. tostring(unlockSkillItem.skillLv) .. ' ' .. tostring(skillLv))
  72. end
  73. else
  74. if newSkills then
  75. table.insert(newSkills, { skillType = Enum.SkillType.Active, skillId = skillId })
  76. end
  77. unlockSkillItem = {}
  78. unlockSkillMap[skillId] = unlockSkillItem
  79. local IsLeaderHero = math.floor(skillId / 10000) == 1
  80. local skillTreeCfgData
  81. if IsLeaderHero then
  82. skillTreeCfgData = ManagerContainer.CfgMgr:GetSkillTreeCfgById(skillId)
  83. else
  84. skillTreeCfgData = ManagerContainer.CfgMgr:GetParterSkillTreeCfgById(skillId)
  85. end
  86. if skillTreeCfgData and skillTreeCfgData.beforeSkill and skillTreeCfgData.beforeSkill ~= 0 then
  87. self.advanceSkillMap[skillId] = skillId
  88. if unlockSkillMap[skillTreeCfgData.beforeSkill] then
  89. unlockSkillMap[skillTreeCfgData.beforeSkill] = nil
  90. self.advanceSkillMap[skillTreeCfgData.beforeSkill] = nil
  91. if self.slotSkills and #self.slotSkills > 0 then
  92. for i = 1, #self.slotSkills do
  93. if self.slotSkills[i].skillId == skillTreeCfgData.beforeSkill then
  94. self.slotSkills[i].skillId = skillId
  95. end
  96. end
  97. end
  98. end
  99. end
  100. end
  101. unlockSkillItem.skillId = skillId
  102. unlockSkillItem.skillLv = skillLv
  103. end
  104. end
  105. end
  106. --刷新技能数据同时刷新压力值
  107. self:RefreshUnlockSkillPress()
  108. --刷新技能数据同时刷新勇气祝福等级
  109. self:RefreshNerveBenediction()
  110. end
  111. function SkillData:RefreshNerveBenediction()
  112. if not self.NerveBenedIction then
  113. self.NerveBenedIction = {}
  114. end
  115. local allCfgDatas = ManagerContainer.CfgMgr:GetAllSkillSuitNewCfg()
  116. for i = #allCfgDatas, 1, -1 do
  117. if not self.NerveBenedIction[i] then
  118. self.NerveBenedIction[i] = {}
  119. end
  120. self.NerveBenedIction[i].needNum = 0
  121. self.NerveBenedIction[i].currcount = 0
  122. local cfgData = allCfgDatas[i]
  123. local conditions = cfgData.TriggerConditions
  124. if conditions and #conditions > 0 then
  125. for j = 1, #conditions do
  126. local needLv = conditions[j][2]
  127. local needNum = conditions[j][1]
  128. self.NerveBenedIction[i].needNum = self.NerveBenedIction[i].needNum + needNum
  129. local currcount = 0
  130. for k, v in pairs(self.advanceSkillMap) do
  131. local skillLv = self:GetSkillLv(v)
  132. if skillLv >= needLv then
  133. currcount = currcount + 1
  134. end
  135. end
  136. self.NerveBenedIction[i].currcount = self.NerveBenedIction[i].currcount + currcount
  137. end
  138. if self.NerveBenedIction[i].currcount >= self.NerveBenedIction[i].needNum then
  139. self.NerveBenedIction[i].currcount = self.NerveBenedIction[i].needNum
  140. self.currNerveLv = i
  141. return
  142. end
  143. else
  144. self.currNerveLv = 1
  145. end
  146. end
  147. end
  148. function SkillData:GetCurrNerveLv()
  149. return self.currNerveLv or 0
  150. end
  151. function SkillData:GetNerveCurrDataById(id)
  152. return self.NerveBenedIction[id]
  153. end
  154. function SkillData:IsCanResetSkill(type)
  155. if type == 1 then --进阶技能重置
  156. for k, v in pairs(self.advanceSkillMap) do
  157. local skillLv = self:GetSkillLv(v)
  158. if skillLv > 1 then
  159. return true
  160. end
  161. end
  162. else --所有技能重置
  163. for k, v in pairs(self.unlockSkillMap) do
  164. if v.skillLv > 1 then
  165. return true
  166. end
  167. end
  168. end
  169. return false
  170. end
  171. function SkillData:InitSlotSkillData(slotSkillList)
  172. local slotSkills = {}
  173. for i = 1, #slotSkillList do
  174. local item = slotSkillList[i]
  175. local slotSkillItem = {}
  176. slotSkillItem.skillId = item.skill_id or 0
  177. slotSkillItem.isUnlock = (item.unlock and item.unlock > 0) and true or false
  178. slotSkillItem.defaultSkillId = item.default_skill_id or 0
  179. slotSkills[i] = slotSkillItem
  180. end
  181. self.slotSkills = slotSkills
  182. end
  183. function SkillData:OnJobSkillDataChange(data)
  184. local jobSkillList = data.job_skill_list
  185. local newSkills = {}
  186. local upLevelSkills = {}
  187. local downLevelSkills = {}
  188. self:InitJobSkillData(jobSkillList, newSkills, upLevelSkills, downLevelSkills)
  189. local isSlotChanged = false
  190. if not isSlotChanged and newSkills then
  191. for k,v in pairs(newSkills) do
  192. -- 是否槽位上填充了新技能
  193. if self:GetSkillIsAtSlot(v.skillId) then
  194. isSlotChanged = true
  195. break
  196. end
  197. end
  198. end
  199. if not isSlotChanged and upLevelSkills then
  200. for k,v in pairs(upLevelSkills) do
  201. -- 是否槽位上的技能升级了
  202. if self:GetSkillIsAtSlot(v.skillId) then
  203. isSlotChanged = true
  204. break
  205. end
  206. end
  207. end
  208. if not isSlotChanged and downLevelSkills then
  209. for k,v in pairs(downLevelSkills) do
  210. -- 是否槽位上的技能降级了
  211. if self:GetSkillIsAtSlot(v.skillId) then
  212. isSlotChanged = true
  213. break
  214. end
  215. end
  216. end
  217. return isSlotChanged, newSkills, upLevelSkills, downLevelSkills
  218. end
  219. --- 当单个技能槽位数据发生变化
  220. function SkillData:OnOneSkillSlotDataChange(data)
  221. local idx = data.idx
  222. local slotInfo = data.slot_info
  223. local item = self.slotSkills[idx]
  224. if not item then
  225. item = {}
  226. self.slotSkills[idx] = item
  227. end
  228. item.skillId = slotInfo.skill_id or 0
  229. item.isUnlock = (slotInfo.unlock and slotInfo.unlock > 0) and true or false
  230. item.defaultSkillId = slotInfo.default_skill_id or 0
  231. end
  232. --- 当技能槽位数据发生变化
  233. function SkillData:OnSkillSlotDataChange(data)
  234. local slots = data.slot_info
  235. for i = 1, #slots do
  236. local slot = slots[i]
  237. local item = self.slotSkills[i]
  238. if not item then
  239. item = {}
  240. self.slotSkills[i] = item
  241. end
  242. item.skillId = slot.skill_id or 0
  243. item.isUnlock = (slot.unlock and slot.unlock > 0) and true or false
  244. item.defaultSkillId = slot.default_skill_id or 0
  245. end
  246. end
  247. function SkillData:GetUnlockSkillMap()
  248. return self.unlockSkillMap
  249. end
  250. --- 获得技能是否解锁了
  251. ---@param skillId integer 技能Id
  252. function SkillData:GetIsUnlock(skillId)
  253. if self.unlockSkillMap then
  254. return self.unlockSkillMap[skillId] and true or false
  255. end
  256. return false
  257. end
  258. --- 获得技能等级
  259. ---@param skillId integer 技能Id
  260. function SkillData:GetSkillLv(skillId)
  261. if self.unlockSkillMap and self.unlockSkillMap[skillId] then
  262. return self.unlockSkillMap[skillId].skillLv or 0
  263. end
  264. return 0
  265. end
  266. --- 槽位的技能信息列表
  267. function SkillData:GetSlotSkills()
  268. return self.slotSkills
  269. end
  270. --- 技能是否已在槽位中
  271. ---@param skillId integer 技能Id
  272. function SkillData:GetSkillIsAtSlot(skillId)
  273. for i = 1, #self.slotSkills do
  274. local slotSkill = self.slotSkills[i]
  275. if slotSkill then
  276. if slotSkill.skillId == skillId and slotSkill.isUnlock then
  277. return true
  278. end
  279. end
  280. end
  281. return false
  282. end
  283. --- 获得槽位上的技能Id
  284. ---@param idx integer 槽位ID 1-4
  285. function SkillData:GetSlotSkillId(idx)
  286. local slotSkill = self.slotSkills[idx]
  287. if slotSkill then
  288. if not slotSkill.isUnlock then
  289. return 0
  290. end
  291. return slotSkill.skillId
  292. end
  293. return 0
  294. end
  295. --- 获得槽位是否解锁
  296. ---@param idx integer 槽位ID 1-4
  297. function SkillData:GetSlotIsUnlock(idx)
  298. local slotSkill = self.slotSkills[idx]
  299. if slotSkill then
  300. return slotSkill.isUnlock
  301. end
  302. return false
  303. end
  304. --- 获得槽位解锁的数量
  305. function SkillData:GetUnlockSlotNum()
  306. local num = 0
  307. for i = 1, #self.slotSkills do
  308. local slotSkill = self.slotSkills[i]
  309. if slotSkill.isUnlock then
  310. num = num + 1
  311. end
  312. end
  313. return num
  314. end
  315. --- 解锁的槽位上已放置的技能
  316. function SkillData:GetSlotVaildSkillNum()
  317. local num = 0
  318. for i = 1, #self.slotSkills do
  319. local slotSkill = self.slotSkills[i]
  320. if slotSkill.isUnlock and slotSkill.skillId > 0 then
  321. num = num + 1
  322. end
  323. end
  324. return num
  325. end
  326. --- 获得槽位上最低等级的技能等级
  327. function SkillData:GetSlotVaildSkillLowerLv()
  328. local lowerLv = nil
  329. local lowerId = nil
  330. for i = 1, #self.slotSkills do
  331. local slotSkill = self.slotSkills[i]
  332. if slotSkill.isUnlock and slotSkill.skillId > 0 then
  333. local lv = self:GetSkillLv(slotSkill.skillId)
  334. if lv > 0 then
  335. if not lowerLv or lowerLv > lv then
  336. lowerLv = lv
  337. lowerId = slotSkill.skillId
  338. end
  339. end
  340. end
  341. end
  342. return lowerLv, lowerId
  343. end
  344. --- 获得不在槽位中的所有解锁的主动技能
  345. function SkillData:GetUnlockSkillsExceptSlot()
  346. local slotSkillIds = {}
  347. for i = 1, #self.slotSkills do
  348. local slotSkill = self.slotSkills[i]
  349. if slotSkill.skillId and slotSkill.skillId > 0 then
  350. table.insert(slotSkillIds, slotSkill.skillId)
  351. end
  352. end
  353. local num = #slotSkillIds
  354. local isVaild = false
  355. local unlockSkills = {}
  356. if self.unlockSkillMap then
  357. for _, skillInfo in pairs(self.unlockSkillMap) do
  358. if skillInfo then
  359. local skillId = skillInfo.skillId
  360. isVaild = true
  361. for j = num, 1, -1 do
  362. if slotSkillIds[j] == skillId then
  363. table.remove(slotSkillIds, j)
  364. num = num - 1
  365. isVaild = false
  366. break
  367. end
  368. end
  369. if isVaild then
  370. table.insert(unlockSkills, skillId)
  371. end
  372. end
  373. end
  374. end
  375. return unlockSkills
  376. end
  377. function SkillData:GetUsedSkillParams()
  378. local len = #self.slotSkills
  379. local usedSkills = System.Array.CreateInstance(typeof(SkillParam), len)
  380. for i = 1, len do
  381. local skillParam = nil
  382. local slotSkill = self.slotSkills[i]
  383. if slotSkill then
  384. if slotSkill.skillId <= 0 or not slotSkill.isUnlock then
  385. skillParam = SkillParam.New(slotSkill.defaultSkillId, 1)
  386. else
  387. skillParam = SkillParam.New(slotSkill.skillId, self:GetSkillLv(slotSkill.skillId))
  388. end
  389. else
  390. skillParam = SkillParam.New(0, 0)
  391. end
  392. usedSkills[i - 1] = skillParam
  393. end
  394. return usedSkills
  395. end
  396. function SkillData:GetUsedSkills()
  397. local skills = {}
  398. for i = 1, #self.slotSkills do
  399. local slotSkill = self.slotSkills[i]
  400. if slotSkill then
  401. if slotSkill.skillId <= 0 or not slotSkill.isUnlock then
  402. skills[i] = { skillId = slotSkill.defaultSkillId, lv = 1 }
  403. else
  404. skills[i] = { skillId = slotSkill.skillId, lv = self:GetSkillLv(slotSkill.skillId) }
  405. end
  406. else
  407. skills[i] = { skillId = 0, lv = 0 }
  408. end
  409. end
  410. return skills
  411. end
  412. function SkillData:GetUnlockSkillAttrs()
  413. local attr = nil
  414. if self.unlockSkillMap then
  415. attr = {}
  416. local attrId, attrValue
  417. for _, skillInfo in pairs(self.unlockSkillMap) do
  418. if skillInfo then
  419. local skillId = skillInfo.skillId
  420. local skillLv = skillInfo.skillLv
  421. for i = skillLv, 1, -1 do
  422. local cfgId = CommonUtil.GetSkillUpEffectCfgIdByIdAndLv(skillId, i)
  423. local cfgData = ManagerContainer.CfgMgr:GetSkillUpEffectCfgById(cfgId)
  424. if cfgData then
  425. local addAttributes = cfgData.AddAttributes
  426. if addAttributes then
  427. for _, value in pairs(addAttributes) do
  428. attrId = value[1]
  429. attrValue = value[2]
  430. if attr[attrId] == nil then
  431. attr[attrId] = attrValue
  432. else
  433. attr[attrId] = attr[attrId] + attrValue
  434. end
  435. end
  436. end
  437. end
  438. end
  439. end
  440. end
  441. end
  442. self:GetAvanceSkillAttrs(attr)
  443. return attr
  444. end
  445. function SkillData:RefreshUnlockSkillPress()
  446. local currPress = 0
  447. local maxPress = 0
  448. if self.unlockSkillMap then
  449. local attrId, attrValue
  450. for _, skillInfo in pairs(self.unlockSkillMap) do
  451. if skillInfo then
  452. local skillId = skillInfo.skillId
  453. local skillLv = skillInfo.skillLv
  454. local skilltreeData = self:GetSkillTreeDataById(skillId)
  455. if skilltreeData and skilltreeData.PressPoint then
  456. local advancePress = self:GetAdvancePressByRecursion(skillId)
  457. maxPress = maxPress + skilltreeData.PressPoint * (skilltreeData.MaxLv - 1) + advancePress +skilltreeData.StartPressPoint
  458. end
  459. end
  460. end
  461. end
  462. self.maxPress = maxPress
  463. end
  464. function SkillData:GetMaxPress()
  465. return self.maxPress or 0
  466. end
  467. function SkillData:GetAdvancePressByRecursion(skillId)
  468. local maxPress = 0
  469. local skillcfg = self:GetSkillTreeDataById(skillId)
  470. if skillcfg and skillcfg.beforeSkill and skillcfg.beforeSkill ~= 0 then
  471. local beforeskillId = skillcfg.beforeSkill
  472. local beforeSkillcfg = self:GetSkillTreeDataById(beforeskillId)
  473. if beforeSkillcfg and beforeSkillcfg.StartPressPoint then
  474. local advancePress = 0
  475. if beforeskillId ~= skillId then
  476. local advancePress = self:GetAdvancePressByRecursion(beforeskillId)
  477. end
  478. maxPress = maxPress + skillcfg.StartPressPoint + advancePress
  479. end
  480. end
  481. return maxPress
  482. end
  483. -- 获取进阶技能属性列表
  484. function SkillData:GetAvanceSkillAttrs(attr,skillList)
  485. --判断是否是进阶技能 如果是 将之前进阶的所有属性加入属性列表
  486. if self.unlockSkillMap then
  487. for _, skillInfo in pairs(self.unlockSkillMap) do
  488. if skillInfo then
  489. self:GetAdvanceSkillAttrByRecursion(attr,skillList,skillInfo.skillId)
  490. end
  491. end
  492. end
  493. end
  494. function SkillData:GetSkillTreeDataById(skillId)
  495. local IsLeaderHero = math.floor(skillId / 10000) == 1
  496. local skillTreeCfgData
  497. if IsLeaderHero then
  498. skillTreeCfgData = ManagerContainer.CfgMgr:GetSkillTreeCfgById(skillId)
  499. else
  500. skillTreeCfgData = ManagerContainer.CfgMgr:GetParterSkillTreeCfgById(skillId)
  501. end
  502. return skillTreeCfgData
  503. end
  504. function SkillData:GetAdvanceSkillAttrByRecursion(attr,skillList,skillId)
  505. local attrId, attrValue
  506. local skillcfg = self:GetSkillTreeDataById(skillId)
  507. if skillcfg and skillcfg.beforeSkill and skillcfg.beforeSkill ~= 0 then
  508. local beforeskillId = skillcfg.beforeSkill
  509. local beforeSkillcfg = self:GetSkillTreeDataById(beforeskillId)
  510. if beforeSkillcfg then
  511. if skillList then
  512. skillList[#skillList + 1] = beforeskillId
  513. end
  514. local skillLv = beforeSkillcfg.MaxLv
  515. for i = skillLv, 1, -1 do
  516. local cfgId = CommonUtil.GetSkillUpEffectCfgIdByIdAndLv(beforeskillId, i)
  517. local cfgData = ManagerContainer.CfgMgr:GetSkillUpEffectCfgById(cfgId)
  518. if cfgData then
  519. local addAttributes = cfgData.AddAttributes
  520. if addAttributes then
  521. for _, value in pairs(addAttributes) do
  522. attrId = value[1]
  523. attrValue = value[2]
  524. if attr[attrId] == nil then
  525. attr[attrId] = attrValue
  526. else
  527. attr[attrId] = attr[attrId] + attrValue
  528. end
  529. end
  530. end
  531. end
  532. end
  533. end
  534. if beforeskillId ~= skillId then
  535. self:GetAdvanceSkillAttrByRecursion(attr,skillList,beforeskillId)
  536. end
  537. end
  538. end
  539. --- 检查技能等级变化,是否会导致属性变化
  540. function SkillData:CheckSkillChangeAttr(skillId, oldSkillLv, newSkillLv)
  541. if not skillId then return false end
  542. if oldSkillLv == newSkillLv then return false end
  543. if oldSkillLv == nil then oldSkillLv = 0 end
  544. if newSkillLv == nil then newSkillLv = 0 end
  545. local iterSign = 1
  546. if oldSkillLv > newSkillLv then iterSign = -1 end
  547. for lv = oldSkillLv + iterSign, newSkillLv, iterSign do
  548. local cfgId = CommonUtil.GetSkillUpEffectCfgIdByIdAndLv(skillId, lv)
  549. local cfgData = ManagerContainer.CfgMgr:GetSkillUpEffectCfgById(cfgId)
  550. if cfgData then
  551. return true
  552. end
  553. end
  554. return false
  555. end
  556. return SkillData