Utility_Tips.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. ---@class Utility @公共函数
  2. Utility = class()
  3. local this = Utility
  4. function this.IsNilOrEmptyString(str)
  5. return type(str) ~= 'string' or str == nil or #str == 0
  6. end
  7. ----table operations
  8. ---@param tbl table
  9. ---@return number lua的table中的键值对数量
  10. function this.GetLuaTableCount(tbl)
  11. local count = 0
  12. if (tbl) then
  13. for k, v in pairs(tbl) do
  14. count = count + 1
  15. end
  16. end
  17. return count
  18. end
  19. function this.IsNullTable(tbl)
  20. if (tbl and type(tbl) == 'table') then
  21. for k, v in pairs(tbl) do
  22. return false
  23. end
  24. end
  25. return true
  26. end
  27. function this.IsContainsKey(tbl, key)
  28. if this.IsNullTable(tbl) then
  29. return false
  30. end
  31. return tbl[key] ~= nil
  32. end
  33. function this.IsContainsValue(tbl, value)
  34. if this.IsNullTable(tbl) then
  35. return false
  36. end
  37. for k, v in pairs(tbl) do
  38. if v == value then
  39. return true
  40. end
  41. end
  42. return false
  43. end
  44. ---移除对应value
  45. ---@return boolean 是否成功移除
  46. function this.RemoveTableValue(tbl, value)
  47. if this.IsNullTable(tbl) then
  48. return false
  49. end
  50. for k, v in pairs(tbl) do
  51. if v == value then
  52. table.remove(tbl, k)
  53. return true
  54. end
  55. end
  56. return false
  57. end
  58. ---不考虑元表,返回表中是否有该键
  59. ---@param tbl table 待检测的表
  60. ---@param key any 待检测的键
  61. ---@return boolean 表中是否包含了该键
  62. function this.IsTruelyContainsKey(tbl, key)
  63. if tbl == nil or type(tbl) ~= "table" or key == nil then
  64. return false
  65. end
  66. return rawget(tbl, key) ~= nil
  67. end
  68. ---获取表内第一个数据
  69. ---@param tbl table 表数据
  70. ---@return any 表的第一个value
  71. function this.GetTableFirstValue(tbl)
  72. if tbl ~= nil and type(tbl) == 'table' then
  73. for k, v in pairs(tbl) do
  74. return v
  75. end
  76. end
  77. end
  78. ---清空table
  79. ---@param tbl table 需要清空的table
  80. function this.ClearTable(tbl)
  81. if tbl then
  82. for i, v in pairs(tbl) do
  83. tbl[i] = nil
  84. end
  85. end
  86. end
  87. ---获取目标距离内的位置
  88. ---@param targetPoint Dot2
  89. ---@param distance number
  90. ---@return Vector3 位置
  91. --function this.RanthomPos(targetPoint, distance)
  92. -- local x = math.random(targetPoint.x, targetPoint.x + distance)
  93. -- local z = math.random(targetPoint.z, targetPoint.z + distance)
  94. -- return Scene.TileToWorldVec(x, z)
  95. --end
  96. ---获取子物体
  97. ---@param parent UnityEngine.Transform
  98. ---@param name string
  99. ---@return UnityEngine.Transform 位置
  100. function this.FindChildren(parent, name, isNotAbnormal)
  101. if parent then
  102. local objs = parent.gameObject:GetComponentsInChildren(typeof(CS.UnityEngine.Transform))
  103. if objs then
  104. for i = 0, objs.Length - 1 do
  105. if objs[i].name == name then
  106. return objs[i]
  107. end
  108. end
  109. end
  110. if not isNotAbnormal then
  111. logError("查找失败")
  112. end
  113. end
  114. end
  115. ---格式化数据
  116. -------diy覆盖-------
  117. --[==[
  118. function this.FormatNumber(count, integer_bit_notZero, isReplace_chinese_character)
  119. if not integer_bit_notZero then
  120. local str = count
  121. count = tonumber(count)
  122. if count >= 100000 and count < 10000000 then
  123. local num = string.format("%0.2f", count / 10000)
  124. str = num .. (isReplace_chinese_character and "w" or "万")
  125. elseif count > 10000000 then
  126. local num = string.format("%0.2f", count / 100000000)
  127. str = num .. (isReplace_chinese_character and "y" or "亿")
  128. end
  129. return str
  130. else
  131. local str = count
  132. if count >= 10000 and count < 100000000 then
  133. local num = string.format("%0.2f", count / 10000)
  134. if tonumber(num) == math.floor(num) then
  135. num = math.floor(num)
  136. end
  137. str = num .. (isReplace_chinese_character and "w" or "万")
  138. elseif count >= 100000000 then
  139. local num = tonumber(string.format("%0.2f", count / 100000000))
  140. if num < 10000 then
  141. str = (isReplace_chinese_character and "y" or "亿")
  142. elseif num >= 10000 and num < 100000000 then
  143. num = string.format("%0.2f", num / 10000)
  144. str = (isReplace_chinese_character and "z" or "兆")
  145. elseif num >= 100000000 then
  146. num = string.format("%0.2f", num / 100000000)
  147. str = (isReplace_chinese_character and "j" or "京")
  148. end
  149. if tonumber(num) == math.floor(num) then
  150. num = math.floor(num)
  151. end
  152. str = num .. str
  153. end
  154. return str
  155. end
  156. end]==]
  157. ---获取数字的个,十,百,位
  158. function this.GetNumberBit(number)
  159. local numberBit = {}
  160. table.insert(numberBit, number % 10)
  161. table.insert(numberBit, math.floor(number / 10))
  162. table.insert(numberBit, math.floor(number / 100))
  163. return numberBit
  164. end
  165. ---格式化时间--(10小时22分20秒)
  166. function this.FormatTime(time)
  167. time = time // 1000
  168. local s = string.format("%02s", time % 60)
  169. local m = string.format("%02s", time // 60 % 60)
  170. local h = string.format("%02s", time // 3600 % 24)
  171. local timeStr = ""
  172. if tonumber(h) ~= 0 then
  173. timeStr = h .. "时"
  174. end
  175. if tonumber(m) ~= 0 then
  176. timeStr = m .. "分"
  177. end
  178. timeStr = timeStr .. s .. "秒"
  179. return timeStr
  180. end
  181. ---计算时装评分
  182. --function this.CalculateFashionGrade(attTbl)
  183. -- local result = 0
  184. -- if attTbl ~= nil then
  185. -- for k, v in pairs(attTbl) do
  186. -- local attValue = ConfigManager.Get_cfg_att_info(v[1]) ---除以10000 ,小数向下取整
  187. -- local dataType = attValue.remarks
  188. -- local temp = 0
  189. -- if dataType == EAttrRemarkType.Number then
  190. -- temp = v[2] * attValue.attValue
  191. -- elseif dataType == EAttrRemarkType.Dimi then
  192. -- temp = v[2] * attValue.attValue / 10000
  193. -- end
  194. -- result = result + temp
  195. -- end
  196. -- end
  197. -- return math.floor(result)
  198. --end
  199. ---获取服务器开服自然天数(如果没有获取到服务器开服时间则返回-1)
  200. function this.GetServerOpenDays()
  201. if LoginManager == nil or LoginManager.EnterRoleRes == nil then
  202. return -1
  203. end
  204. local defTime = Time.GetServerTime() - LoginManager.EnterRoleRes.openServerTime + LoginManager.millisecond
  205. if defTime > 0 then
  206. local day = math.ceil(defTime / 86400000)
  207. return day
  208. end
  209. return -1
  210. end
  211. ---获取宝石品质底图(策划说不用配表的方式,直接写死)
  212. ---@param gemCfgId number
  213. function this.GetGemQualitySpriteName(gemCfgId, isCircle)
  214. local atlasName = "UIGemBottom"
  215. local spriteName = "icon_bottom_rarity0"
  216. ---@type cfg_gem_column
  217. --local gemCfg = ConfigManager.Get_cfg_gem(gemCfgId)
  218. if not gemCfg then
  219. return
  220. end
  221. if gemCfg.gemSubType == 1 then
  222. if isCircle then
  223. spriteName = "icon_circle_rarity0"
  224. else
  225. spriteName = "icon_bottom_rarity0"
  226. end
  227. elseif gemCfg.gemSubType == 2 then
  228. if isCircle then
  229. spriteName = "icon_circle_rarity1"
  230. else
  231. spriteName = "icon_bottom_rarity1"
  232. end
  233. elseif gemCfg.gemSubType == 3 then
  234. if isCircle then
  235. spriteName = "icon_circle_rarity2"
  236. else
  237. spriteName = "icon_bottom_rarity2"
  238. end
  239. elseif gemCfg.gemSubType == 4 then
  240. if isCircle then
  241. spriteName = "icon_circle_rarity3"
  242. else
  243. spriteName = "icon_bottom_rarity3"
  244. end
  245. elseif gemCfg.gemSubType == 5 then
  246. if isCircle then
  247. spriteName = "icon_circle_rarity4"
  248. else
  249. spriteName = "icon_bottom_rarity4"
  250. end
  251. end
  252. return atlasName, spriteName
  253. end
  254. ---设置角色头像
  255. ---@param panel UIControl
  256. ---@param career string @职业id
  257. ---@param imageCtrl UIControl @控件
  258. function this.SetHeadPortrait(panel, career, imageCtrl)
  259. if career then
  260. panel:SetSprite("MUIcon", "1" .. career, imageCtrl)
  261. else
  262. panel:SetSprite("MUIcon", "11", imageCtrl)
  263. end
  264. end
  265. ---格式化数据
  266. function this.FormatNumber(count, integer_bit_notZero, isReplace_chinese_character)
  267. if not integer_bit_notZero then
  268. local str = count
  269. count = tonumber(count)
  270. if count >= 100000 and count < 10000000 then
  271. local num = string.format("%0.1f", count / 10000)
  272. str = num .. (isReplace_chinese_character and "w" or "万")
  273. elseif count > 10000000 then
  274. local num = string.format("%0.1f", count / 100000000)
  275. str = num .. (isReplace_chinese_character and "y" or "亿")
  276. end
  277. return str
  278. else
  279. local str = count
  280. if count >= 10000 and count < 100000000 then
  281. local num = string.format("%0.1f", count / 10000)
  282. if tonumber(num) == math.floor(num) then
  283. num = math.floor(num)
  284. end
  285. str = num .. (isReplace_chinese_character and "w" or "万")
  286. elseif count >= 100000000 then
  287. local num = tonumber(string.format("%0.1f", count / 100000000))
  288. if num < 10000 then
  289. str = (isReplace_chinese_character and "y" or "亿")
  290. elseif num >= 10000 and num < 100000000 then
  291. num = string.format("%0.1f", num / 10000)
  292. str = (isReplace_chinese_character and "z" or "兆")
  293. elseif num >= 100000000 then
  294. num = string.format("%0.1f", num / 100000000)
  295. str = (isReplace_chinese_character and "j" or "京")
  296. end
  297. if tonumber(num) == math.floor(num) then
  298. num = math.floor(num)
  299. end
  300. str = num .. str
  301. end
  302. return str
  303. end
  304. end
  305. ---@param AIConfig cfg_partner_AI_column
  306. ---@param isNotHeadIcon boolean @是否不是头像
  307. function this.GetModelPosAndScale(uiParent, AIConfig, isNotHeadIcon)
  308. local x, y = uiParent.transform:GetRectSize()
  309. local partnerModelScale = { 1, 1, 1 }
  310. local partnerModelPos = { 0, 0, 0 }
  311. if isNotHeadIcon then
  312. partnerModelScale = { AIConfig.scaleUI[1], AIConfig.scaleUI[2], AIConfig.scaleUI[3] }
  313. partnerModelPos = { AIConfig.posUI[1], AIConfig.posUI[2], AIConfig.posUI[3] }
  314. else
  315. partnerModelScale = { AIConfig.scale[1], AIConfig.scale[2], AIConfig.scale[3] }
  316. partnerModelPos = { AIConfig.pos[1], AIConfig.pos[2], AIConfig.pos[3] }
  317. end
  318. local scaleRatio = y / 100
  319. if x / 100 < y / 100 then
  320. scaleRatio = x / 100
  321. end
  322. partnerModelPos[2] = partnerModelPos[2] * y / 100
  323. partnerModelScale[1] = partnerModelScale[1] * scaleRatio
  324. partnerModelScale[2] = partnerModelScale[2] * scaleRatio
  325. partnerModelScale[3] = partnerModelScale[3] * scaleRatio
  326. return partnerModelPos, partnerModelScale
  327. end
  328. ----@param srcDot Dot2
  329. ---@param searchRange number
  330. function this.FindNearCanMovePoint(srcDot,searchRange)
  331. local dot = Dot2.New(srcDot.x,srcDot.z)
  332. for i = 1, searchRange do
  333. local targetDot = this.FindNearPoint(dot,i)
  334. if targetDot then
  335. return targetDot
  336. end
  337. end
  338. return dot
  339. end
  340. ----@param dctDot Dot2
  341. ---@param searchRange number
  342. function this.FindNearPoint(dctDot,searchRange)
  343. for x = -searchRange, searchRange do
  344. for z = -searchRange, searchRange do
  345. if not CellManager.IsObstacle(dctDot.x+x,dctDot.z+z) then
  346. dctDot.x = dctDot.x + x
  347. dctDot.z = dctDot.z + z
  348. return dctDot
  349. end
  350. end
  351. end
  352. end