Пример #1
0
        public NodePath FindPath(IPathfindNodeNetwork <AstarNode> nodeNetwork, IPathRequest pathRequest, out bool succes)
        {
            var pathfindingNetwork = nodeNetwork.GetCollisionLayerNetwork(pathRequest.CollisionCategory);
            var startNode          = NodePointer.Dereference(pathRequest.PathStart.Index, pathfindingNetwork);
            var endNode            = NodePointer.Dereference(pathRequest.PathEnd.Index, pathfindingNetwork);
            var path = FindPath(pathfindingNetwork, startNode, endNode, pathRequest.AgentSize, pathRequest.CollisionCategory);

            if (path == null)
            {
                succes = false;
                return(new NodePath(new[] { startNode.DefinitionNode }, nodeNetwork.DefinitionNodeNetwork.Transformer));
            }
            succes = true;
            switch (nodeNetwork.DefinitionNodeNetwork)
            {
            case IDefinitionNodeGrid definitionNodeGrid:
                var offset = GridClearanceHelper.GridNodeOffset(pathRequest.AgentSize, definitionNodeGrid.Transformer.Scale);
                return(new NodePath(path.ToArray(), definitionNodeGrid.Transformer));

            case IDefinitionNodeNetwork definitionNodeNetwork:
                return(new NodePath(path.ToArray(), definitionNodeNetwork.Transformer));

            default:
                throw new NotSupportedException($"{nodeNetwork.DefinitionNodeNetwork.GetType()} is not supported");
            }
        }
Пример #2
0
        public PathRequest <PotentialField> CreatePathRequest(IPathfinder <PotentialField> pathfinder, IDefinitionNodeNetwork definitionNodes, float x1, float y1, float x2, float y2, PathfindaxCollisionCategory collisionLayer = PathfindaxCollisionCategory.None, byte agentSize = 1)
        {
            switch (definitionNodes)
            {
            case IDefinitionNodeGrid definitionNodeGrid:
                var offset    = -GridClearanceHelper.GridNodeOffset(agentSize, definitionNodeGrid.Transformer.Scale);
                var startNode = definitionNodeGrid.GetNode(x1 + offset.X, y1 + offset.Y);
                var endNode   = definitionNodeGrid.GetNode(x2 + offset.X, y2 + offset.Y);
                return(PathRequest.Create(pathfinder, startNode, endNode, collisionLayer, agentSize));

            default:
                throw new NotSupportedException($"{definitionNodes.GetType()} is not supported");
            }
        }
Пример #3
0
        public PotentialField FindPath(DijkstraNodeGrid dijkstraNodeNetwork, IPathRequest pathRequest, out bool succes)
        {
            try
            {
                if (pathRequest.AgentSize % 2 == 0)
                {
                    throw new InvalidAgentSizeException("Potential fields only support uneven agent sizes such as 1,3,5 etc.");
                }

                if (_potentialFieldCache == null || !_potentialFieldCache.TryGetValue(pathRequest, out var potentialField))
                {
                    var sw = Stopwatch.StartNew();
                    var pathfindingNetwork = dijkstraNodeNetwork.GetCollisionLayerNetwork(pathRequest.CollisionCategory);
                    var startNode          = NodePointer.Dereference(pathRequest.PathStart.Index, pathfindingNetwork);
                    var targetNode         = NodePointer.Dereference(pathRequest.PathEnd.Index, pathfindingNetwork);
                    if (_dijkstraAlgorithm.FindPath(pathfindingNetwork, targetNode, startNode, pathRequest))
                    {
                        potentialField = FindPath(dijkstraNodeNetwork, pathfindingNetwork, targetNode, pathRequest);
                    }
                    else
                    {
                        potentialField = new PotentialField(dijkstraNodeNetwork.DefinitionNodeGrid.Transformer, (Point2)targetNode.DefinitionNode.Position);
                    }
                    _potentialFieldCache?.Add(pathRequest, potentialField);
                    Debug.WriteLine($"Potentialfield created in {sw.ElapsedMilliseconds} ms.");
                }
                var nodeWorldPosition = potentialField.GridTransformer.ToWorld(pathRequest.PathStart.Position);
                var offset            = GridClearanceHelper.GridNodeOffset(pathRequest.AgentSize, dijkstraNodeNetwork.DefinitionNodeGrid.Transformer.Scale);
                succes = potentialField.GetHeading(nodeWorldPosition + offset).Length > 0;
                return(potentialField);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Debugger.Break();
                succes = false;
                return(null);
            }
        }