private void CreateGrid() { if (GridCreated) return; Cells = new List<WorldSceneCell>(); foreach (var navCell in Scene.Mesh.Zone.NavZoneDef.NavCells) { Cells.Add(new WorldSceneCell(navCell, Min)); } if (SubScene != null) { foreach (var navCell in SubScene.Scene.Mesh.Zone.NavZoneDef.NavCells) { Cells.Add(new WorldSceneCell(navCell, SubScene.Min)); } if (SubScene.SubScene != null) { foreach (var navCell in SubScene.SubScene.Scene.Mesh.Zone.NavZoneDef.NavCells) { Cells.Add(new WorldSceneCell(navCell, SubScene.SubScene.Min)); } } } var navBoxSize = TrinityGrid.NodeBoxSize; var searchBeginning = navBoxSize / 2; for (var x = Min.X + searchBeginning; x <= Max.X; x = x + navBoxSize) { for (var y = Min.Y + searchBeginning; y <= Max.Y; y = y + navBoxSize) { var cell = this.Cells.FirstOrDefault(c => c.IsInCell(x, y)); if (cell != null) { var navNode = new TrinityNode(new Vector3(x, y, cell.Z), navBoxSize, cell); Nodes.Add(navNode); } } } GridCreated = true; }
public List<TrinityNode> GetNeighbors(TrinityNode node, int distance = 1) { var neighbors = new List<TrinityNode>(); if (node == null) { return neighbors; } var gridPoint = node.GridPoint; if (gridPoint == default(GridPoint)) return neighbors; for (var x = gridPoint.X - distance; x <= gridPoint.X + distance; x++) { if (x < 0 || x > GridMaxX) continue; for (var y = gridPoint.Y - distance; y <= gridPoint.Y + distance; y++) { if (y < 0 || y > GridMaxY) continue; // Excluding itself if (x == gridPoint.X && y == gridPoint.Y) continue; var gridNode = InnerGrid[x, y]; if (gridNode != null) { neighbors.Add(gridNode); } } } return neighbors; }
public List<TrinityNode> GetNodesInRadius(TrinityNode node, float radius, bool circular = false) { //using (new PerformanceLogger(string.Format("[{0}] GetNodesInRadius (Radius: {1})", GetType().Name, radius), true, true)) //{ var gridRadius = ToGridDistance(radius); var neighbors = FindNodes(node, 0, gridRadius); neighbors.Add(node); //if (!circular) //{ return neighbors; //} //var v2Center = node.Center; //var radiusSqr = radius*radius; //return neighbors.Where(n => n.Center.DistanceSqr(v2Center) <= radiusSqr).ToList(); //} }
public TrinityNode GetNodeInDirection(TrinityNode node, Direction direction) { var x = ToGridDistance(node.Center.X); var y = ToGridDistance(node.Center.Y); switch (direction) { case Direction.West: x -= BaseSize; break; case Direction.North: y += BaseSize; break; case Direction.East: x += BaseSize; break; case Direction.South: y -= BaseSize; break; case Direction.NorthWest: x -= BaseSize; y += BaseSize; break; case Direction.SouthWest: x -= BaseSize; y -= BaseSize; break; case Direction.SouthEast: x += BaseSize; y -= BaseSize; break; case Direction.NorthEast: x += BaseSize; y += BaseSize; break; } return InnerGrid[x, y]; }
public List<TrinityNode> FindNodes(TrinityNode node, int minDistance, int maxDistance, Func<TrinityNode, bool> condition = null) { var neighbors = new List<TrinityNode>(); if (node == null) return neighbors; var gridX = ToGridDistance(node.Center.X); var gridY = ToGridDistance(node.Center.Y); var roundWidth = maxDistance >= 3 ? (int)Math.Round(maxDistance * 0.35, 0, MidpointRounding.AwayFromZero) : 1; var edgelength = maxDistance * 2 + 1; var v2Center = node.Center; var worldDistanceMax = maxDistance * BoxSize; var worldDistanceMin = minDistance * BoxSize; var worldDistanceMaxSqr = worldDistanceMax * worldDistanceMax; var worldDistanceMinSqr = worldDistanceMin * worldDistanceMin; var row = -1; for (var x = gridX - maxDistance; x <= gridX + maxDistance; x++) { row++; if (x < 0 || x > GridMaxX) continue; var col = 0; for (var y = gridY - maxDistance; y <= gridY + maxDistance; y++) { col++; if (y < 0 || y > GridMaxY) continue; if (x == gridX && y == gridY) continue; var gridNode = InnerGrid[x, y]; if (gridNode != null) { if (minDistance > 0 && (x > gridX - minDistance && x < gridX + minDistance && y > gridY - minDistance && y < gridY + minDistance)) { if (gridNode.Center.DistanceSqr(v2Center) <= worldDistanceMinSqr) continue; } if (row <= roundWidth || col <= roundWidth || edgelength - row <= roundWidth || edgelength - col <= roundWidth) { if (gridNode.Center.DistanceSqr(v2Center) >= worldDistanceMaxSqr) continue; } if (condition == null || condition(gridNode)) neighbors.Add(gridNode); } } } return neighbors; }