LuaToCSData.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. local LuaToCSData = {}
  2. function LuaToCSData.ConvertRoleDataToActorParam(roleData)
  3. local param = ActorParam.New()
  4. param.uid = roleData.uid
  5. param.baseId = roleData.id
  6. param.jobId = roleData.jobId or 0
  7. param.level = roleData.lv
  8. param.isMainRole = roleData.mainRole
  9. param.isRole = roleData.role
  10. param.strengthLevel = roleData.strengthLevel or 0
  11. if roleData.skills ~= nil then
  12. param.skills = System.Array.CreateInstance(Enum.TypeInfo.SkillParam, #roleData.skills)
  13. for i = 1, #roleData.skills do
  14. param.skills[i-1] = LuaToCSData.CreateSkillParamData(roleData.skills[i])
  15. end
  16. end
  17. if roleData.buffs ~= nil then
  18. param.buffs = System.Array.CreateInstance(Enum.TypeInfo.BuffParam, #roleData.buffs)
  19. for i =1, #roleData.buffs do
  20. param.buffs[i -1] = LuaToCSData.CreateBuffParamData(roleData.buffs[i])
  21. end
  22. --- buff 规则修改,如果后续无问题,上述注释可删除
  23. --local buffParamLs = ActorData.ParseBuffParam(roleData.buffs)
  24. --if buffParamLs then
  25. -- param.buffs = buffParamLs:ToArray()
  26. --end
  27. end
  28. param.fashion = roleData.fashion == nil and nil or LuaToCSData.CreateFashionParamData(param.baseId, param.jobId, roleData.fashion)
  29. return param
  30. end
  31. --- 创建 ActorParam 对象
  32. ---@param heroData table
  33. ---@param userData table
  34. ---@param skillData Class(SkillData)
  35. ---@param buffs table
  36. ---@param fashionData table
  37. ---@return ActorParam
  38. function LuaToCSData.CreateActorParamData(heroData, skillData, buffs, userData, fashionData)
  39. local param = ActorParam.New()
  40. param.uid = heroData.id
  41. param.baseId = (userData == nil or userData.roleCfgId == nil)and 0 or userData.roleCfgId
  42. param.jobId = heroData.configId or 0
  43. param.level = heroData.baseLevel
  44. param.headFrameId = heroData.headFrame or 0
  45. param.isMainRole = heroData.isMainRole
  46. param.isRole = heroData.isRole
  47. if param.isMainRole or param.isRole then
  48. param.strengthLevel = 7
  49. else
  50. param.strengthLevel = heroData.strengthLevel or 0
  51. end
  52. if param.baseId == 0 then
  53. param.baseId = param.jobId
  54. end
  55. local skills = skillData:GetUsedSkills()
  56. CommonUtil.ReplaceBattleSkillBySkillEquip(heroData, skills)
  57. param.skills = System.Array.CreateInstance(Enum.TypeInfo.SkillParam, #skills)
  58. local idx = 0
  59. for _,v in pairs(skills) do
  60. param.skills[idx] = LuaToCSData.CreateSkillParamData(v)
  61. idx = idx + 1
  62. end
  63. -- param.buffs = System.Array.CreateInstance(Enum.TypeInfo.BuffParam, #buffs)
  64. -- idx = 0
  65. -- for _,v in pairs(buffs) do
  66. -- param.buffs[idx] = LuaToCSData.CreateBuffParamData(v)
  67. -- idx = idx + 1
  68. -- end
  69. if buffs then
  70. local buffParamLs = ActorData.ParseBuffParam(buffs)
  71. if buffParamLs then
  72. param.buffs = buffParamLs:ToArray()
  73. end
  74. end
  75. if userData ~= nil and fashionData ~= nil then
  76. param.fashion = LuaToCSData.CreateFashionParamData(param.baseId, param.jobId, fashionData)
  77. end
  78. param.petId = heroData.battlePetId > 0 and heroData.battlePetId + Enum.ActorUidField.Pet or 0
  79. return param
  80. end
  81. function LuaToCSData.CreatePetActorParamData(petData)
  82. local param = ActorParam.New()
  83. param.uid = petData.id > 0 and petData.id + Enum.ActorUidField.Pet or 0
  84. param.baseId = petData.cfgId
  85. param.level = petData.level
  86. param.skills = System.Array.CreateInstance(Enum.TypeInfo.SkillParam, #petData.skillList)
  87. local idx = 0
  88. for _,v in pairs(petData.skillList) do
  89. local data = { skillId = v.cfgId, lv = v.level, rate = v.rate}
  90. param.skills[idx] = LuaToCSData.CreateSkillParamData(data)
  91. idx = idx + 1
  92. end
  93. return param
  94. end
  95. --- 创建 SkillParam 对象
  96. ---@param data table
  97. ---@return SkillParam
  98. function LuaToCSData.CreateSkillParamData(data)
  99. if data == nil or next(data) == nil then return nil end
  100. local rate = data.rate or 100
  101. return SkillParam.New(data.skillId, data.lv, rate)
  102. end
  103. --- 创建 BuffParam 对象
  104. ---@param data table
  105. ---@return BuffParam
  106. function LuaToCSData.CreateBuffParamData(data)
  107. if data == nil or next(data) == nil then return nil end
  108. return BuffParam.New(data[1], data[2], data[3], data[4])
  109. end
  110. --- 创建 FashionParam 对象
  111. ---@param data table
  112. ---@return FashionParam
  113. function LuaToCSData.CreateFashionParamData(roleCfgId, jobId, data)
  114. if data == nil or next(data) == nil then return nil end
  115. local hairColorParam = LuaToCSData.CreateHairColorParamData(data.hairColor)
  116. local bodies = {}
  117. --- 找是否有互斥的数据 (目前是头发)
  118. local isHideHair = false
  119. local headTopFashionId = data.fashionData[Enum.FashionSlotType.HeadTop]
  120. if headTopFashionId then
  121. local fashionData = ManagerContainer.CfgMgr:GetFashionById(headTopFashionId)
  122. if fashionData then
  123. isHideHair = fashionData.FashionHideHair
  124. end
  125. end
  126. -- local eyeBody = nil
  127. -- if data.eye ~= nil then
  128. -- bodies[#bodies + 1] = LuaToCSData.CreateEyeBodyPartParamData(data.eye)
  129. -- end
  130. -- local hairBody = nil
  131. -- if data.hair ~= nil then
  132. -- bodies[#bodies + 1] = LuaToCSData.CreateHairBodyPartParamData(data.hair)
  133. -- end
  134. local faceBody = LuaToCSData.CreateFacePartParamData(roleCfgId)
  135. if faceBody ~= nil then
  136. bodies[#bodies + 1] = faceBody
  137. end
  138. for i = Enum.FashionSlotType.HeadTop, Enum.FashionSlotType.Pupil do
  139. local fashionId = data.fashionData[i]
  140. if i == Enum.FashionSlotType.Weapon then
  141. local leftWeaponParam, rightWeaponParam = LuaToCSData.CreateWeaponPartParamData(roleCfgId, jobId, i, fashionId)
  142. if leftWeaponParam then
  143. bodies[#bodies + 1] = leftWeaponParam
  144. end
  145. if rightWeaponParam then
  146. bodies[#bodies + 1] = rightWeaponParam
  147. end
  148. else
  149. local fashionData = LuaToCSData.CreateBodyPartParamData(roleCfgId, jobId, i, fashionId, isHideHair)
  150. if fashionData ~= nil then
  151. bodies[#bodies + 1] = fashionData
  152. end
  153. end
  154. end
  155. local bodyArray = System.Array.CreateInstance(Enum.TypeInfo.BodyPartParam, #bodies)
  156. local num = 0
  157. for k,v in pairs(bodies) do
  158. bodyArray[num] = v
  159. num = num + 1
  160. end
  161. return FashionParam.New(bodies, hairColorParam)
  162. end
  163. --- 创建 HairColorParam 对象
  164. ---@param data table
  165. ---@return HairColorParam
  166. function LuaToCSData.CreateHairColorParamData(data)
  167. if data == nil then return nil end
  168. local cfgData = ManagerContainer.CfgMgr:GetCreateHairColorById(data)
  169. if cfgData == nil then
  170. return HairColorParam.New(Color.New(1,1,1), Vector2(0,0))
  171. end
  172. local clr = Color.New(cfgData.HairColour[1], cfgData.HairColour[2], cfgData.HairColour[3])
  173. local offset = Vector2(cfgData.UVOffset[1], cfgData.UVOffset[2])
  174. return HairColorParam.New(clr, offset)
  175. end
  176. --- 创建 眼睛部位 BodyPartParam 对象
  177. ---@param data table
  178. ---@return BodyPartParam
  179. function LuaToCSData.CreateEyeBodyPartParamData(data)
  180. if data == nil then return nil end
  181. local cfgData = ManagerContainer.CfgMgr:GetCreateEyeById(data)
  182. if cfgData == nil then
  183. return nil
  184. end
  185. return BodyPartParam.New(Enum.SkinSlotType.Eye, cfgData.EyeColour, Enum.SkinSlotBindBone[Enum.FashionSlotType.Eye], Vector3.zero, Vector3.zero, Vector3.zero)
  186. end
  187. --- 创建 头发部位 BodyPartParam 对象
  188. ---@param data table
  189. ---@return BodyPartParam
  190. function LuaToCSData.CreateHairBodyPartParamData(data)
  191. if data == nil then return nil end
  192. local cfgData = ManagerContainer.CfgMgr:GetCreateHairById(data)
  193. if cfgData == nil then
  194. return nil
  195. end
  196. return BodyPartParam.New(Enum.SkinSlotType.HairStyle, cfgData.HairAvatar, Enum.SkinSlotBindBone[Enum.FashionSlotType.Hair], Vector3.zero, Vector3.zero, Vector3.zero)
  197. end
  198. --- 创建 脸部位 BodyPartParam 对象
  199. ---@return BodyPartParam
  200. function LuaToCSData.CreateFacePartParamData(roleCfgId)
  201. if roleCfgId == nil then return nil end
  202. local roleCfgData = ManagerContainer.CfgMgr:GetRoleDataById(roleCfgId)
  203. local avatarId = nil
  204. if roleCfgData.Sex == Enum.SexType.Man then
  205. avatarId = GlobalConfig.Instance:GetConfigIntValue(GlobalConfig.c_man_face_model_path)
  206. else
  207. avatarId = GlobalConfig.Instance:GetConfigIntValue(GlobalConfig.c_woman_face_model_path)
  208. end
  209. local avatarPrefab = nil
  210. local roleAvatarCfg = ManagerContainer.CfgMgr:GetRoleAvatarById(avatarId)
  211. if roleAvatarCfg then
  212. avatarPrefab = roleAvatarCfg.AvatarPrefab
  213. end
  214. return BodyPartParam.New(Enum.SkinSlotType.Face, avatarPrefab, nil, Vector3.zero, Vector3.zero, Vector3.zero)
  215. end
  216. function LuaToCSData.CreateWeaponPartParamData(roleCfgId, jobId, type, data)
  217. local roleCfgData = ManagerContainer.CfgMgr:GetRoleDataById(roleCfgId)
  218. local leftWeaponParam, rightWeaponParam
  219. local fashionCfgData = nil
  220. local roleAvatarID = nil
  221. local roleAvatarCfg = nil
  222. if data then
  223. fashionCfgData = ManagerContainer.CfgMgr:GetFashionById(data)
  224. end
  225. if roleCfgData.LeftWeapon and roleCfgData.LeftWeapon ~= 0 then
  226. if fashionCfgData then
  227. roleAvatarID = fashionCfgData.RoleAvatarID
  228. end
  229. -- 去默认的配置中找
  230. if not roleAvatarID or roleAvatarID == 0 then
  231. roleAvatarID = roleCfgData.LeftWeapon
  232. end
  233. if roleAvatarID and roleAvatarID ~= 0 then
  234. roleAvatarCfg = ManagerContainer.CfgMgr:GetRoleAvatarById(roleAvatarID)
  235. end
  236. if roleAvatarCfg then
  237. leftWeaponParam = BodyPartParam.New(Enum.SkinSlotType.LeftHand, roleAvatarCfg.AvatarPrefab, Enum.SkinSlotBindBone[Enum.SkinSlotType.LeftHand], CommonUtil.TableToVector3(roleAvatarCfg.ModelPosition), CommonUtil.TableToVector3(roleAvatarCfg.ModelRotation), CommonUtil.TableToVector3(roleAvatarCfg.ModelScale, Vector3.one))
  238. else
  239. leftWeaponParam = BodyPartParam.New(Enum.SkinSlotType.LeftHand, nil, Enum.SkinSlotBindBone[Enum.SkinSlotType.LeftHand], Vector3.zero, Vector3.zero, Vector3.zero)
  240. end
  241. end
  242. roleAvatarID = nil
  243. roleAvatarCfg = nil
  244. if roleCfgData.RightWeapon and roleCfgData.RightWeapon ~= 0 then
  245. if fashionCfgData then
  246. roleAvatarID = fashionCfgData.ExtRoleAvatarID
  247. end
  248. -- 去默认的配置中找
  249. if not roleAvatarID or roleAvatarID == 0 then
  250. roleAvatarID = roleCfgData.RightWeapon
  251. end
  252. if roleAvatarID and roleAvatarID ~= 0 then
  253. roleAvatarCfg = ManagerContainer.CfgMgr:GetRoleAvatarById(roleAvatarID)
  254. end
  255. if roleAvatarCfg then
  256. rightWeaponParam = BodyPartParam.New(Enum.SkinSlotType.RightHand, roleAvatarCfg.AvatarPrefab, Enum.SkinSlotBindBone[Enum.SkinSlotType.RightHand], CommonUtil.TableToVector3(roleAvatarCfg.ModelPosition), CommonUtil.TableToVector3(roleAvatarCfg.ModelRotation), CommonUtil.TableToVector3(roleAvatarCfg.ModelScale, Vector3.one))
  257. else
  258. rightWeaponParam = BodyPartParam.New(Enum.SkinSlotType.RightHand, nil, Enum.SkinSlotBindBone[Enum.SkinSlotType.RightHand], Vector3.zero, Vector3.zero, Vector3.one)
  259. end
  260. end
  261. return leftWeaponParam, rightWeaponParam
  262. end
  263. --- 创建 其他部位 BodyPartParam 对象
  264. ---@param data table
  265. ---@return BodyPartParam
  266. function LuaToCSData.CreateBodyPartParamData(roleCfgId, jobId, type, data, isHideHair)
  267. local skinSlotType = Enum.FashionSlotToSkinSlot[type]
  268. if isHideHair and skinSlotType == Enum.SkinSlotType.HairStyle then
  269. return nil
  270. end
  271. local roleAvatarCfg = nil
  272. local roleAvatarID = nil
  273. local linkPoint = Enum.SkinSlotBindBone[skinSlotType]
  274. if data ~= nil then
  275. local cfgData = ManagerContainer.CfgMgr:GetFashionById(data)
  276. if cfgData then
  277. roleAvatarID = cfgData.ExtRoleAvatarID
  278. if not roleAvatarID or roleAvatarID == 0 then
  279. roleAvatarID = cfgData.RoleAvatarID
  280. end
  281. end
  282. end
  283. -- 去默认的配置中找
  284. if not roleAvatarID or roleAvatarID == 0 then
  285. local roleCfgData = ManagerContainer.CfgMgr:GetRoleDataById(roleCfgId)
  286. roleAvatarID = nil
  287. if type == Enum.FashionSlotType.HeadTop then
  288. roleAvatarID = roleCfgData.HeadTop
  289. elseif type == Enum.FashionSlotType.HeadMiddle then
  290. roleAvatarID = roleCfgData.HeadMiddle
  291. elseif type == Enum.FashionSlotType.HeadBottom then
  292. roleAvatarID = roleCfgData.HeadBottom
  293. elseif type == Enum.FashionSlotType.Cloth then
  294. roleAvatarID = roleCfgData.ClothId
  295. elseif type == Enum.FashionSlotType.BodyBack then
  296. roleAvatarID = roleCfgData.BodyBackId
  297. elseif type == Enum.FashionSlotType.HairStyle then
  298. roleAvatarID = roleCfgData.Hair
  299. elseif type == Enum.FashionSlotType.Pupil then
  300. roleAvatarID = roleCfgData.Eye
  301. end
  302. if roleAvatarID == "" then
  303. -- LogError("fashion type error "..type)
  304. return nil
  305. end
  306. end
  307. if roleAvatarID and roleAvatarID ~= 0 then
  308. roleAvatarCfg = ManagerContainer.CfgMgr:GetRoleAvatarById(roleAvatarID)
  309. end
  310. if roleAvatarCfg then
  311. return BodyPartParam.New(skinSlotType, roleAvatarCfg.AvatarPrefab, linkPoint, CommonUtil.TableToVector3(roleAvatarCfg.ModelPosition), CommonUtil.TableToVector3(roleAvatarCfg.ModelRotation), CommonUtil.TableToVector3(roleAvatarCfg.ModelScale, Vector3.one))
  312. else
  313. return BodyPartParam.New(skinSlotType, nil, linkPoint, Vector3.zero, Vector3.zero, Vector3.one)
  314. end
  315. end
  316. return LuaToCSData