/// <summary> /// 注册一个 AOI 对象, 同时设置其默认 AOI 半径。注:每个对象都有一个默认的 AOI 半径,凡第一次进入半径范围的其它物体,都会触发 AOI 消息。 /// </summary> /// <param name="self"></param> /// <param name="unit"></param> public static void RegisterUnit(this AOISceneComponent self, AOIUnitComponent unit) { unit.Scene = self; AOICell cell = self.GetAOIGrid(unit.Position); cell.Add(unit); Log.Info("RegisterUnit:" + unit.Id + " Position:" + unit.Position + " grid x:" + cell.posx + ",y:" + cell.posy + " type" + unit.Type + " range" + unit.Range); using (var ListenerGrids = cell.GetNearbyGrid(unit.Range)) { for (int i = 0; i < ListenerGrids.Count; i++) { var item = ListenerGrids[i]; item.AddListener(unit); if (unit.Type == UnitType.Player) { using (var list = item.GetAllUnit()) { for (int j = 0; j < list.Count; j++) { var t = list[j]; Game.EventSystem.Publish(new AOIRegisterUnit() { Receive = unit, Unit = t }); } } } } } }
/// <summary> /// 获取指定位置为中心指定圈数的所有格子 /// </summary> /// <param name="self"></param> /// <param name="turnNum"></param> /// <param name="pos"></param> /// <returns></returns> public static ListComponent <AOICell> GetNearbyGrid(this AOISceneComponent self, int turnNum, Vector3 pos) { var grid = self.GetAOIGrid(pos); ListComponent <AOICell> res = ListComponent <AOICell> .Create(); for (int i = 0; i <= turnNum * 2 + 1; i++) { var x = grid.posx - turnNum + i; for (int j = 0; j <= turnNum * 2 + 1; j++) { var y = grid.posy - turnNum + j; res.Add(self.GetCell(x, y)); } } return(res); }