/// <summary> /// 删除一个 AOI 对象 ,同时有可能触发它相对其它 AOI 对象的离开消息。 /// </summary> /// <param name="self"></param> /// <param name="unit"></param> public static void RemoveUnit(this AOISceneComponent self, AOIUnitComponent unit) { Log.Info("RemoveUnit:" + unit.Id); unit.Scene = null; if (unit.Cell != null) { using (var ListenerGrids = unit.Cell.GetNearbyGrid(unit.Range)) { for (int i = 0; i < ListenerGrids.Count; i++) { var item = ListenerGrids[i]; item.RemoveListener(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 AOIRemoveUnit() { Receive = unit, Unit = t }); } } } } } unit.Cell.Remove(unit); } }
/// <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> /// 找到指定位置所在的Grid /// </summary> /// <param name="self"></param> /// <param name="pos"></param> /// <param name="create">没有是否创建</param> public static AOICell GetAOIGrid(this AOISceneComponent self, Vector3 pos, bool create = true) { int xIndex = (int)Math.Floor(pos.x / self.gridLen); int yIndex = (int)Math.Floor(pos.z / self.gridLen); return(self.GetCell(xIndex, yIndex, create)); }
/// <summary> /// 获取指定位置为中心指定圈数的所有格子 /// </summary> /// <param name="self"></param> /// <param name="turnNum"></param> /// <param name="posx"></param> /// <param name="posy"></param> /// <returns></returns> public static ListComponent <AOICell> GetNearbyGrid(this AOISceneComponent self, int turnNum, int posx, int posy) { ListComponent <AOICell> res = ListComponent <AOICell> .Create(); for (int i = 0; i <= turnNum * 2 + 1; i++) { var x = posx - turnNum + i; for (int j = 0; j <= turnNum * 2 + 1; j++) { var y = posy - turnNum + j; res.Add(self.GetCell(x, y)); } } return(res); }
private static AOICell GetCell(this AOISceneComponent self, int x, int y, bool create = true) { long cellId = AOIHelper.CreateCellId(x, y); AOICell cell = self.GetChild <AOICell>(cellId); if (cell == null && create) { cell = self.AddChildWithId <AOICell>(cellId); cell.xMin = x * self.gridLen; cell.xMax = cell.xMin + self.gridLen; cell.yMin = y * self.gridLen; cell.yMax = cell.yMin + self.gridLen; cell.posx = x; cell.posy = y; cell.halfDiagonal = self.halfDiagonal; } return(cell); }
//todo: private static RaycastHit[] RaycastAll(AOISceneComponent scene, Ray ray, UnitType[] type = null) { return(null); }
public static bool Raycast(AOISceneComponent scene, Ray ray, out RaycastHit hit, UnitType[] type = null) { hit = default; if (type == null) { return(false); } using (DictionaryComponent <UnitType, bool> typeTemp = DictionaryComponent <UnitType, bool> .Create()) { using (HashSetComponent <AOITriggerComponent> temp = HashSetComponent <AOITriggerComponent> .Create()) { for (int i = 0; i < type.Length; i++) { var item = type[i]; typeTemp.Add(item, true); } int xIndex = (int)Math.Floor(ray.Start.x / scene.gridLen); int yIndex = (int)Math.Floor(ray.Start.z / scene.gridLen); //z = kx+b float k = 0; float k_1 = 0; float b = 0; if (ray.Dir.x != 0 && ray.Dir.z != 0) { k = ray.Dir.z / ray.Dir.x; k_1 = ray.Dir.x / ray.Dir.z; b = ray.Start.z - k * ray.Start.x; } Vector3 inPoint = ray.Start; while (true) { long cellId = AOIHelper.CreateCellId(xIndex, yIndex); AOICell cell = scene.GetChild <AOICell>(cellId); var xMin = xIndex * scene.gridLen; var xMax = xMin + scene.gridLen; var yMin = yIndex * scene.gridLen; var yMax = yMin + scene.gridLen; //Log.Info("Raycast Check "+xIndex+" "+yIndex); if (cell != null) { ListComponent <RaycastHit> hits = ListComponent <RaycastHit> .Create(); RaycastHits(ray, cell, inPoint, hits, temp, typeTemp); if (hits.Count > 0) { hits.KSsort((i1, i2) => i1.Distance >= i2.Distance?1:-1);//从小到大 hit = hits[0]; //Log.Info("hits.Count > 0"+hit.Trigger.Parent.Parent.Id); hits.Dispose(); return(true); } } //一般情况 if (ray.Dir.x != 0 && ray.Dir.z != 0) { if (ray.Dir.x > 0 && ray.Dir.z > 0) { var z1 = xMax * k + b; if (z1 > yMin && z1 < yMax) { xIndex++; inPoint = new Vector3(xMax, inPoint.y + (xMax - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex++; inPoint = new Vector3((yMax - b) * k_1, inPoint.y + (yMax - inPoint.z) * ray.Dir.y / ray.Dir.z, yMax); } } else if (ray.Dir.x > 0 && ray.Dir.z < 0) { var z1 = xMax * k + b; if (z1 > yMin && z1 < yMax) { xIndex++; inPoint = new Vector3(xMax, inPoint.y + (xMax - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex--; inPoint = new Vector3((yMin - b) * k_1, inPoint.y + (yMin - inPoint.z) * ray.Dir.y / ray.Dir.z, yMin); } } else if (ray.Dir.x < 0 && ray.Dir.z < 0) { var z1 = xMin * k + b; if (z1 > yMin && z1 < yMax) { xIndex--; inPoint = new Vector3(xMin, inPoint.y + (xMin - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex--; inPoint = new Vector3((yMin - b) * k_1, inPoint.y + (yMin - inPoint.z) * ray.Dir.y / ray.Dir.z, yMin); } } else if (ray.Dir.x < 0 && ray.Dir.z > 0) { var z1 = xMin * k + b; if (z1 > yMin && z1 < yMax) { xIndex--; inPoint = new Vector3(xMin, inPoint.y + (xMin - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex++; inPoint = new Vector3((yMax - b) * k_1, inPoint.y + (yMax - inPoint.z) * ray.Dir.y / ray.Dir.z, yMax); } } else { Log.Error("What's f**k???"); } } //平行于轴了 else if (ray.Dir.x == 0 && ray.Dir.z != 0) { if (ray.Dir.z > 0) { yIndex++; inPoint = new Vector3(inPoint.x, inPoint.y + (yMax - inPoint.z) * ray.Dir.y / ray.Dir.z, yMax); } else { yIndex--; inPoint = new Vector3(inPoint.x, inPoint.y + (yMin - inPoint.z) * ray.Dir.y / ray.Dir.z, yMin); } } else if (ray.Dir.z == 0 && ray.Dir.x != 0) { if (ray.Dir.x > 0) { xIndex++; inPoint = new Vector3(xMax, inPoint.y + (xMax - inPoint.x) * ray.Dir.y / ray.Dir.x, inPoint.z); } else { xIndex--; inPoint = new Vector3(xMin, inPoint.y + (xMin - inPoint.x) * ray.Dir.y / ray.Dir.x, inPoint.z); } } //垂直于地图 else { break; } if (Vector3.Distance(inPoint, ray.Start) > ray.Distance) { break; } } } } return(false); }