EntityMgr.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. local EntityMgr = class("EntityMgr")
  2. local MinePlayerEntity = require("MinePlayerEntity")
  3. local OtherPlayerEntity = require("OtherPlayerEntity")
  4. function EntityMgr:ctor()
  5. self.entityMap = {}
  6. end
  7. function EntityMgr:Dispose()
  8. self:RemoveAllEntity()
  9. self.entityMap = nil
  10. end
  11. function EntityMgr:CreateEntity(entityType, uid, pos)
  12. local entity = self:GetEntity(entityType, uid)
  13. if entity then
  14. entity:SynPosition(pos)
  15. return entity
  16. end
  17. if entityType == Enum.EntityType.MinePlayer then
  18. entity = MinePlayerEntity:new(uid, pos, Quaternion.identity)
  19. elseif entityType == Enum.EntityType.OtherPlayer then
  20. entity = OtherPlayerEntity:new(uid, pos, Quaternion.identity)
  21. else
  22. LogError("[Wboy] unkonw EntityType : " .. tostring(entityType) .. " uid : " .. tostring(uid))
  23. end
  24. if entity then
  25. if not self.entityMap then self.entityMap = {} end
  26. local entityDic = self.entityMap[entityType]
  27. if not entityDic then
  28. entityDic = {}
  29. self.entityMap[entityType] = entityDic
  30. end
  31. entityDic[uid] = entity
  32. entity:EnterMap()
  33. end
  34. return entity
  35. end
  36. function EntityMgr:HasEntity(entityType, uid)
  37. if not self.entityMap then return false end
  38. local entityDic = self.entityMap[entityType]
  39. if not entityDic then return false end
  40. local entity = entityDic[uid]
  41. if not entity then return false end
  42. return true
  43. end
  44. function EntityMgr:GetEntity(entityType, uid)
  45. if not self.entityMap then return nil end
  46. local entityDic = self.entityMap[entityType]
  47. if not entityDic then return nil end
  48. return entityDic[uid]
  49. end
  50. function EntityMgr:GetEntitys(entityType)
  51. if not self.entityMap then return nil end
  52. local entityDic = self.entityMap[entityType]
  53. return entityDic
  54. end
  55. function EntityMgr:RemoveEntity(entityType, uid)
  56. if not self.entityMap then return end
  57. local entityDic = self.entityMap[entityType]
  58. if not entityDic then return end
  59. local entity = entityDic[uid]
  60. if not entity then return end
  61. entity:Dispose()
  62. entityDic[uid] = nil
  63. end
  64. function EntityMgr:RemoveAllEntity()
  65. if self.entityMap then
  66. for entityType, entityDic in pairs(self.entityMap) do
  67. if entityDic then
  68. for uid, entity in pairs(entityDic) do
  69. if entity then
  70. entity:Dispose()
  71. entityDic[uid] = nil
  72. end
  73. end
  74. end
  75. end
  76. end
  77. self.entityMap = {}
  78. end
  79. function EntityMgr:EnterMap(entityType, uid, pos)
  80. return self:CreateEntity(entityType, uid, pos)
  81. end
  82. function EntityMgr:ExitMap(entityType, uid)
  83. self:RemoveEntity(entityType, uid)
  84. end
  85. function EntityMgr:EnterWorld(entity)
  86. if not entity then return end
  87. entity:EnterWorld()
  88. end
  89. function EntityMgr:ExitWorld(entity)
  90. if not entity then return end
  91. entity:ExitWorld()
  92. end
  93. function EntityMgr:AllEntityEnterWorld()
  94. if self.entityMap then
  95. for entityType, entityDic in pairs(self.entityMap) do
  96. if entityDic then
  97. for uid, entity in pairs(entityDic) do
  98. if entity then
  99. entity:EnterWorld()
  100. end
  101. end
  102. end
  103. end
  104. end
  105. end
  106. function EntityMgr:AllEntityExitWorld()
  107. if self.entityMap then
  108. for entityType, entityDic in pairs(self.entityMap) do
  109. if entityDic then
  110. for uid, entity in pairs(entityDic) do
  111. if entity then
  112. entity:ExitWorld()
  113. end
  114. end
  115. end
  116. end
  117. end
  118. end
  119. function EntityMgr:Update(deltaTime)
  120. if self.entityMap then
  121. for entityType, entityDic in pairs(self.entityMap) do
  122. if entityDic then
  123. for uid, entity in pairs(entityDic) do
  124. if entity then
  125. entity:Update(deltaTime)
  126. end
  127. end
  128. end
  129. end
  130. end
  131. end
  132. function EntityMgr:SynPosition(entityType, uid, pos)
  133. local entity = self:GetEntity(entityType, uid)
  134. if entity then
  135. entity:SynPosition(pos)
  136. end
  137. end
  138. function EntityMgr:GetPosition(entityType, uid)
  139. local entity = self:GetEntity(entityType, uid)
  140. if entity then
  141. return entity:GetPosition()
  142. end
  143. return nil
  144. end
  145. function EntityMgr:GetTransform(entityType, uid)
  146. local entity = self:GetEntity(entityType, uid)
  147. if entity and entity.view then
  148. return entity.view:GetTransform()
  149. end
  150. return nil
  151. end
  152. function EntityMgr:ForceRefreshGenerateView(entityType, uid)
  153. local entity = self:GetEntity(entityType, uid)
  154. if entity then
  155. entity:ForceRefreshGenerateView()
  156. end
  157. end
  158. return EntityMgr