MountInfo.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ---@class MountInfo
  2. ---@field isHasOwnerMount boolean
  3. MountInfo = class()
  4. local this = MountInfo
  5. function this:ctor()
  6. end
  7. function this:Reset()
  8. end
  9. function this:Init()
  10. self:InitData()
  11. self:RegistMessages()
  12. end
  13. function this:InitData()
  14. self.ride_mount_id = 0
  15. self.fightTime = Time.time
  16. self.exit_fight_time = 0
  17. if SL:HasConfig("cfg_global",810) then
  18. ---@type cfg_global_column
  19. local tbl = SL:GetConfig("cfg_global",810)
  20. self.exit_fight_time =tonumber(tbl.value)
  21. end
  22. ---@type cfg_mount_column
  23. local tbl = SL:GetConfig("cfg_mount",1)
  24. if tbl then
  25. self.effectId = tonumber(tbl.ridingEffect)
  26. end
  27. end
  28. function this:RegistMessages()
  29. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_CURRENT_RIDE_MOUNT,self.RES_CURRENT_RIDE_MOUNT,self)
  30. SL:RegisterLUAEvent(LUA_EVENT_MOUNT_ISON,self.LUA_EVENT_MOUNT_ISON,self)
  31. SL:RegisterLUAEvent(LUA_EVENT_PLAYER_MAPPOS_CHANGE,self.OnPlayerMapPosChange,self)
  32. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_MOUNT_LOGO,self.RES_MOUNT_LOGO,self)
  33. SL:RegisterLUAEvent(LUA_EVENT_FIGHT_RESULT, self.LUA_EVENT_FIGHT_RESULT, self)
  34. SL:RegisterLUAEvent(LUA_EVENT_MOUNTID_CHANGE, self.LUA_EVENT_MOUNTID_CHANGE, self)
  35. SL:RegisterLUAEvent(LUA_EVENT_ROLE_ACTION_CHANGE, self.LUA_EVENT_ROLE_ACTION_CHANGE, self)
  36. end
  37. function this:RES_CURRENT_RIDE_MOUNT(id,mountId)
  38. self.ride_mount_id = mountId
  39. end
  40. function this:LUA_EVENT_MOUNT_ISON(id,stateInfo)
  41. local rid = SL:GetMetaValue("MAIN_ACTOR_ID")
  42. if stateInfo.role.data.id == rid then
  43. local isDie = SL:GetMetaValue(EMetaVarGetKey.USER_IS_DIE)
  44. if not isDie then
  45. local isSafe = SL:GetMetaValue(EMetaVarGetKey.IN_SAFE_AREA)
  46. SL:SendLuaNetMsg(LuaMessageIdToSever.REQ_SET_MOUNT_IS_SHOW,(stateInfo.state and not isSafe) and 1 or 0)
  47. end
  48. end
  49. if self.effectId then
  50. if stateInfo.state then
  51. SL:SetMetaValue(EMetaVarSetKey.SET_EFFECT_ON_ROLE, stateInfo.role.data.id, tonumber(self.effectId), nil, true, EPlayEffectOverType.Loop)
  52. else
  53. SL:SetMetaValue(EMetaVarSetKey.RECYCLE_EFFECT_ON_ROLE,stateInfo.role.data.id, tonumber(self.effectId))
  54. end
  55. end
  56. end
  57. function this:OnPlayerMapPosChange()
  58. local isDie = SL:GetMetaValue(EMetaVarGetKey.USER_IS_DIE)
  59. local isSafe = SL:GetMetaValue(EMetaVarGetKey.IN_SAFE_AREA)
  60. if not isSafe and self.lastIsSafe and self.ride_mount_id ~= 0 and not isDie then
  61. SL:SendLuaNetMsg(LuaMessageIdToSever.REQ_SET_MOUNT_IS_SHOW,isSafe and 0 or 1)
  62. end
  63. self.lastIsSafe = isSafe
  64. end
  65. function this:RES_MOUNT_LOGO(id,message)
  66. self.isHasOwnerMount = message.logo == 1
  67. self.isRankShowMount = message.rank ~= 0
  68. end
  69. function this:LUA_EVENT_FIGHT_RESULT(id,message)
  70. if self.ride_mount_id ~= 0 then
  71. local rid = SL:GetMetaValue("MAIN_ACTOR_ID")
  72. rid = tonumber(rid)
  73. local isFight = message.targetId == rid or message.attackerId == rid
  74. if not isFight then
  75. for i, v in pairs(message.target) do
  76. if v.targetId == rid then
  77. isFight = true
  78. break
  79. end
  80. end
  81. end
  82. if isFight then
  83. local isOn = SL:GetMetaValue(EMetaVarGetKey.GET_PLAYER_IS_ON_MOUNTID,rid)
  84. local role = SL:GetRoleById(rid)
  85. if isOn or (role and role.data.MountId~=0) then
  86. SL:SendLuaNetMsg(LuaMessageIdToSever.REQ_SET_MOUNT_IS_SHOW,0)
  87. end
  88. self.fightTime = Time.time
  89. if self.timer then
  90. SL:UnSchedule(self.timer)
  91. self.timer = nil
  92. end
  93. self.timer = SL:Schedule(self.timer,0,1,self.exit_fight_time,function()
  94. if Time.time - self.fightTime >= self.exit_fight_time then--脱战
  95. local isDie = SL:GetMetaValue(EMetaVarGetKey.USER_IS_DIE)
  96. local isSafe = SL:GetMetaValue(EMetaVarGetKey.IN_SAFE_AREA)
  97. if not isDie and not isSafe then
  98. SL:SendLuaNetMsg(LuaMessageIdToSever.REQ_SET_MOUNT_IS_SHOW,1)
  99. end
  100. end
  101. end)
  102. end
  103. end
  104. end
  105. function this:LUA_EVENT_MOUNTID_CHANGE(id,data)
  106. if data.new ~= 0 then
  107. ---@type cfg_mount_column
  108. local tbl = SL:GetConfig("cfg_mount",data.new)
  109. if tbl then
  110. self.effectId = tbl.ridingEffect
  111. SL:SetMetaValue(EMetaVarSetKey.SET_EFFECT_ON_ROLE, data.role.data.id, tonumber(self.effectId), nil, true, EPlayEffectOverType.Loop)
  112. end
  113. elseif self.effectId then
  114. SL:SetMetaValue(EMetaVarSetKey.RECYCLE_EFFECT_ON_ROLE,data.role.data.id, tonumber(self.effectId))
  115. end
  116. end
  117. ---@param role Role
  118. function this:LUA_EVENT_ROLE_ACTION_CHANGE(id,role)
  119. local isSafe = SL:GetMetaValue(EMetaVarGetKey.IN_SAFE_AREA)
  120. if role.data.mountId and role.data.mountId ~= 0 then
  121. if role.data.action == EAction.Walk or role.data.action == EAction.Run then
  122. if not isSafe then
  123. ---@type cfg_mount_column
  124. local tbl = SL:GetConfig("cfg_mount",role.data.mountId)
  125. if tbl then
  126. self.effectId = tbl.ridingEffect
  127. SL:SetMetaValue(EMetaVarSetKey.SET_EFFECT_ON_ROLE, role.data.id, tonumber(self.effectId), nil, true, EPlayEffectOverType.Loop)
  128. end
  129. end
  130. elseif self.effectId then
  131. --SL:SetMetaValue(EMetaVarSetKey.RECYCLE_EFFECT_ON_ROLE,role.data.id, tonumber(self.effectId))
  132. end
  133. end
  134. end
  135. function this:Reset()
  136. self.lastIsSafe = false
  137. self.ride_mount_id = 0
  138. if self.timer then
  139. SL:UnSchedule(self.timer)
  140. self.timer = nil
  141. end
  142. end