AOIManagerComponentSystem.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections.Generic;
  2. namespace ET.Server
  3. {
  4. public static class AOIManagerComponentSystem
  5. {
  6. public static void Add(this AOIManagerComponent self, AOIEntity aoiEntity, float x, float y)
  7. {
  8. int cellX = (int)(x * 1000) / AOIManagerComponent.CellSize;
  9. int cellY = (int)(y * 1000) / AOIManagerComponent.CellSize;
  10. if (aoiEntity.ViewDistance == 0)
  11. {
  12. aoiEntity.ViewDistance = 1;
  13. }
  14. AOIHelper.CalcEnterAndLeaveCell(aoiEntity, cellX, cellY, aoiEntity.SubEnterCells, aoiEntity.SubLeaveCells);
  15. // 遍历EnterCell
  16. foreach (long cellId in aoiEntity.SubEnterCells)
  17. {
  18. Cell cell = self.GetCell(cellId);
  19. aoiEntity.SubEnter(cell);
  20. }
  21. // 遍历LeaveCell
  22. foreach (long cellId in aoiEntity.SubLeaveCells)
  23. {
  24. Cell cell = self.GetCell(cellId);
  25. aoiEntity.SubLeave(cell);
  26. }
  27. // 自己加入的Cell
  28. Cell selfCell = self.GetCell(AOIHelper.CreateCellId(cellX, cellY));
  29. aoiEntity.Cell = selfCell;
  30. selfCell.Add(aoiEntity);
  31. // 通知订阅该Cell Enter的Unit
  32. foreach (KeyValuePair<long, AOIEntity> kv in selfCell.SubsEnterEntities)
  33. {
  34. kv.Value.EnterSight(aoiEntity);
  35. }
  36. }
  37. public static void Remove(this AOIManagerComponent self, AOIEntity aoiEntity)
  38. {
  39. if (aoiEntity.Cell == null)
  40. {
  41. return;
  42. }
  43. // 通知订阅该Cell Leave的Unit
  44. aoiEntity.Cell.Remove(aoiEntity);
  45. foreach (KeyValuePair<long, AOIEntity> kv in aoiEntity.Cell.SubsLeaveEntities)
  46. {
  47. kv.Value.LeaveSight(aoiEntity);
  48. }
  49. // 通知自己订阅的Enter Cell,清理自己
  50. foreach (long cellId in aoiEntity.SubEnterCells)
  51. {
  52. Cell cell = self.GetCell(cellId);
  53. aoiEntity.UnSubEnter(cell);
  54. }
  55. foreach (long cellId in aoiEntity.SubLeaveCells)
  56. {
  57. Cell cell = self.GetCell(cellId);
  58. aoiEntity.UnSubLeave(cell);
  59. }
  60. // 检查
  61. if (aoiEntity.SeeUnits.Count > 1)
  62. {
  63. Log.Error($"aoiEntity has see units: {aoiEntity.SeeUnits.Count}");
  64. }
  65. if (aoiEntity.BeSeeUnits.Count > 1)
  66. {
  67. Log.Error($"aoiEntity has beSee units: {aoiEntity.BeSeeUnits.Count}");
  68. }
  69. }
  70. private static Cell GetCell(this AOIManagerComponent self, long cellId)
  71. {
  72. Cell cell = self.GetChild<Cell>(cellId);
  73. if (cell == null)
  74. {
  75. cell = self.AddChildWithId<Cell>(cellId);
  76. }
  77. return cell;
  78. }
  79. public static void Move(AOIEntity aoiEntity, Cell newCell, Cell preCell)
  80. {
  81. aoiEntity.Cell = newCell;
  82. preCell.Remove(aoiEntity);
  83. newCell.Add(aoiEntity);
  84. // 通知订阅该newCell Enter的Unit
  85. foreach (KeyValuePair<long, AOIEntity> kv in newCell.SubsEnterEntities)
  86. {
  87. if (kv.Value.SubEnterCells.Contains(preCell.Id))
  88. {
  89. continue;
  90. }
  91. kv.Value.EnterSight(aoiEntity);
  92. }
  93. // 通知订阅preCell leave的Unit
  94. foreach (KeyValuePair<long, AOIEntity> kv in preCell.SubsLeaveEntities)
  95. {
  96. // 如果新的cell仍然在对方订阅的subleave中
  97. if (kv.Value.SubLeaveCells.Contains(newCell.Id))
  98. {
  99. continue;
  100. }
  101. kv.Value.LeaveSight(aoiEntity);
  102. }
  103. }
  104. public static void Move(this AOIManagerComponent self, AOIEntity aoiEntity, int cellX, int cellY)
  105. {
  106. long newCellId = AOIHelper.CreateCellId(cellX, cellY);
  107. if (aoiEntity.Cell.Id == newCellId) // cell没有变化
  108. {
  109. return;
  110. }
  111. // 自己加入新的Cell
  112. Cell newCell = self.GetCell(newCellId);
  113. Move(aoiEntity, newCell, aoiEntity.Cell);
  114. AOIHelper.CalcEnterAndLeaveCell(aoiEntity, cellX, cellY, aoiEntity.enterHashSet, aoiEntity.leaveHashSet);
  115. // 算出自己leave新Cell
  116. foreach (long cellId in aoiEntity.leaveHashSet)
  117. {
  118. if (aoiEntity.SubLeaveCells.Contains(cellId))
  119. {
  120. continue;
  121. }
  122. Cell cell = self.GetCell(cellId);
  123. aoiEntity.SubLeave(cell);
  124. }
  125. // 算出需要通知离开的Cell
  126. aoiEntity.SubLeaveCells.ExceptWith(aoiEntity.leaveHashSet);
  127. foreach (long cellId in aoiEntity.SubLeaveCells)
  128. {
  129. Cell cell = self.GetCell(cellId);
  130. aoiEntity.UnSubLeave(cell);
  131. }
  132. // 这里交换两个HashSet,提高性能
  133. ObjectHelper.Swap(ref aoiEntity.SubLeaveCells, ref aoiEntity.leaveHashSet);
  134. // 算出自己看到的新Cell
  135. foreach (long cellId in aoiEntity.enterHashSet)
  136. {
  137. if (aoiEntity.SubEnterCells.Contains(cellId))
  138. {
  139. continue;
  140. }
  141. Cell cell = self.GetCell(cellId);
  142. aoiEntity.SubEnter(cell);
  143. }
  144. // 离开的Enter
  145. aoiEntity.SubEnterCells.ExceptWith(aoiEntity.enterHashSet);
  146. foreach (long cellId in aoiEntity.SubEnterCells)
  147. {
  148. Cell cell = self.GetCell(cellId);
  149. aoiEntity.UnSubEnter(cell);
  150. }
  151. // 这里交换两个HashSet,提高性能
  152. ObjectHelper.Swap(ref aoiEntity.SubEnterCells, ref aoiEntity.enterHashSet);
  153. }
  154. }
  155. }