// // PUBLIC // public List <Vector3> GetPositionPathTo(Vector3 startPos, Vector3 endPos) { List <Vector3> path = new List <Vector3>(); AI_Node startNode = GetNodeClosestTo(new Vector2(startPos.x, startPos.z)), endNode = GetNodeClosestTo(new Vector2(endPos.x, endPos.z)); //if (!startNode.IsValid) // startNode = startNode.GetValidNeighbour() ?? startNode; if (!endNode.IsValid) { endNode = endNode.GetValidNeighbour() ?? endNode; } var nodePath = GetPathTo(startNode, endNode); if (nodePath == null) { return(path); } foreach (var node in nodePath) { path.Add(new Vector3(node.NodePos.x, 0, node.NodePos.y)); } return(path); }
private void CreateNodeGrid() { _nodes = new AI_Node[AI_GridSizeX * AI_GridSizeY]; AI_Node currentNode; for (int y = 0; y < AI_GridSizeY; y++) { for (int x = 0; x < AI_GridSizeX; x++) { currentNode = new AI_Node(true, Vector2.zero); currentNode.MatrixPos = new Vector2Int(x, y); _nodes[x + y * AI_GridSizeX] = currentNode; if (x > 0) { _nodes[x - 1 + y * AI_GridSizeX].AddNeighbour(currentNode); currentNode.AddNeighbour(_nodes[x - 1 + y * AI_GridSizeX]); } if (y > 0) { _nodes[x + (y - 1) * AI_GridSizeX].AddNeighbour(currentNode); currentNode.AddNeighbour(_nodes[x + (y - 1) * AI_GridSizeX]); } } } }
private static void ClearAllNodeConnections() { // Get all objects in the scene List <GameObject> nodeObjs = new List <GameObject>(); foreach (GameObject GO in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]) { nodeObjs.Add(GO); } // Iterate through all of them and clear the nodes of all connections. for (int i = 0; i < nodeObjs.Count; i++) { AI_Node node = nodeObjs[i].GetComponent <AI_Node>(); if (node) { Undo.RecordObject(node, "Clearing every nodes connections"); node.rConNodes.Clear(); } } }
private bool IsNodeInSight(AI_Node startNode, AI_Node targetNode) { float t; int xDist = startNode.MatrixPos.x - targetNode.MatrixPos.x; xDist = xDist > 0 ? xDist : -xDist; int yDist = startNode.MatrixPos.y - targetNode.MatrixPos.y; yDist = yDist > 0 ? yDist : -yDist; int diagonalDist = Mathf.Max(xDist, yDist); bool sideSight; for (int step = 0; step < diagonalDist; step++) { sideSight = false; t = (step == 0) ? 0 : step / (float)diagonalDist; Vector2Int pointMatrixPos = Vector2Int.FloorToInt(Vector2.Lerp(startNode.MatrixPos, targetNode.MatrixPos, t)); if (!_nodes[pointMatrixPos.x + pointMatrixPos.y * AI_GridSizeX].IsValid) { return(false); } foreach (var side in _nodes[pointMatrixPos.x + pointMatrixPos.y * AI_GridSizeX].Neighbours) { if (side.IsValid) { sideSight = true; break; } } if (!sideSight) { return(false); } } return(true); }
public bool IsPositionOnSight(Vector2 startPos, Vector2 endPos) { AI_Node startNode = GetNodeClosestTo(startPos), endNode = GetNodeClosestTo(endPos); return(IsNodeInSight(startNode, endNode)); }
private List <AI_Node> GetPathTo(AI_Node startNode, AI_Node endNode) { //return ThetaStar.RunWithoutWeight(startNode, n => n == endNode, n => n.Neighbours, n => Vector2.Distance(n.NodePos, endNode.NodePos), IsNodeInSight, (n1, n2) => Vector2.Distance(n1.NodePos, n2.NodePos), n => n.IsValid, maxPathfindSteps); return(AStar.Run(startNode, n => n == endNode, n => n.Neighbours.Select(neighbour => (neighbour, 1f)), n => Vector2.Distance(endNode.NodePos, n.NodePos), maxPathfindSteps)); }
public void AddNeighbour(AI_Node node) { Neighbours.Add(node); }