public static void Move(AOIEntity aoiEntity, Cell newCell, Cell preCell) { aoiEntity.Cell = newCell; preCell.Remove(aoiEntity); newCell.Add(aoiEntity); // 通知订阅该newCell Enter的Unit foreach (KeyValuePair <long, AOIEntity> kv in newCell.SubsEnterEntities) { if (kv.Value.SubEnterCells.Contains(preCell.Id)) { continue; } kv.Value.EnterSight(aoiEntity); } // 通知订阅preCell leave的Unit foreach (KeyValuePair <long, AOIEntity> kv in preCell.SubsLeaveEntities) { // 如果新的cell仍然在对方订阅的subleave中 if (kv.Value.SubLeaveCells.Contains(newCell.Id)) { continue; } kv.Value.LeaveSight(aoiEntity); } }
public static void Add(this AOIManagerComponent self, AOIEntity aoiEntity, float x, float y) { int cellX = (int)(x * 1000) / AOIManagerComponent.CellSize; int cellY = (int)(y * 1000) / AOIManagerComponent.CellSize; if (aoiEntity.ViewDistance == 0) { aoiEntity.ViewDistance = 1; } AOIHelper.CalcEnterAndLeaveCell(aoiEntity, cellX, cellY, aoiEntity.SubEnterCells, aoiEntity.SubLeaveCells); // 遍历EnterCell foreach (long cellId in aoiEntity.SubEnterCells) { Cell cell = self.GetCell(cellId); aoiEntity.SubEnter(cell); } // 遍历LeaveCell foreach (long cellId in aoiEntity.SubLeaveCells) { Cell cell = self.GetCell(cellId); aoiEntity.SubLeave(cell); } // 自己加入的Cell Cell selfCell = self.GetCell(AOIHelper.CreateCellId(cellX, cellY)); aoiEntity.Cell = selfCell; selfCell.Add(aoiEntity); // 通知订阅该Cell Enter的Unit foreach (KeyValuePair <long, AOIEntity> kv in selfCell.SubsEnterEntities) { kv.Value.EnterSight(aoiEntity); } }