dropItemManager.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. local dropItemManager = class('dropItemManager')
  2. local tSort = table.sort
  3. local mAbs = math.abs
  4. local KEY_MAP_XY = KEY_MAP_XY
  5. function dropItemManager:ctor()
  6. self.mDropItemMapXYInCurrViewField = {}
  7. self.mDropItemInCurrViewFieldMap = {}
  8. self.mDropItemInCurrViewField = {}
  9. self.mDropItemIndexMap = {}
  10. self.mDropItemCount = 0
  11. end
  12. function dropItemManager:destory()
  13. if dropItemManager.instance then
  14. dropItemManager.instance = nil
  15. end
  16. end
  17. function dropItemManager:Inst()
  18. if not dropItemManager.instance then
  19. dropItemManager.instance = dropItemManager.new()
  20. end
  21. return dropItemManager.instance
  22. end
  23. function dropItemManager:Cleanup()
  24. self.mDropItemMapXYInCurrViewField = {}
  25. self.mDropItemInCurrViewFieldMap = {}
  26. self.mDropItemInCurrViewField = {}
  27. self.mDropItemIndexMap = {}
  28. self.mDropItemCount = 0
  29. end
  30. function dropItemManager:FindDropItem(id)
  31. return self.mDropItemInCurrViewFieldMap[id]
  32. end
  33. function dropItemManager:AddDropItem(item)
  34. if not item then
  35. return false
  36. end
  37. local itemID = item:GetID()
  38. if self.mDropItemInCurrViewFieldMap[itemID] then
  39. return false
  40. end
  41. self.mDropItemCount = self.mDropItemCount + 1
  42. local n = self.mDropItemCount
  43. self.mDropItemIndexMap[itemID] = n
  44. self.mDropItemInCurrViewField[n] = item
  45. self.mDropItemInCurrViewFieldMap[itemID] = item
  46. local point = KEY_MAP_XY(item:GetMapX(), item:GetMapY())
  47. if not self.mDropItemMapXYInCurrViewField[point] then
  48. self.mDropItemMapXYInCurrViewField[point] = {}
  49. end
  50. self.mDropItemMapXYInCurrViewField[point][itemID] = item
  51. end
  52. function dropItemManager:RmvDropItem(itemID)
  53. local item = self.mDropItemInCurrViewFieldMap[itemID]
  54. if not item then
  55. return false
  56. end
  57. self.mDropItemInCurrViewFieldMap[itemID] = nil
  58. local index = self.mDropItemIndexMap[itemID]
  59. if index == self.mDropItemCount then
  60. self.mDropItemInCurrViewField[index] = nil
  61. else
  62. -- last index --> index
  63. local lastActor = self.mDropItemInCurrViewField[self.mDropItemCount]
  64. self.mDropItemInCurrViewField[index] = lastActor
  65. self.mDropItemIndexMap[lastActor:GetID()] = index
  66. -- reset last value
  67. self.mDropItemInCurrViewField[self.mDropItemCount] = nil
  68. end
  69. self.mDropItemIndexMap[itemID] = nil
  70. self.mDropItemCount = self.mDropItemCount - 1
  71. local point = KEY_MAP_XY(item:GetMapX(), item:GetMapY())
  72. local itemMapXY = self.mDropItemMapXYInCurrViewField[point][itemID]
  73. if not itemMapXY then
  74. return false
  75. end
  76. self.mDropItemMapXYInCurrViewField[point][itemID] = nil
  77. end
  78. function dropItemManager:FindDropItemInCurrViewField(range, sortNearToFarFromPlayer, ownerID)
  79. if range then
  80. return self:FindDropItemInCurrViewFieldByRange(range, sortNearToFarFromPlayer, ownerID)
  81. end
  82. return self:FindDropItemInCurrViewFieldAll()
  83. end
  84. function dropItemManager:FindDropIDInCurrViewField(range, sortNearToFarFromPlayer, ownerID)
  85. if range then
  86. local itemRecvVec, num = self:FindDropItemInCurrViewFieldByRange(range, sortNearToFarFromPlayer, ownerID)
  87. local recvVec = {}
  88. for i, v in ipairs(itemRecvVec) do
  89. recvVec[i] = v:GetID()
  90. end
  91. return recvVec, num
  92. end
  93. local recvVec = {}
  94. local num = 0
  95. for i = 1, self.mDropItemCount do
  96. local m = self.mDropItemInCurrViewField[i]
  97. if m then
  98. num = num + 1
  99. recvVec[num] = m:GetID()
  100. end
  101. end
  102. return recvVec, num
  103. end
  104. function dropItemManager:FindDropItemInCurrViewFieldAll()
  105. return self.mDropItemInCurrViewField, self.mDropItemCount
  106. end
  107. function dropItemManager:FindDropItemInCurrViewFieldByRange(range, sortNearToFarFromPlayer, ownerID)
  108. if nil == sortNearToFarFromPlayer then
  109. sortNearToFarFromPlayer = false
  110. end
  111. if nil == ownerID then
  112. ownerID = global.MMO.ACTOR_ID_INVALID
  113. end
  114. local recvVec = {}
  115. local num = 0
  116. -- Find any items in range
  117. local function inRange(a, range)
  118. if -1 == range then return true end
  119. local p = global.gamePlayerController:GetMainPlayer()
  120. local mX = p:GetMapX()
  121. local mY = p:GetMapY()
  122. local dX = a:GetMapX() - mX
  123. local dY = a:GetMapY() - mY
  124. local diffA = (mAbs(dX) > mAbs(dY)) and mAbs(dX) or mAbs(dY)
  125. return diffA <= range
  126. end
  127. for i = 1, self.mDropItemCount do
  128. local m = self.mDropItemInCurrViewField[i]
  129. if m and (global.MMO.ACTOR_ID_INVALID == ownerID or m:CheckOwnerID(ownerID)) and inRange(m, range) then
  130. num = num + 1
  131. recvVec[num] = m
  132. end
  133. end
  134. -- Need to sort ?
  135. if sortNearToFarFromPlayer and num > 1 then
  136. local p = global.gamePlayerController:GetMainPlayer()
  137. local mX = p:GetMapX()
  138. local mY = p:GetMapY()
  139. local function distCmp(a, b)
  140. local dX = a:GetMapX() - mX
  141. local dY = a:GetMapY() - mY
  142. local diffA = (mAbs(dX) > mAbs(dY)) and mAbs(dX) or mAbs(dY)
  143. dX = b:GetMapX() - mX
  144. dY = b:GetMapY() - mY
  145. local diffB = (mAbs(dX) > mAbs(dY)) and mAbs(dX) or mAbs(dY)
  146. return diffA < diffB
  147. end
  148. tSort(recvVec, distCmp)
  149. end
  150. return recvVec, num
  151. end
  152. function dropItemManager:FindDropItemAllInMapXY(X, Y)
  153. local point = KEY_MAP_XY(X, Y)
  154. local itemMapXY = self.mDropItemMapXYInCurrViewField[point]
  155. return itemMapXY
  156. end
  157. return dropItemManager