123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System.Collections.Generic;
- namespace ET.Server
- {
- [FriendOf(typeof(AOIManagerComponent))]
- [FriendOf(typeof(AOIEntity))]
- [FriendOf(typeof(Cell))]
- public static class AOIManagerComponentSystem
- {
- 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);
-
- foreach (long cellId in aoiEntity.SubEnterCells)
- {
- Cell cell = self.GetCell(cellId);
- aoiEntity.SubEnter(cell);
- }
-
- foreach (long cellId in aoiEntity.SubLeaveCells)
- {
- Cell cell = self.GetCell(cellId);
- aoiEntity.SubLeave(cell);
- }
-
- Cell selfCell = self.GetCell(AOIHelper.CreateCellId(cellX, cellY));
- aoiEntity.Cell = selfCell;
- selfCell.Add(aoiEntity);
-
- foreach (KeyValuePair<long, AOIEntity> kv in selfCell.SubsEnterEntities)
- {
- kv.Value.EnterSight(aoiEntity);
- }
- }
- public static void Remove(this AOIManagerComponent self, AOIEntity aoiEntity)
- {
- if (aoiEntity.Cell == null)
- {
- return;
- }
-
- aoiEntity.Cell.Remove(aoiEntity);
- foreach (KeyValuePair<long, AOIEntity> kv in aoiEntity.Cell.SubsLeaveEntities)
- {
- kv.Value.LeaveSight(aoiEntity);
- }
-
- foreach (long cellId in aoiEntity.SubEnterCells)
- {
- Cell cell = self.GetCell(cellId);
- aoiEntity.UnSubEnter(cell);
- }
- foreach (long cellId in aoiEntity.SubLeaveCells)
- {
- Cell cell = self.GetCell(cellId);
- aoiEntity.UnSubLeave(cell);
- }
-
- if (aoiEntity.SeeUnits.Count > 1)
- {
- Log.Error($"aoiEntity has see units: {aoiEntity.SeeUnits.Count}");
- }
- if (aoiEntity.BeSeeUnits.Count > 1)
- {
- Log.Error($"aoiEntity has beSee units: {aoiEntity.BeSeeUnits.Count}");
- }
- }
- private static Cell GetCell(this AOIManagerComponent self, long cellId)
- {
- Cell cell = self.GetChild<Cell>(cellId);
- if (cell == null)
- {
- cell = self.AddChildWithId<Cell>(cellId);
- }
- return cell;
- }
- public static void Move(AOIEntity aoiEntity, Cell newCell, Cell preCell)
- {
- aoiEntity.Cell = newCell;
- preCell.Remove(aoiEntity);
- newCell.Add(aoiEntity);
-
- foreach (KeyValuePair<long, AOIEntity> kv in newCell.SubsEnterEntities)
- {
- if (kv.Value.SubEnterCells.Contains(preCell.Id))
- {
- continue;
- }
- kv.Value.EnterSight(aoiEntity);
- }
-
- foreach (KeyValuePair<long, AOIEntity> kv in preCell.SubsLeaveEntities)
- {
-
- if (kv.Value.SubLeaveCells.Contains(newCell.Id))
- {
- continue;
- }
- kv.Value.LeaveSight(aoiEntity);
- }
- }
- public static void Move(this AOIManagerComponent self, AOIEntity aoiEntity, int cellX, int cellY)
- {
- long newCellId = AOIHelper.CreateCellId(cellX, cellY);
- if (aoiEntity.Cell.Id == newCellId)
- {
- return;
- }
-
- Cell newCell = self.GetCell(newCellId);
- Move(aoiEntity, newCell, aoiEntity.Cell);
- AOIHelper.CalcEnterAndLeaveCell(aoiEntity, cellX, cellY, aoiEntity.enterHashSet, aoiEntity.leaveHashSet);
-
- foreach (long cellId in aoiEntity.leaveHashSet)
- {
- if (aoiEntity.SubLeaveCells.Contains(cellId))
- {
- continue;
- }
- Cell cell = self.GetCell(cellId);
- aoiEntity.SubLeave(cell);
- }
-
- aoiEntity.SubLeaveCells.ExceptWith(aoiEntity.leaveHashSet);
- foreach (long cellId in aoiEntity.SubLeaveCells)
- {
- Cell cell = self.GetCell(cellId);
- aoiEntity.UnSubLeave(cell);
- }
-
- ObjectHelper.Swap(ref aoiEntity.SubLeaveCells, ref aoiEntity.leaveHashSet);
-
- foreach (long cellId in aoiEntity.enterHashSet)
- {
- if (aoiEntity.SubEnterCells.Contains(cellId))
- {
- continue;
- }
- Cell cell = self.GetCell(cellId);
- aoiEntity.SubEnter(cell);
- }
-
- aoiEntity.SubEnterCells.ExceptWith(aoiEntity.enterHashSet);
- foreach (long cellId in aoiEntity.SubEnterCells)
- {
- Cell cell = self.GetCell(cellId);
- aoiEntity.UnSubEnter(cell);
- }
-
- ObjectHelper.Swap(ref aoiEntity.SubEnterCells, ref aoiEntity.enterHashSet);
- }
- }
- }
|